git.iko.soy/typeable/openapi3
on master help sign in
Include the scheme when using http security (#10)

* Include the scheme when using http security * Add HttpSchemeCustom and optional bearerFormat

commitfc1d9a6fc2cb58fc5374ed823c6d26fa55de623f authorTommy Engström <tommy@tommyengstrom.com> dateWed 3 Mar 2021, 12:44 (6 years ago) parenta2d1509
2 files changed+29−4
src/Data/OpenApi.hs+1 src/Data/OpenApi/Internal.hs+28−4
src/Data/OpenApi.hs+1
@@ -91,6 +91,7 @@ module Data.OpenApi (
9191 -- ** Security
9292 SecurityScheme(..),
9393 SecuritySchemeType(..),
94+ HttpSchemeType(..),
9495 SecurityDefinitions(..),
9596 SecurityRequirement(..),
9697
src/Data/OpenApi/Internal.hs+28−4
@@ -838,8 +838,16 @@ data OAuth2Flows = OAuth2Flows
838838 , _oAuth2FlowsAuthorizationCode :: Maybe (OAuth2Flow OAuth2AuthorizationCodeFlow)
839839 } deriving (Eq, Show, Generic, Data, Typeable)
840840
841+type BearerFormat = Text
842+
843+data HttpSchemeType
844+ = HttpSchemeBearer (Maybe BearerFormat)
845+ | HttpSchemeBasic
846+ | HttpSchemeCustom Text
847+ deriving (Eq, Show, Generic, Data, Typeable)
848+
841849data SecuritySchemeType
842− = SecuritySchemeHttp
850+ = SecuritySchemeHttp HttpSchemeType
843851 | SecuritySchemeApiKey ApiKeyParams
844852 | SecuritySchemeOAuth2 OAuth2Flows
845853 | SecuritySchemeOpenIdConnect URL
@@ -1229,8 +1237,19 @@ instance ToJSON OAuth2Flows where
12291237 toEncoding = sopSwaggerGenericToEncoding
12301238
12311239instance ToJSON SecuritySchemeType where
1232− toJSON SecuritySchemeHttp
1233− = object [ "type" .= ("http" :: Text) ]
1240+ toJSON (SecuritySchemeHttp ty) = case ty of
1241+ HttpSchemeBearer mFmt ->
1242+ object $ [ "type" .= ("http" :: Text)
1243+ , "scheme" .= ("bearer" :: Text)
1244+ ] <> maybe [] (\t -> ["bearerFormat" .= t]) mFmt
1245+ HttpSchemeBasic ->
1246+ object [ "type" .= ("http" :: Text)
1247+ , "scheme" .= ("basic" :: Text)
1248+ ]
1249+ HttpSchemeCustom t ->
1250+ object [ "type" .= ("http" :: Text)
1251+ , "scheme" .= t
1252+ ]
12341253 toJSON (SecuritySchemeApiKey params)
12351254 = toJSON params
12361255 <+> object [ "type" .= ("apiKey" :: Text) ]
@@ -1379,7 +1398,12 @@ instance FromJSON SecuritySchemeType where
13791398 parseJSON js@(Object o) = do
13801399 (t :: Text) <- o .: "type"
13811400 case t of
1382− "http" -> pure SecuritySchemeHttp
1401+ "http" -> do
1402+ scheme <- o .: "scheme"
1403+ SecuritySchemeHttp <$> case scheme of
1404+ "bearer" -> HttpSchemeBearer <$> (o .:! "bearerFormat")
1405+ "basic" -> pure HttpSchemeBasic
1406+ t -> pure $ HttpSchemeCustom t
13831407 "apiKey" -> SecuritySchemeApiKey <$> parseJSON js
13841408 "oauth2" -> SecuritySchemeOAuth2 <$> (o .: "flows")
13851409 "openIdConnect" -> SecuritySchemeOpenIdConnect <$> (o .: "openIdConnectUrl")