Skip to content

Commit

Permalink
Merge pull request #3333 from Jibbarth/fix/oauth-security-definition-…
Browse files Browse the repository at this point in the history
…openapi

[OpenApi] [Oauth] Fix render oauth when using OpenApi v3
  • Loading branch information
soyuka committed Jan 20, 2020
2 parents 14ed547 + 8dccfa3 commit 24fa80c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
22 changes: 18 additions & 4 deletions src/Swagger/Serializer/DocumentationNormalizer.php
Expand Up @@ -631,15 +631,29 @@ private function computeDoc(bool $v3, Documentation $documentation, \ArrayObject
$security = [];

if ($this->oauthEnabled) {
$securityDefinitions['oauth'] = [
'type' => $this->oauthType,
'description' => 'OAuth client_credentials Grant',
'flow' => $this->oauthFlow,
$oauthAttributes = [
'tokenUrl' => $this->oauthTokenUrl,
'authorizationUrl' => $this->oauthAuthorizationUrl,
'scopes' => $this->oauthScopes,
];

$securityDefinitions['oauth'] = [
'type' => $this->oauthType,
'description' => sprintf(
'OAuth 2.0 %s Grant',
strtolower(preg_replace('/[A-Z]/', ' \\0', lcfirst($this->oauthFlow)))
),
];

if ($v3) {
$securityDefinitions['oauth']['flows'] = [
$this->oauthFlow => $oauthAttributes,
];
} else {
$securityDefinitions['oauth']['flow'] = $this->oauthFlow;
$securityDefinitions['oauth'] = array_merge($securityDefinitions['oauth'], $oauthAttributes);
}

$security[] = ['oauth' => []];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Swagger/Serializer/DocumentationNormalizerV2Test.php
Expand Up @@ -492,7 +492,7 @@ interface_exists(AdvancedNameConverterInterface::class)
'securityDefinitions' => [
'oauth' => [
'type' => 'oauth2',
'description' => 'OAuth client_credentials Grant',
'description' => 'OAuth 2.0 application Grant',
'flow' => 'application',
'tokenUrl' => '/oauth/v2/token',
'authorizationUrl' => '/oauth/v2/auth',
Expand Down
15 changes: 9 additions & 6 deletions tests/Swagger/Serializer/DocumentationNormalizerV3Test.php
Expand Up @@ -483,7 +483,7 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void
$legacy ? $nameConverter : null,
true,
'oauth2',
'application',
'authorizationCode',
'/oauth/v2/token',
'/oauth/v2/auth',
['scope param'],
Expand Down Expand Up @@ -554,11 +554,14 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void
'securitySchemes' => [
'oauth' => [
'type' => 'oauth2',
'description' => 'OAuth client_credentials Grant',
'flow' => 'application',
'tokenUrl' => '/oauth/v2/token',
'authorizationUrl' => '/oauth/v2/auth',
'scopes' => ['scope param'],
'description' => 'OAuth 2.0 authorization code Grant',
'flows' => [
'authorizationCode' => [
'tokenUrl' => '/oauth/v2/token',
'authorizationUrl' => '/oauth/v2/auth',
'scopes' => ['scope param'],
],
],
],
],
],
Expand Down

0 comments on commit 24fa80c

Please sign in to comment.