Skip to content

Commit

Permalink
Output scopes field when empty (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanWinther authored Oct 26, 2022
1 parent c2e706b commit 8285df8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Data/OpenApi/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,10 @@ instance ToJSONKey MediaType where
toJSONKey = JSON.toJSONKeyText (Text.pack . show)

instance (Eq p, ToJSON p, AesonDefaultValue p) => ToJSON (OAuth2Flow p) where
toJSON = sopSwaggerGenericToJSON
toJSON a = sopSwaggerGenericToJSON a &
if InsOrdHashMap.null (_oAuth2Scopes a)
then (<+> object ["scopes" .= object []])
else id
toEncoding = sopSwaggerGenericToEncoding

instance ToJSON OAuth2Flows where
Expand Down
36 changes: 34 additions & 2 deletions test/Data/OpenApiSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ spec = do
describe "Responses Definition Object" $ responsesDefinitionExample <=> responsesDefinitionExampleJSON
describe "Security Definitions Object" $ securityDefinitionsExample <=> securityDefinitionsExampleJSON
describe "OAuth2 Security Definitions with merged Scope" $ oAuth2SecurityDefinitionsExample <=> oAuth2SecurityDefinitionsExampleJSON
describe "OAuth2 Security Definitions with empty Scope" $ oAuth2SecurityDefinitionsEmptyExample <=> oAuth2SecurityDefinitionsEmptyExampleJSON
describe "Composition Schema Example" $ compositionSchemaExample <=> compositionSchemaExampleJSON
describe "Swagger Object" $ do
context "Example with no paths" $ emptyPathsFieldExample <=> emptyPathsFieldExampleJSON
Expand All @@ -47,7 +48,7 @@ spec = do
(toJSON :: OpenApi -> Value) <$> fromJSON petstoreExampleJSON `shouldBe` Success petstoreExampleJSON
context "Security schemes" $ do
it "merged correctly" $ do
let merged = oAuth2SecurityDefinitionsReadOpenApi <> oAuth2SecurityDefinitionsWriteOpenApi
let merged = oAuth2SecurityDefinitionsReadOpenApi <> oAuth2SecurityDefinitionsWriteOpenApi <> oAuth2SecurityDefinitionsEmptyOpenApi
merged `shouldBe` oAuth2SecurityDefinitionsOpenApi

main :: IO ()
Expand Down Expand Up @@ -508,10 +509,22 @@ oAuth2SecurityDefinitionsWriteExample = SecurityDefinitions
, _securitySchemeDescription = Nothing })
]

oAuth2SecurityDefinitionsEmptyExample :: SecurityDefinitions
oAuth2SecurityDefinitionsEmptyExample = SecurityDefinitions
[ ("petstore_auth", SecurityScheme
{ _securitySchemeType = SecuritySchemeOAuth2 (mempty & implicit ?~ OAuth2Flow
{ _oAuth2Params = OAuth2ImplicitFlow "http://swagger.io/api/oauth/dialog"
, _oAath2RefreshUrl = Nothing
, _oAuth2Scopes = []
} )
, _securitySchemeDescription = Nothing })
]

oAuth2SecurityDefinitionsExample :: SecurityDefinitions
oAuth2SecurityDefinitionsExample =
oAuth2SecurityDefinitionsWriteExample <>
oAuth2SecurityDefinitionsReadExample
oAuth2SecurityDefinitionsReadExample <>
oAuth2SecurityDefinitionsEmptyExample

oAuth2SecurityDefinitionsExampleJSON :: Value
oAuth2SecurityDefinitionsExampleJSON = [aesonQQ|
Expand All @@ -531,6 +544,21 @@ oAuth2SecurityDefinitionsExampleJSON = [aesonQQ|
}
|]

oAuth2SecurityDefinitionsEmptyExampleJSON :: Value
oAuth2SecurityDefinitionsEmptyExampleJSON = [aesonQQ|
{
"petstore_auth": {
"type": "oauth2",
"flows": {
"implicit": {
"scopes": {},
"authorizationUrl": "http://swagger.io/api/oauth/dialog"
}
}
}
}
|]

oAuth2SecurityDefinitionsReadOpenApi :: OpenApi
oAuth2SecurityDefinitionsReadOpenApi =
mempty & components . securitySchemes .~ oAuth2SecurityDefinitionsReadExample
Expand All @@ -539,6 +567,10 @@ oAuth2SecurityDefinitionsWriteOpenApi :: OpenApi
oAuth2SecurityDefinitionsWriteOpenApi =
mempty & components . securitySchemes .~ oAuth2SecurityDefinitionsWriteExample

oAuth2SecurityDefinitionsEmptyOpenApi :: OpenApi
oAuth2SecurityDefinitionsEmptyOpenApi =
mempty & components . securitySchemes .~ oAuth2SecurityDefinitionsEmptyExample

oAuth2SecurityDefinitionsOpenApi :: OpenApi
oAuth2SecurityDefinitionsOpenApi =
mempty & components . securitySchemes .~ oAuth2SecurityDefinitionsExample
Expand Down

0 comments on commit 8285df8

Please sign in to comment.