-
-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fixes to openapi-3.0 support of security definitions * fixes to openapi-3.0 support of security definitions * updated config to show open api 3.0 security example * minor code cleanup * refactored * code cleanup * styleci fixes * added testing tweaks for more code coverage * styleci fixed * styleci fixed * skip testing for OpenApi 3 * skip tests when version swagger / open api versions are different then supported test * fixed test for mixing settings * styleci fix
- Loading branch information
1 parent
ee1de13
commit 4ad03c4
Showing
7 changed files
with
214 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace L5Swagger; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class SecurityDefinitions | ||
{ | ||
/** | ||
* Reads in the l5-swagger configuration and appends security settings to documentation. | ||
* | ||
* @param string $filename The path to the generated json documentation | ||
*/ | ||
public function generate($filename) | ||
{ | ||
$securityConfig = config('l5-swagger.security', []); | ||
|
||
if (is_array($securityConfig) && ! empty($securityConfig)) { | ||
$documentation = collect( | ||
json_decode(file_get_contents($filename)) | ||
); | ||
|
||
$openApi3 = version_compare(config('l5-swagger.swagger_version'), '3.0', '>='); | ||
|
||
$documentation = $openApi3 ? | ||
$this->generateOpenApi($documentation, $securityConfig) : | ||
$this->generateSwaggerApi($documentation, $securityConfig); | ||
|
||
file_put_contents($filename, $documentation->toJson()); | ||
} | ||
} | ||
|
||
/** | ||
* Inject security settings for Swagger 1 & 2. | ||
* | ||
* @param Collection $documentation The parse json | ||
* @param array $securityConfig The security settings from l5-swagger | ||
* | ||
* @return Collection | ||
*/ | ||
public function generateSwaggerApi(Collection $documentation, array $securityConfig) | ||
{ | ||
$securityDefinitions = collect(); | ||
if ($documentation->has('securityDefinitions')) { | ||
$securityDefinitions = collect($documentation->get('securityDefinitions')); | ||
} | ||
|
||
foreach ($securityConfig as $key => $cfg) { | ||
$securityDefinitions->offsetSet($key, self::arrayToObject($cfg)); | ||
} | ||
|
||
$documentation->offsetSet('securityDefinitions', $securityDefinitions); | ||
|
||
return $documentation; | ||
} | ||
|
||
/** | ||
* Inject security settings for OpenApi 3. | ||
* | ||
* @param Collection $documentation The parse json | ||
* @param array $securityConfig The security settings from l5-swagger | ||
* | ||
* @return Collection | ||
*/ | ||
public function generateOpenApi(Collection $documentation, array $securityConfig) | ||
{ | ||
$components = collect(); | ||
if ($documentation->has('components')) { | ||
$components = collect($documentation->get('components')); | ||
} | ||
|
||
$securitySchemes = collect(); | ||
if ($components->has('securitySchemes')) { | ||
$securitySchemes = collect($components->get('securitySchemes')); | ||
} | ||
|
||
foreach ($securityConfig as $key => $cfg) { | ||
$securitySchemes->offsetSet($key, self::arrayToObject($cfg)); | ||
} | ||
|
||
$components->offsetSet('securitySchemes', $securitySchemes); | ||
|
||
$documentation->offsetSet('components', $components); | ||
|
||
return $documentation; | ||
} | ||
|
||
/** | ||
* Converts an array to an object. | ||
* | ||
* @param $array | ||
* | ||
* @return object | ||
*/ | ||
public static function arrayToObject($array) | ||
{ | ||
return json_decode(json_encode($array)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use L5Swagger\Generator; | ||
|
||
class SecurityDefinitionsTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function canGenerateApiJsonFileWithSecurityDefinition() | ||
{ | ||
if ($this->isOpenApi()) { | ||
$this->markTestSkipped('only for openApi 2.0'); | ||
} | ||
$this->setAnnotationsPath(); | ||
|
||
$cfg = config('l5-swagger'); | ||
$security = [ | ||
'new_api_key_securitye' => [ | ||
'type' => 'apiKey', | ||
'name' => 'api_key_name', | ||
'in' => 'query', | ||
], | ||
]; | ||
$cfg['security'] = $security; | ||
$cfg['swagger_version'] = '2.0'; | ||
config(['l5-swagger' => $cfg]); | ||
|
||
tap(new Generator)->generateDocs(); | ||
|
||
$this->assertTrue(file_exists($this->jsonDocsFile())); | ||
|
||
$this->get(route('l5-swagger.docs')) | ||
->assertSee('new_api_key_securitye') | ||
->assertJsonFragment($security) | ||
->isOk(); | ||
} | ||
|
||
/** @test */ | ||
public function canGenerateApiJsonFileWithSecurityDefinitionOpenApi3() | ||
{ | ||
if (! $this->isOpenApi()) { | ||
$this->markTestSkipped('only for openApi 3.0'); | ||
} | ||
$this->setAnnotationsPath(); | ||
|
||
$cfg = config('l5-swagger'); | ||
$security = [ | ||
'new_api_key_securitye' => [ | ||
'type' => 'apiKey', | ||
'name' => 'api_key_name', | ||
'in' => 'query', | ||
], | ||
]; | ||
$cfg['security'] = $security; | ||
$cfg['swagger_version'] = '3.0'; | ||
config(['l5-swagger' => $cfg]); | ||
|
||
tap(new Generator)->generateDocs(); | ||
|
||
$this->assertTrue(file_exists($this->jsonDocsFile())); | ||
|
||
$this->get(route('l5-swagger.docs')) | ||
->assertSee('new_api_key_securitye') | ||
->assertJsonFragment($security) | ||
->isOk(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters