git.iko.soy/typeable/openapi3
on master help sign in
Add 'scheme' field for 'http' security scheme type
commit9ea13c605cc01add03710560864c3ce015f8ab5c authorUladzimir Stsepchanka <st.uladzimir@protonmail.com> dateMon 9 Nov 2020, 16:58 (6 years ago) parentf8e978e
5 files changed+63−6
openapi3.cabal+1 src/Data/OpenApi.hs+3 src/Data/OpenApi/Internal.hs+36−4 test/Data/OpenApiSpec.hs+12−2 test/SpecCommon.hs+11
openapi3.cabal+1
@@ -88,6 +88,7 @@ library
8888 , uuid-types >=1.0.3 && <1.1
8989 , vector >=0.12.0.1 && <0.13
9090 , QuickCheck >=2.10.1 && <2.15
91+ , generic-arbitrary >=0.1 && <0.2
9192
9293 default-language: Haskell2010
9394
src/Data/OpenApi.hs+3
@@ -94,6 +94,9 @@ module Data.OpenApi (
9494 SecurityDefinitions(..),
9595 SecurityRequirement(..),
9696
97+ -- ** Http Scheme
98+ HttpScheme(..),
99+
97100 -- *** API key
98101 ApiKeyParams(..),
99102 ApiKeyLocation(..),
src/Data/OpenApi/Internal.hs+36−4
@@ -41,6 +41,7 @@ import Network.HTTP.Media (MediaType, mainType, parameters, parseAc
4141 (/:))
4242import Network.Socket (HostName, PortNumber)
4343import Text.Read (readMaybe)
44+import Test.QuickCheck.Arbitrary.Generic
4445
4546import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
4647import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
@@ -774,6 +775,20 @@ data Header = Header
774775 , _headerSchema :: Maybe (Referenced Schema)
775776 } deriving (Eq, Show, Generic, Data, Typeable)
776777
778+-- | According to RFC7235
779+data HttpScheme
780+ = Basic
781+ | Bearer
782+ | Digest
783+ | HOBA
784+ | Mutual
785+ | Negotiate
786+ | OAuth
787+ | SCRAM_SHA_1
788+ | SCRAM_SHA_256
789+ | Vapid
790+ deriving (Eq, Show, Generic, Data, Typeable)
791+
777792-- | The location of the API key.
778793data ApiKeyLocation
779794 = ApiKeyQuery
@@ -839,7 +854,7 @@ data OAuth2Flows = OAuth2Flows
839854 } deriving (Eq, Show, Generic, Data, Typeable)
840855
841856data SecuritySchemeType
842− = SecuritySchemeHttp
857+ = SecuritySchemeHttp HttpScheme
843858 | SecuritySchemeApiKey ApiKeyParams
844859 | SecuritySchemeOAuth2 OAuth2Flows
845860 | SecuritySchemeOpenIdConnect URL
@@ -1127,6 +1142,13 @@ instance ToJSON License where
11271142instance ToJSON ServerVariable where
11281143 toJSON = genericToJSON (jsonPrefix "ServerVariable")
11291144
1145+instance ToJSON HttpScheme where
1146+ toJSON scheme = case scheme of
1147+ SCRAM_SHA_1 -> "SCRAM-SHA-1"
1148+ SCRAM_SHA_256 -> "SCRAM-SHA-256"
1149+ Vapid -> "vapid"
1150+ other -> genericToJSON defaultOptions other
1151+
11301152instance ToJSON ApiKeyLocation where
11311153 toJSON = genericToJSON (jsonPrefix "ApiKey")
11321154
@@ -1182,6 +1204,13 @@ instance FromJSON License where
11821204instance FromJSON ServerVariable where
11831205 parseJSON = genericParseJSON (jsonPrefix "ServerVariable")
11841206
1207+instance FromJSON HttpScheme where
1208+ parseJSON = withText "HttpScheme" $ \txt -> case txt of
1209+ "SCRAM-SHA-1" -> pure SCRAM_SHA_1
1210+ "SCRAM-SHA-256" -> pure SCRAM_SHA_256
1211+ "vapid" -> pure Vapid
1212+ _ -> genericParseJSON defaultOptions $ String txt
1213+
11851214instance FromJSON ApiKeyLocation where
11861215 parseJSON = genericParseJSON (jsonPrefix "ApiKey")
11871216
@@ -1229,8 +1258,8 @@ instance ToJSON OAuth2Flows where
12291258 toEncoding = sopSwaggerGenericToEncoding
12301259
12311260instance ToJSON SecuritySchemeType where
1232− toJSON SecuritySchemeHttp
1233− = object [ "type" .= ("http" :: Text) ]
1261+ toJSON (SecuritySchemeHttp scheme)
1262+ = object [ "type" .= ("http" :: Text), "scheme" .= toJSON scheme ]
12341263 toJSON (SecuritySchemeApiKey params)
12351264 = toJSON params
12361265 <+> object [ "type" .= ("apiKey" :: Text) ]
@@ -1379,7 +1408,7 @@ instance FromJSON SecuritySchemeType where
13791408 parseJSON js@(Object o) = do
13801409 (t :: Text) <- o .: "type"
13811410 case t of
1382− "http" -> pure SecuritySchemeHttp
1411+ "http" -> SecuritySchemeHttp <$> o .: "scheme"
13831412 "apiKey" -> SecuritySchemeApiKey <$> parseJSON js
13841413 "oauth2" -> SecuritySchemeOAuth2 <$> (o .: "flows")
13851414 "openIdConnect" -> SecuritySchemeOpenIdConnect <$> (o .: "openIdConnectUrl")
@@ -1550,3 +1579,6 @@ instance AesonDefaultValue MimeList where defaultValue = Just mempty
15501579instance AesonDefaultValue Info
15511580instance AesonDefaultValue ParamLocation
15521581instance AesonDefaultValue Link
1582+
1583+instance Arbitrary HttpScheme where
1584+ arbitrary = genericArbitrary
test/Data/OpenApiSpec.hs+12−2
@@ -13,9 +13,11 @@ import Data.Aeson
1313import Data.Aeson.QQ.Simple
1414import Data.HashMap.Strict (HashMap)
1515import qualified Data.HashSet.InsOrd as InsOrdHS
16+import Data.Proxy
1617import Data.Text (Text)
1718
1819import Data.OpenApi
20+import Data.OpenApi.Internal
1921import SpecCommon
2022import Test.Hspec hiding (example)
2123
@@ -45,6 +47,7 @@ spec = do
4547 fromJSON petstoreExampleJSON `shouldSatisfy` (\x -> case x of Success (_ :: OpenApi) -> True; _ -> False)
4648 it "roundtrips: fmap toJSON . fromJSON" $ do
4749 (toJSON :: OpenApi -> Value) <$> fromJSON petstoreExampleJSON `shouldBe` Success petstoreExampleJSON
50+ testJsonIdempotency (Proxy :: Proxy HttpScheme)
4851
4952main :: IO ()
5053main = hspec spec
@@ -456,7 +459,10 @@ securityDefinitionsExample = SecurityDefinitions
456459 , _oAuth2Scopes =
457460 [ ("write:pets", "modify pets in your account")
458461 , ("read:pets", "read your pets") ] } )
459− , _securitySchemeDescription = Nothing }) ]
462+ , _securitySchemeDescription = Nothing })
463+ , ("basic", SecurityScheme
464+ { _securitySchemeType = SecuritySchemeHttp Basic
465+ , _securitySchemeDescription = Nothing })]
460466
461467securityDefinitionsExampleJSON :: Value
462468securityDefinitionsExampleJSON = [aesonQQ|
@@ -477,7 +483,11 @@ securityDefinitionsExampleJSON = [aesonQQ|
477483 "authorizationUrl": "http://swagger.io/api/oauth/dialog"
478484 }
479485 }
480− }
486+ },
487+ "basic": {
488+ "type": "http",
489+ "scheme": "Basic"
490+ }
481491}
482492
483493|]
test/SpecCommon.hs+11
@@ -4,9 +4,12 @@ import Data.Aeson
44import Data.ByteString.Builder (toLazyByteString)
55import qualified Data.Foldable as F
66import qualified Data.HashMap.Strict as HashMap
7+import Data.Proxy
8+import Data.Typeable
79import qualified Data.Vector as Vector
810
911import Test.Hspec
12+import Test.QuickCheck (Arbitrary, property)
1013
1114isSubJSON :: Value -> Value -> Bool
1215isSubJSON Null _ = True
@@ -28,3 +31,11 @@ x <=> js = do
2831 eitherDecode (encode $ toJSON x) `shouldBe` Right x
2932 it "roundtrips with toEncoding" $ do
3033 eitherDecode (toLazyByteString $ fromEncoding $ toEncoding x) `shouldBe` Right x
34+
35+testJsonIdempotency :: (Eq a, Show a, Arbitrary a, FromJSON a, ToJSON a, Typeable a, HasCallStack) =>
36+ Proxy a -> Spec
37+testJsonIdempotency px = describe (show ty) $ do
38+ it "roundtrips: decode . encode" $ property $ \x ->
39+ decode (encode x) == Just (x `asProxyTypeOf` px)
40+ where
41+ ty = typeOf $ undefined `asProxyTypeOf` px