@@ -1,6 +1,6 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE DataKinds #-}
23{-# LANGUAGE DeriveDataTypeable #-}
3−{-# LANGUAGE DeriveFunctor #-}
44{-# LANGUAGE DeriveGeneric #-}
55{-# LANGUAGE DeriveTraversable #-}
66{-# LANGUAGE FlexibleContexts #-}
@@ -14,7 +14,7 @@
1414{-# LANGUAGE TemplateHaskell #-}
1515{-# LANGUAGE TypeFamilies #-}
1616{-# LANGUAGE UndecidableInstances #-}
17−module Data.Swagger.Internal where
17+module Data.OpenApi.Internal where
1818
1919import Prelude ()
2020import Prelude.Compat
@@ -22,6 +22,9 @@ import Prelude.Compat
2222import Control.Applicative
2323import Control.Lens ((&), (.~), (?~))
2424import Data.Aeson hiding (Encoding)
25+#if MIN_VERSION_aeson(2,0,0)
26+import qualified Data.Aeson.KeyMap as KeyMap
27+#endif
2528import qualified Data.Aeson.Types as JSON
2629import Data.Data (Constr, Data (..), DataType, Fixity (..), Typeable,
2730 constrIndex, mkConstr, mkDataType)
@@ -46,58 +49,56 @@ import Text.Read (readMaybe)
4649import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
4750import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4851
52+import Data.OpenApi.Aeson.Compat (deleteKey)
53+import Data.OpenApi.Internal.AesonUtils (AesonDefaultValue (..), HasSwaggerAesonOptions (..),
54+ mkSwaggerAesonOptions, saoAdditionalPairs, saoSubObject,
55+ sopSwaggerGenericParseJSON, sopSwaggerGenericToEncoding,
56+ sopSwaggerGenericToJSON, sopSwaggerGenericToJSONWithOpts)
57+import Data.OpenApi.Internal.Utils
4958import Generics.SOP.TH (deriveGeneric)
50−import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
51− ,sopSwaggerGenericToJSONWithOpts
52− ,sopSwaggerGenericParseJSON
53− ,HasSwaggerAesonOptions(..)
54− ,AesonDefaultValue(..)
55− ,mkSwaggerAesonOptions
56− ,saoAdditionalPairs
57− ,saoSubObject)
58−import Data.Swagger.Internal.Utils
59−import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToEncoding)
6059
6160-- $setup
6261-- >>> :seti -XDataKinds
6362-- >>> import Data.Aeson
63+-- >>> import Data.ByteString.Lazy.Char8 as BSL
64+-- >>> import Data.OpenApi.Internal.Utils
6465
6566-- | A list of definitions that can be used in references.
6667type Definitions = InsOrdHashMap Text
6768
6869-- | This is the root document object for the API specification.
69−data Swagger = Swagger
70+data OpenApi = OpenApi
7071 { -- | Provides metadata about the API.
7172 -- The metadata can be used by the clients if needed.
72− _swaggerInfo :: Info
73+ _openApiInfo :: Info
7374
7475 -- | An array of Server Objects, which provide connectivity information
7576 -- to a target server. If the servers property is not provided, or is an empty array,
7677 -- the default value would be a 'Server' object with a url value of @/@.
77− , _swaggerServers :: [Server]
78+ , _openApiServers :: [Server]
7879
7980 -- | The available paths and operations for the API.
80− , _swaggerPaths :: InsOrdHashMap FilePath PathItem
81+ , _openApiPaths :: InsOrdHashMap FilePath PathItem
8182
8283 -- | An element to hold various schemas for the specification.
83− , _swaggerComponents :: Components
84+ , _openApiComponents :: Components
8485
8586 -- | A declaration of which security mechanisms can be used across the API.
8687 -- The list of values includes alternative security requirement objects that can be used.
8788 -- Only one of the security requirement objects need to be satisfied to authorize a request.
8889 -- Individual operations can override this definition.
8990 -- To make security optional, an empty security requirement can be included in the array.
90− , _swaggerSecurity :: [SecurityRequirement]
91+ , _openApiSecurity :: [SecurityRequirement]
9192
9293 -- | A list of tags used by the specification with additional metadata.
9394 -- The order of the tags can be used to reflect on their order by the parsing tools.
9495 -- Not all tags that are used by the 'Operation' Object must be declared.
9596 -- The tags that are not declared MAY be organized randomly or based on the tools' logic.
9697 -- Each tag name in the list MUST be unique.
97− , _swaggerTags :: InsOrdHashSet Tag
98+ , _openApiTags :: InsOrdHashSet Tag
9899
99100 -- | Additional external documentation.
100− , _swaggerExternalDocs :: Maybe ExternalDocs
101+ , _openApiExternalDocs :: Maybe ExternalDocs
101102 } deriving (Eq, Show, Generic, Data, Typeable)
102103
103104-- | The object provides metadata about the API.
@@ -194,8 +195,8 @@ data Components = Components
194195 , _componentsParameters :: Definitions Param
195196 , _componentsExamples :: Definitions Example
196197 , _componentsRequestBodies :: Definitions RequestBody
197− , _componentsHeader :: Definitions Header
198− , _componentsSecuritySchemes :: Definitions SecurityScheme
198+ , _componentsHeaders :: Definitions Header
199+ , _componentsSecuritySchemes :: SecurityDefinitions
199200 , _componentsLinks :: Definitions Link
200201 , _componentsCallbacks :: Definitions Callback
201202 } deriving (Eq, Show, Generic, Data, Typeable)
@@ -336,9 +337,11 @@ data RequestBody = RequestBody
336337 -- | The content of the request body.
337338 -- The key is a media type or media type range and the value describes it.
338339 -- For requests that match multiple keys, only the most specific key is applicable.
339− -- e.g. @text/plain@ overrides @text/*@
340+ -- e.g. @text/plain@ overrides @text/\*@
340341 , _requestBodyContent :: InsOrdHashMap MediaType MediaTypeObject
341342
343+ -- | Determines if the request body is required in the request.
344+ -- Defaults to 'False'.
342345 , _requestBodyRequired :: Maybe Bool
343346 } deriving (Eq, Show, Generic, Data, Typeable)
344347
@@ -391,7 +394,7 @@ data Encoding = Encoding
391394 -- for other primitive types – @text/plain@; for object - @application/json@;
392395 -- for array – the default is defined based on the inner type.
393396 -- The value can be a specific media type (e.g. @application/json@),
394− -- a wildcard media type (e.g. @image/*@), or a comma-separated list of the two types.
397+ -- a wildcard media type (e.g. @image/\*@), or a comma-separated list of the two types.
395398 _encodingContentType :: Maybe MediaType
396399
397400 -- | A map allowing additional information to be provided as headers,
@@ -431,11 +434,11 @@ mimeListConstr :: Constr
431434mimeListConstr = mkConstr mimeListDataType "MimeList" ["getMimeList"] Prefix
432435
433436mimeListDataType :: DataType
434−mimeListDataType = mkDataType "Data.Swagger.MimeList" [mimeListConstr]
437+mimeListDataType = mkDataType "Data.OpenApi.MimeList" [mimeListConstr]
435438
436439instance Data MimeList where
437440 gunfold k z c = case constrIndex c of
438− 1 -> k (z (\xs -> MimeList (map fromString xs)))
441+ 1 -> k (z (MimeList . map fromString))
439442 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type MimeList."
440443 toConstr (MimeList _) = mimeListConstr
441444 dataTypeOf _ = mimeListDataType
@@ -449,7 +452,7 @@ data Param = Param
449452
450453 -- | A brief description of the parameter.
451454 -- This could contain examples of use.
452− -- GFM syntax can be used for rich text representation.
455+ -- CommonMark syntax MAY be used for rich text representation.
453456 , _paramDescription :: Maybe Text
454457
455458 -- | Determines whether this parameter is mandatory.
@@ -469,6 +472,13 @@ data Param = Param
469472 -- a parameter with an empty value. Default value is @false@.
470473 , _paramAllowEmptyValue :: Maybe Bool
471474
475+ -- | Determines whether the parameter value SHOULD allow reserved characters,
476+ -- as defined by [RFC3986](https://tools.ietf.org/html/rfc3986#section-2.2)
477+ -- @:/?#[]@!$&'()*+,;=@ to be included without percent-encoding.
478+ -- This property only applies to parameters with an '_paramIn' value of 'ParamQuery'.
479+ -- The default value is 'False'.
480+ , _paramAllowReserved :: Maybe Bool
481+
472482 -- | Parameter schema.
473483 , _paramSchema :: Maybe (Referenced Schema)
474484
@@ -499,6 +509,10 @@ data Param = Param
499509 -- Furthermore, if referencing a schema that contains an example,
500510 -- the examples value SHALL override the example provided by the schema.
501511 , _paramExamples :: InsOrdHashMap Text (Referenced Example)
512+
513+ -- TODO
514+ -- _paramContent :: InsOrdHashMap MediaType MediaTypeObject
515+ -- should be singleton. mutually exclusive with _paramSchema.
502516 } deriving (Eq, Show, Generic, Data, Typeable)
503517
504518data Example = Example
@@ -559,27 +573,27 @@ data Link = Link
559573 , _linkServer :: Maybe Server
560574 } deriving (Eq, Show, Generic, Typeable, Data)
561575
562−-- | Items for @'SwaggerArray'@ schemas.
576+-- | Items for @'OpenApiArray'@ schemas.
563577--
564578-- __Warning__: OpenAPI 3.0 does not support tuple arrays. However, OpenAPI 3.1 will, as
565579-- it will incorporate Json Schema mostly verbatim.
566580--
567−-- @'SwaggerItemsObject'@ should be used to specify homogenous array @'Schema'@s.
581+-- @'OpenApiItemsObject'@ should be used to specify homogenous array @'Schema'@s.
568582--
569−-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
570−data SwaggerItems where
571− SwaggerItemsObject :: Referenced Schema -> SwaggerItems
572− SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems
583+-- @'OpenApiItemsArray'@ should be used to specify tuple @'Schema'@s.
584+data OpenApiItems where
585+ OpenApiItemsObject :: Referenced Schema -> OpenApiItems
586+ OpenApiItemsArray :: [Referenced Schema] -> OpenApiItems
573587 deriving (Eq, Show, Typeable, Data)
574588
575−data SwaggerType where
576− SwaggerString :: SwaggerType
577− SwaggerNumber :: SwaggerType
578− SwaggerInteger :: SwaggerType
579− SwaggerBoolean :: SwaggerType
580− SwaggerArray :: SwaggerType
581− SwaggerNull :: SwaggerType
582− SwaggerObject :: SwaggerType
589+data OpenApiType where
590+ OpenApiString :: OpenApiType
591+ OpenApiNumber :: OpenApiType
592+ OpenApiInteger :: OpenApiType
593+ OpenApiBoolean :: OpenApiType
594+ OpenApiArray :: OpenApiType
595+ OpenApiNull :: OpenApiType
596+ OpenApiObject :: OpenApiType
583597 deriving (Eq, Show, Typeable, Generic, Data)
584598
585599data ParamLocation
@@ -624,9 +638,33 @@ data Schema = Schema
624638 , _schemaMaxProperties :: Maybe Integer
625639 , _schemaMinProperties :: Maybe Integer
626640
627− , _schemaParamSchema :: ParamSchema
641+ , -- | Declares the value of the parameter that the server will use if none is provided,
642+ -- for example a @"count"@ to control the number of results per page might default to @100@
643+ -- if not supplied by the client in the request.
644+ -- (Note: "default" has no meaning for required parameters.)
645+ -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
646+ _schemaDefault :: Maybe Value
647+
648+ , _schemaType :: Maybe OpenApiType
649+ , _schemaFormat :: Maybe Format
650+ , _schemaItems :: Maybe OpenApiItems
651+ , _schemaMaximum :: Maybe Scientific
652+ , _schemaExclusiveMaximum :: Maybe Bool
653+ , _schemaMinimum :: Maybe Scientific
654+ , _schemaExclusiveMinimum :: Maybe Bool
655+ , _schemaMaxLength :: Maybe Integer
656+ , _schemaMinLength :: Maybe Integer
657+ , _schemaPattern :: Maybe Pattern
658+ , _schemaMaxItems :: Maybe Integer
659+ , _schemaMinItems :: Maybe Integer
660+ , _schemaUniqueItems :: Maybe Bool
661+ , _schemaEnum :: Maybe [Value]
662+ , _schemaMultipleOf :: Maybe Scientific
628663 } deriving (Eq, Show, Generic, Data, Typeable)
629664
665+-- | Regex pattern for @string@ type.
666+type Pattern = Text
667+
630668data Discriminator = Discriminator
631669 { -- | The name of the property in the payload that will hold the discriminator value.
632670 _discriminatorPropertyName :: Text
@@ -642,37 +680,9 @@ data NamedSchema = NamedSchema
642680 , _namedSchemaSchema :: Schema
643681 } deriving (Eq, Show, Generic, Data, Typeable)
644682
645−-- | Regex pattern for @string@ type.
646−type Pattern = Text
647−
648−data ParamSchema = ParamSchema
649− { -- | Declares the value of the parameter that the server will use if none is provided,
650− -- for example a @"count"@ to control the number of results per page might default to @100@
651− -- if not supplied by the client in the request.
652− -- (Note: "default" has no meaning for required parameters.)
653− -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
654− _paramSchemaDefault :: Maybe Value
655−
656− , _paramSchemaType :: Maybe SwaggerType
657− , _paramSchemaFormat :: Maybe Format
658− , _paramSchemaItems :: Maybe SwaggerItems
659− , _paramSchemaMaximum :: Maybe Scientific
660− , _paramSchemaExclusiveMaximum :: Maybe Bool
661− , _paramSchemaMinimum :: Maybe Scientific
662− , _paramSchemaExclusiveMinimum :: Maybe Bool
663− , _paramSchemaMaxLength :: Maybe Integer
664− , _paramSchemaMinLength :: Maybe Integer
665− , _paramSchemaPattern :: Maybe Pattern
666− , _paramSchemaMaxItems :: Maybe Integer
667− , _paramSchemaMinItems :: Maybe Integer
668− , _paramSchemaUniqueItems :: Maybe Bool
669− , _paramSchemaEnum :: Maybe [Value]
670− , _paramSchemaMultipleOf :: Maybe Scientific
671− } deriving (Eq, Show, Generic, Typeable, Data)
672−
673683data Xml = Xml
674684 { -- | Replaces the name of the element/attribute used for the described schema property.
675− -- When defined within the @'SwaggerItems'@ (items), it will affect the name of the individual XML elements within the list.
685+ -- When defined within the @'OpenApiItems'@ (items), it will affect the name of the individual XML elements within the list.
676686 -- When defined alongside type being array (outside the items),
677687 -- it will affect the wrapping element and only if wrapped is true.
678688 -- If wrapped is false, it will be ignored.
@@ -724,7 +734,7 @@ data Response = Response
724734 -- | A map containing descriptions of potential response payloads.
725735 -- The key is a media type or media type range and the value describes it.
726736 -- For responses that match multiple keys, only the most specific key is applicable.
727− -- e.g. @text/plain@ overrides @text/*@.
737+ -- e.g. @text/plain@ overrides @text/\*@.
728738 , _responseContent :: InsOrdHashMap MediaType MediaTypeObject
729739
730740 -- | Maps a header name to its definition.
@@ -749,11 +759,20 @@ newtype Callback = Callback (InsOrdHashMap Text PathItem)
749759
750760type HeaderName = Text
751761
752−-- TODO this is mostly a copy of 'Param'.
762+-- | Header fields have the same meaning as for 'Param'.
763+--
764+-- Style is always treated as 'StyleSimple', as it is the only value allowed for headers.
753765data Header = Header
754766 { -- | A short description of the header.
755767 _headerDescription :: Maybe HeaderName
756768
769+ , _headerRequired :: Maybe Bool
770+ , _headerDeprecated :: Maybe Bool
771+ , _headerAllowEmptyValue :: Maybe Bool
772+ , _headerExplode :: Maybe Bool
773+ , _headerExample :: Maybe Value
774+ , _headerExamples :: InsOrdHashMap Text (Referenced Example)
775+
757776 , _headerSchema :: Maybe (Referenced Schema)
758777 } deriving (Eq, Show, Generic, Data, Typeable)
759778
@@ -778,17 +797,17 @@ type AuthorizationURL = Text
778797-- | The token URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
779798type TokenURL = Text
780799
781−data OAuth2ImplicitFlow = OAuth2ImplicitFlow
782− { _oAuth2ImplicitFlowAuthorizationUrl :: AuthorizationURL
783− } deriving (Eq, Show, Generic, Data, Typeable)
800+newtype OAuth2ImplicitFlow
801+ = OAuth2ImplicitFlow {_oAuth2ImplicitFlowAuthorizationUrl :: AuthorizationURL}
802+ deriving (Eq, Show, Generic, Data, Typeable)
784803
785−data OAuth2PasswordFlow = OAuth2PasswordFlow
786− { _oAuth2PasswordFlowTokenUrl :: TokenURL
787− } deriving (Eq, Show, Generic, Data, Typeable)
804+newtype OAuth2PasswordFlow
805+ = OAuth2PasswordFlow {_oAuth2PasswordFlowTokenUrl :: TokenURL}
806+ deriving (Eq, Show, Generic, Data, Typeable)
788807
789−data OAuth2ClientCredentialsFlow = OAuth2ClientCredentialsFlow
790− { _oAuth2ClientCredentialsFlowTokenUrl :: TokenURL
791− } deriving (Eq, Show, Generic, Data, Typeable)
808+newtype OAuth2ClientCredentialsFlow
809+ = OAuth2ClientCredentialsFlow {_oAuth2ClientCredentialsFlowTokenUrl :: TokenURL}
810+ deriving (Eq, Show, Generic, Data, Typeable)
792811
793812data OAuth2AuthorizationCodeFlow = OAuth2AuthorizationCodeFlow
794813 { _oAuth2AuthorizationCodeFlowAuthorizationUrl :: AuthorizationURL
@@ -821,8 +840,50 @@ data OAuth2Flows = OAuth2Flows
821840 , _oAuth2FlowsAuthorizationCode :: Maybe (OAuth2Flow OAuth2AuthorizationCodeFlow)
822841 } deriving (Eq, Show, Generic, Data, Typeable)
823842
843+type BearerFormat = Text
844+
845+data HttpSchemeType
846+ = HttpSchemeBearer (Maybe BearerFormat)
847+ | HttpSchemeBasic
848+ | HttpSchemeCustom Text
849+ deriving (Eq, Show, Generic, Data, Typeable)
850+
851+-- |
852+--
853+-- >>> BSL.putStrLn $ encodePretty (SecuritySchemeHttp (HttpSchemeBearer Nothing))
854+-- {
855+-- "scheme": "bearer",
856+-- "type": "http"
857+-- }
858+--
859+-- >>> BSL.putStrLn $ encodePretty (SecuritySchemeHttp (HttpSchemeBearer (Just "jwt")))
860+-- {
861+-- "bearerFormat": "jwt",
862+-- "scheme": "bearer",
863+-- "type": "http"
864+-- }
865+--
866+-- >>> BSL.putStrLn $ encodePretty (SecuritySchemeHttp HttpSchemeBasic)
867+-- {
868+-- "scheme": "basic",
869+-- "type": "http"
870+-- }
871+--
872+-- >>> BSL.putStrLn $ encodePretty (SecuritySchemeHttp (HttpSchemeCustom "CANARY"))
873+-- {
874+-- "scheme": "CANARY",
875+-- "type": "http"
876+-- }
877+--
878+-- >>> BSL.putStrLn $ encodePretty (SecuritySchemeApiKey (ApiKeyParams "id" ApiKeyCookie))
879+-- {
880+-- "in": "cookie",
881+-- "name": "id",
882+-- "type": "apiKey"
883+-- }
884+--
824885data SecuritySchemeType
825− = SecuritySchemeHttp
886+ = SecuritySchemeHttp HttpSchemeType
826887 | SecuritySchemeApiKey ApiKeyParams
827888 | SecuritySchemeOAuth2 OAuth2Flows
828889 | SecuritySchemeOpenIdConnect URL
@@ -857,7 +918,7 @@ data Tag = Tag
857918 _tagName :: TagName
858919
859920 -- | A short description for the tag.
860− -- GFM syntax can be used for rich text representation.
921+ -- [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation.
861922 , _tagDescription :: Maybe Text
862923
863924 -- | Additional external documentation for this tag.
@@ -872,7 +933,7 @@ instance IsString Tag where
872933-- | Allows referencing an external resource for extended documentation.
873934data ExternalDocs = ExternalDocs
874935 { -- | A short description of the target documentation.
875− -- GFM syntax can be used for rich text representation.
936+ -- [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation.
876937 _externalDocsDescription :: Maybe Text
877938
878939 -- | The URL for the target documentation.
@@ -919,8 +980,7 @@ deriveGeneric ''MediaTypeObject
919980deriveGeneric ''Responses
920981deriveGeneric ''SecurityScheme
921982deriveGeneric ''Schema
922−deriveGeneric ''ParamSchema
923−deriveGeneric ''Swagger
983+deriveGeneric ''OpenApi
924984deriveGeneric ''Example
925985deriveGeneric ''Encoding
926986deriveGeneric ''Link
@@ -929,9 +989,9 @@ deriveGeneric ''Link
929989-- Monoid instances
930990-- =======================================================================
931991
932−instance Semigroup Swagger where
992+instance Semigroup OpenApi where
933993 (<>) = genericMappend
934−instance Monoid Swagger where
994+instance Monoid OpenApi where
935995 mempty = genericMempty
936996 mappend = (<>)
937997
@@ -965,12 +1025,6 @@ instance Monoid Schema where
9651025 mempty = genericMempty
9661026 mappend = (<>)
9671027
968−instance Semigroup ParamSchema where
969− (<>) = genericMappend
970−instance Monoid ParamSchema where
971− mempty = genericMempty
972− mappend = (<>)
973−
9741028instance Semigroup Param where
9751029 (<>) = genericMappend
9761030instance Monoid Param where
@@ -1065,19 +1119,19 @@ instance SwaggerMonoid Info
10651119instance SwaggerMonoid Components
10661120instance SwaggerMonoid PathItem
10671121instance SwaggerMonoid Schema
1068−instance SwaggerMonoid ParamSchema
10691122instance SwaggerMonoid Param
10701123instance SwaggerMonoid Responses
10711124instance SwaggerMonoid Response
10721125instance SwaggerMonoid ExternalDocs
10731126instance SwaggerMonoid Operation
10741127instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)
1128+instance SwaggerMonoid SecurityDefinitions
10751129
10761130instance SwaggerMonoid MimeList
10771131deriving instance SwaggerMonoid URL
10781132
1079−instance SwaggerMonoid SwaggerType where
1080− swaggerMempty = SwaggerString
1133+instance SwaggerMonoid OpenApiType where
1134+ swaggerMempty = OpenApiString
10811135 swaggerMappend _ y = y
10821136
10831137instance SwaggerMonoid ParamLocation where
@@ -1100,7 +1154,7 @@ instance Monoid a => SwaggerMonoid (Referenced a) where
11001154instance ToJSON Style where
11011155 toJSON = genericToJSON (jsonPrefix "Style")
11021156
1103−instance ToJSON SwaggerType where
1157+instance ToJSON OpenApiType where
11041158 toJSON = genericToJSON (jsonPrefix "Swagger")
11051159
11061160instance ToJSON ParamLocation where
@@ -1155,7 +1209,7 @@ instance ToJSON OAuth2AuthorizationCodeFlow where
11551209instance FromJSON Style where
11561210 parseJSON = genericParseJSON (jsonPrefix "Style")
11571211
1158−instance FromJSON SwaggerType where
1212+instance FromJSON OpenApiType where
11591213 parseJSON = genericParseJSON (jsonPrefix "Swagger")
11601214
11611215instance FromJSON ParamLocation where
@@ -1220,8 +1274,19 @@ instance ToJSON OAuth2Flows where
12201274 toEncoding = sopSwaggerGenericToEncoding
12211275
12221276instance ToJSON SecuritySchemeType where
1223− toJSON SecuritySchemeHttp
1224− = object [ "type" .= ("http" :: Text) ]
1277+ toJSON (SecuritySchemeHttp ty) = case ty of
1278+ HttpSchemeBearer mFmt ->
1279+ object $ [ "type" .= ("http" :: Text)
1280+ , "scheme" .= ("bearer" :: Text)
1281+ ] <> maybe [] (\t -> ["bearerFormat" .= t]) mFmt
1282+ HttpSchemeBasic ->
1283+ object [ "type" .= ("http" :: Text)
1284+ , "scheme" .= ("basic" :: Text)
1285+ ]
1286+ HttpSchemeCustom t ->
1287+ object [ "type" .= ("http" :: Text)
1288+ , "scheme" .= t
1289+ ]
12251290 toJSON (SecuritySchemeApiKey params)
12261291 = toJSON params
12271292 <+> object [ "type" .= ("apiKey" :: Text) ]
@@ -1234,9 +1299,9 @@ instance ToJSON SecuritySchemeType where
12341299 , "openIdConnectUrl" .= url
12351300 ]
12361301
1237−instance ToJSON Swagger where
1302+instance ToJSON OpenApi where
12381303 toJSON a = sopSwaggerGenericToJSON a &
1239− if InsOrdHashMap.null (_swaggerPaths a)
1304+ if InsOrdHashMap.null (_openApiPaths a)
12401305 then (<+> object ["paths" .= object []])
12411306 else id
12421307 toEncoding = sopSwaggerGenericToEncoding
@@ -1250,8 +1315,8 @@ instance ToJSON SecurityScheme where
12501315 toEncoding = sopSwaggerGenericToEncoding
12511316
12521317instance ToJSON Schema where
1253− toJSON = sopSwaggerGenericToJSON
1254− toEncoding = sopSwaggerGenericToEncoding
1318+ toJSON = sopSwaggerGenericToJSONWithOpts $
1319+ mkSwaggerAesonOptions "schema" & saoSubObject ?~ "items"
12551320
12561321instance ToJSON Header where
12571322 toJSON = sopSwaggerGenericToJSON
@@ -1260,17 +1325,21 @@ instance ToJSON Header where
12601325-- | As for nullary schema for 0-arity type constructors, see
12611326-- <https://github.com/GetShopTV/swagger2/issues/167>.
12621327--
1263−-- >>> encode (SwaggerItemsArray [])
1264−-- "{\"example\":[],\"items\":{},\"maxItems\":0}"
1328+-- >>> BSL.putStrLn $ encodePretty (OpenApiItemsArray [])
1329+-- {
1330+-- "example": [],
1331+-- "items": {},
1332+-- "maxItems": 0
1333+-- }
12651334--
1266−instance ToJSON SwaggerItems where
1267− toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
1268− toJSON (SwaggerItemsArray []) = object
1335+instance ToJSON OpenApiItems where
1336+ toJSON (OpenApiItemsObject x) = object [ "items" .= x ]
1337+ toJSON (OpenApiItemsArray []) = object
12691338 [ "items" .= object []
12701339 , "maxItems" .= (0 :: Int)
12711340 , "example" .= Array mempty
12721341 ]
1273− toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
1342+ toJSON (OpenApiItemsArray x) = object [ "items" .= x ]
12741343
12751344instance ToJSON Components where
12761345 toJSON = sopSwaggerGenericToJSON
@@ -1338,11 +1407,6 @@ instance ToJSON (Referenced Header) where toJSON = referencedToJSON "#/compone
13381407instance ToJSON (Referenced Link) where toJSON = referencedToJSON "#/components/links/"
13391408instance ToJSON (Referenced Callback) where toJSON = referencedToJSON "#/components/callbacks/"
13401409
1341−instance ToJSON ParamSchema where
1342− -- TODO: this is a bit fishy, why we need sub object only in `ToJSON`?
1343− toJSON = sopSwaggerGenericToJSONWithOpts $
1344− mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1345−
13461410instance ToJSON AdditionalProperties where
13471411 toJSON (AdditionalPropertiesAllowed b) = toJSON b
13481412 toJSON (AdditionalPropertiesSchema s) = toJSON s
@@ -1375,14 +1439,19 @@ instance FromJSON SecuritySchemeType where
13751439 parseJSON js@(Object o) = do
13761440 (t :: Text) <- o .: "type"
13771441 case t of
1378− "http" -> pure SecuritySchemeHttp
1442+ "http" -> do
1443+ scheme <- o .: "scheme"
1444+ SecuritySchemeHttp <$> case scheme of
1445+ "bearer" -> HttpSchemeBearer <$> (o .:! "bearerFormat")
1446+ "basic" -> pure HttpSchemeBasic
1447+ t -> pure $ HttpSchemeCustom t
13791448 "apiKey" -> SecuritySchemeApiKey <$> parseJSON js
13801449 "oauth2" -> SecuritySchemeOAuth2 <$> (o .: "flows")
13811450 "openIdConnect" -> SecuritySchemeOpenIdConnect <$> (o .: "openIdConnectUrl")
13821451 _ -> empty
13831452 parseJSON _ = empty
13841453
1385−instance FromJSON Swagger where
1454+instance FromJSON OpenApi where
13861455 parseJSON = sopSwaggerGenericParseJSON
13871456
13881457instance FromJSON Server where
@@ -1394,27 +1463,28 @@ instance FromJSON SecurityScheme where
13941463instance FromJSON Schema where
13951464 parseJSON = fmap nullaryCleanup . sopSwaggerGenericParseJSON
13961465 where nullaryCleanup :: Schema -> Schema
1397− nullaryCleanup s@Schema{_schemaParamSchema=ps} =
1398− if _paramSchemaItems ps == Just (SwaggerItemsArray [])
1466+ nullaryCleanup s =
1467+ if _schemaItems s == Just (OpenApiItemsArray [])
13991468 then s { _schemaExample = Nothing
1400− , _schemaParamSchema = ps { _paramSchemaMaxItems = Nothing } }
1469+ , _schemaMaxItems = Nothing
1470+ }
14011471 else s
14021472
14031473instance FromJSON Header where
14041474 parseJSON = sopSwaggerGenericParseJSON
14051475
1406−instance FromJSON SwaggerItems where
1476+instance FromJSON OpenApiItems where
14071477 parseJSON js@(Object obj)
1408− | null obj = pure $ SwaggerItemsArray [] -- Nullary schema.
1409− | otherwise = SwaggerItemsObject <$> parseJSON js
1410− parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
1478+ | null obj = pure $ OpenApiItemsArray [] -- Nullary schema.
1479+ | otherwise = OpenApiItemsObject <$> parseJSON js
1480+ parseJSON js@(Array _) = OpenApiItemsArray <$> parseJSON js
14111481 parseJSON _ = empty
14121482
14131483instance FromJSON Components where
14141484 parseJSON = sopSwaggerGenericParseJSON
14151485
14161486instance FromJSON MimeList where
1417− parseJSON js = (MimeList . map fromString) <$> parseJSON js
1487+ parseJSON js = MimeList . map fromString <$> parseJSON js
14181488
14191489instance FromJSON Param where
14201490 parseJSON = sopSwaggerGenericParseJSON
@@ -1422,7 +1492,7 @@ instance FromJSON Param where
14221492instance FromJSON Responses where
14231493 parseJSON (Object o) = Responses
14241494 <$> o .:? "default"
1425− <*> (parseJSON (Object (HashMap.delete "default" o)))
1495+ <*> parseJSON (Object (deleteKey "default" o))
14261496 parseJSON _ = empty
14271497
14281498instance FromJSON Example where
@@ -1481,9 +1551,6 @@ instance FromJSON (Referenced Callback) where parseJSON = referencedParseJSON "#
14811551instance FromJSON Xml where
14821552 parseJSON = genericParseJSON (jsonPrefix "xml")
14831553
1484−instance FromJSON ParamSchema where
1485− parseJSON = sopSwaggerGenericParseJSON
1486−
14871554instance FromJSON AdditionalProperties where
14881555 parseJSON (Bool b) = pure $ AdditionalPropertiesAllowed b
14891556 parseJSON js = AdditionalPropertiesSchema <$> parseJSON js
@@ -1524,7 +1591,7 @@ instance HasSwaggerAesonOptions SecurityScheme where
15241591 swaggerAesonOptions _ = mkSwaggerAesonOptions "securityScheme" & saoSubObject ?~ "type"
15251592instance HasSwaggerAesonOptions Schema where
15261593 swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject ?~ "paramSchema"
1527−instance HasSwaggerAesonOptions Swagger where
1594+instance HasSwaggerAesonOptions OpenApi where
15281595 swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.0")]
15291596instance HasSwaggerAesonOptions Example where
15301597 swaggerAesonOptions _ = mkSwaggerAesonOptions "example"
@@ -1534,12 +1601,8 @@ instance HasSwaggerAesonOptions Encoding where
15341601instance HasSwaggerAesonOptions Link where
15351602 swaggerAesonOptions _ = mkSwaggerAesonOptions "link"
15361603
1537−instance HasSwaggerAesonOptions ParamSchema where
1538− swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema"
1539−
15401604instance AesonDefaultValue Server
15411605instance AesonDefaultValue Components
1542−instance AesonDefaultValue ParamSchema
15431606instance AesonDefaultValue OAuth2ImplicitFlow
15441607instance AesonDefaultValue OAuth2PasswordFlow
15451608instance AesonDefaultValue OAuth2ClientCredentialsFlow
@@ -1547,8 +1610,10 @@ instance AesonDefaultValue OAuth2AuthorizationCodeFlow
15471610instance AesonDefaultValue p => AesonDefaultValue (OAuth2Flow p)
15481611instance AesonDefaultValue Responses
15491612instance AesonDefaultValue SecuritySchemeType
1550−instance AesonDefaultValue SwaggerType
1613+instance AesonDefaultValue OpenApiType
15511614instance AesonDefaultValue MimeList where defaultValue = Just mempty
15521615instance AesonDefaultValue Info
15531616instance AesonDefaultValue ParamLocation
15541617instance AesonDefaultValue Link
1618+instance AesonDefaultValue SecurityDefinitions where
1619+ defaultValue = Just mempty