@@ -1,6 +1,6 @@
1−{-# LANGUAGE CPP #-}
21{-# LANGUAGE DataKinds #-}
32{-# LANGUAGE DeriveDataTypeable #-}
3+{-# LANGUAGE DeriveFunctor #-}
44{-# LANGUAGE DeriveGeneric #-}
55{-# LANGUAGE DeriveTraversable #-}
66{-# LANGUAGE FlexibleContexts #-}
@@ -14,91 +14,87 @@
1414{-# LANGUAGE TemplateHaskell #-}
1515{-# LANGUAGE TypeFamilies #-}
1616{-# LANGUAGE UndecidableInstances #-}
17−module Data.OpenApi.Internal where
17+module Data.Swagger.Internal where
1818
1919import Prelude ()
2020import Prelude.Compat
2121
22+import Control.Lens ((&), (.~), (?~))
2223import Control.Applicative
23−import Control.Lens ((&), (.~), (?~))
24−import Data.Aeson hiding (Encoding)
25−#if MIN_VERSION_aeson(2,0,0)
26−import qualified Data.Aeson.KeyMap as KeyMap
27−#endif
28−import qualified Data.Aeson.Types as JSON
29−import Data.Data (Constr, Data (..), DataType, Fixity (..), Typeable,
30− constrIndex, mkConstr, mkDataType)
31−import Data.Hashable (Hashable (..))
32−import qualified Data.HashMap.Strict as HashMap
33−import Data.HashSet.InsOrd (InsOrdHashSet)
34−import Data.Map (Map)
35−import qualified Data.Map as Map
36−import Data.Monoid (Monoid (..))
37−import Data.Scientific (Scientific)
38−import Data.Semigroup.Compat (Semigroup (..))
39−import Data.String (IsString (..))
40−import Data.Text (Text)
41−import qualified Data.Text as Text
42−import Data.Text.Encoding (encodeUtf8)
43−import GHC.Generics (Generic)
44−import Network.HTTP.Media (MediaType, mainType, parameters, parseAccept, subType, (//),
45− (/:))
46−import Network.Socket (HostName, PortNumber)
47−import Text.Read (readMaybe)
24+import Data.Aeson hiding (Encoding)
25+import qualified Data.Aeson.Types as JSON
26+import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
27+import Data.Hashable (Hashable)
28+import qualified Data.HashMap.Strict as HashMap
29+import Data.HashSet.InsOrd (InsOrdHashSet)
30+import Data.Map (Map)
31+import qualified Data.Map as Map
32+import Data.Monoid (Monoid (..))
33+import Data.Semigroup.Compat (Semigroup (..))
34+import Data.Scientific (Scientific)
35+import Data.String (IsString(..))
36+import Data.Text (Text)
37+import qualified Data.Text as Text
38+import GHC.Generics (Generic)
39+import Network.Socket (HostName, PortNumber)
40+import Network.HTTP.Media (MediaType)
41+import Text.Read (readMaybe)
4842
4943import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
5044import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
5145
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
5846import Generics.SOP.TH (deriveGeneric)
47+import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
48+ ,sopSwaggerGenericToJSONWithOpts
49+ ,sopSwaggerGenericParseJSON
50+ ,HasSwaggerAesonOptions(..)
51+ ,AesonDefaultValue(..)
52+ ,mkSwaggerAesonOptions
53+ ,saoAdditionalPairs
54+ ,saoSubObject)
55+import Data.Swagger.Internal.Utils
56+import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToEncoding)
5957
6058-- $setup
6159-- >>> :seti -XDataKinds
6260-- >>> import Data.Aeson
63−-- >>> import Data.ByteString.Lazy.Char8 as BSL
64−-- >>> import Data.OpenApi.Internal.Utils
6561
6662-- | A list of definitions that can be used in references.
6763type Definitions = InsOrdHashMap Text
6864
6965-- | This is the root document object for the API specification.
70−data OpenApi = OpenApi
66+data Swagger = Swagger
7167 { -- | Provides metadata about the API.
7268 -- The metadata can be used by the clients if needed.
73− _openApiInfo :: Info
69+ _swaggerInfo :: Info
7470
7571 -- | An array of Server Objects, which provide connectivity information
7672 -- to a target server. If the servers property is not provided, or is an empty array,
7773 -- the default value would be a 'Server' object with a url value of @/@.
78− , _openApiServers :: [Server]
74+ , _swaggerServers :: [Server]
7975
8076 -- | The available paths and operations for the API.
81− , _openApiPaths :: InsOrdHashMap FilePath PathItem
77+ , _swaggerPaths :: InsOrdHashMap FilePath PathItem
8278
8379 -- | An element to hold various schemas for the specification.
84− , _openApiComponents :: Components
80+ , _swaggerComponents :: Components
8581
8682 -- | A declaration of which security mechanisms can be used across the API.
8783 -- The list of values includes alternative security requirement objects that can be used.
8884 -- Only one of the security requirement objects need to be satisfied to authorize a request.
8985 -- Individual operations can override this definition.
9086 -- To make security optional, an empty security requirement can be included in the array.
91− , _openApiSecurity :: [SecurityRequirement]
87+ , _swaggerSecurity :: [SecurityRequirement]
9288
9389 -- | A list of tags used by the specification with additional metadata.
9490 -- The order of the tags can be used to reflect on their order by the parsing tools.
9591 -- Not all tags that are used by the 'Operation' Object must be declared.
9692 -- The tags that are not declared MAY be organized randomly or based on the tools' logic.
9793 -- Each tag name in the list MUST be unique.
98− , _openApiTags :: InsOrdHashSet Tag
94+ , _swaggerTags :: InsOrdHashSet Tag
9995
10096 -- | Additional external documentation.
101− , _openApiExternalDocs :: Maybe ExternalDocs
97+ , _swaggerExternalDocs :: Maybe ExternalDocs
10298 } deriving (Eq, Show, Generic, Data, Typeable)
10399
104100-- | The object provides metadata about the API.
@@ -195,10 +191,10 @@ data Components = Components
195191 , _componentsParameters :: Definitions Param
196192 , _componentsExamples :: Definitions Example
197193 , _componentsRequestBodies :: Definitions RequestBody
198− , _componentsHeaders :: Definitions Header
199− , _componentsSecuritySchemes :: SecurityDefinitions
200− , _componentsLinks :: Definitions Link
201− , _componentsCallbacks :: Definitions Callback
194+ , _componentsHeader :: Definitions Header
195+ , _componentsSecuritySchemes :: Definitions SecurityScheme
196+-- , _componentsLinks
197+-- , _componentsCallbacks
202198 } deriving (Eq, Show, Generic, Data, Typeable)
203199
204200-- | Describes the operations available on a single path.
@@ -288,11 +284,7 @@ data Operation = Operation
288284 -- | The list of possible responses as they are returned from executing this operation.
289285 , _operationResponses :: Responses
290286
291− -- | A map of possible out-of band callbacks related to the parent operation.
292− -- The key is a unique identifier for the 'Callback' Object.
293− -- Each value in the map is a 'Callback' Object that describes a request
294− -- that may be initiated by the API provider and the expected responses.
295− , _operationCallbacks :: InsOrdHashMap Text (Referenced Callback)
287+ -- TODO callbacks
296288
297289 -- | Declares this operation to be deprecated.
298290 -- Usage of the declared operation should be refrained.
@@ -312,22 +304,6 @@ data Operation = Operation
312304 , _operationServers :: [Server]
313305 } deriving (Eq, Show, Generic, Data, Typeable)
314306
315−-- This instance should be in @http-media@.
316−instance Data MediaType where
317− gunfold k z c = case constrIndex c of
318− 1 -> k (k (k (z (\main sub params -> foldl (/:) (main // sub) (Map.toList params)))))
319− _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type MediaType."
320−
321− toConstr _ = mediaTypeConstr
322−
323− dataTypeOf _ = mediaTypeData
324−
325−mediaTypeConstr = mkConstr mediaTypeData "MediaType" [] Prefix
326−mediaTypeData = mkDataType "MediaType" [mediaTypeConstr]
327−
328−instance Hashable MediaType where
329− hashWithSalt salt mt = salt `hashWithSalt` show mt
330−
331307-- | Describes a single request body.
332308data RequestBody = RequestBody
333309 { -- | A brief description of the request body. This could contain examples of use.
@@ -337,11 +313,9 @@ data RequestBody = RequestBody
337313 -- | The content of the request body.
338314 -- The key is a media type or media type range and the value describes it.
339315 -- For requests that match multiple keys, only the most specific key is applicable.
340− -- e.g. @text/plain@ overrides @text/\*@
341− , _requestBodyContent :: InsOrdHashMap MediaType MediaTypeObject
316+ -- e.g. @text/plain@ overrides @text/*@
317+ , _requestBodyContent :: InsOrdHashMap {-MediaType-} Text MediaTypeObject -- FIXME Data MediaType
342318
343− -- | Determines if the request body is required in the request.
344− -- Defaults to 'False'.
345319 , _requestBodyRequired :: Maybe Bool
346320 } deriving (Eq, Show, Generic, Data, Typeable)
347321
@@ -394,8 +368,8 @@ data Encoding = Encoding
394368 -- for other primitive types – @text/plain@; for object - @application/json@;
395369 -- for array – the default is defined based on the inner type.
396370 -- The value can be a specific media type (e.g. @application/json@),
397− -- a wildcard media type (e.g. @image/\*@), or a comma-separated list of the two types.
398− _encodingContentType :: Maybe MediaType
371+ -- a wildcard media type (e.g. @image/*@), or a comma-separated list of the two types.
372+ _encodingContentType :: Maybe Text
399373
400374 -- | A map allowing additional information to be provided as headers,
401375 -- for example @Content-Disposition@. @Content-Type@ is described separately
@@ -434,15 +408,18 @@ mimeListConstr :: Constr
434408mimeListConstr = mkConstr mimeListDataType "MimeList" ["getMimeList"] Prefix
435409
436410mimeListDataType :: DataType
437−mimeListDataType = mkDataType "Data.OpenApi.MimeList" [mimeListConstr]
411+mimeListDataType = mkDataType "Data.Swagger.MimeList" [mimeListConstr]
438412
439413instance Data MimeList where
440414 gunfold k z c = case constrIndex c of
441− 1 -> k (z (MimeList . map fromString))
415+ 1 -> k (z (\xs -> MimeList (map fromString xs)))
442416 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type MimeList."
443417 toConstr (MimeList _) = mimeListConstr
444418 dataTypeOf _ = mimeListDataType
445419
420+-- TODO style
421+-- TODO example
422+
446423-- | Describes a single operation parameter.
447424-- A unique parameter is defined by a combination of a name and location.
448425data Param = Param
@@ -452,7 +429,7 @@ data Param = Param
452429
453430 -- | A brief description of the parameter.
454431 -- This could contain examples of use.
455− -- CommonMark syntax MAY be used for rich text representation.
432+ -- GFM syntax can be used for rich text representation.
456433 , _paramDescription :: Maybe Text
457434
458435 -- | Determines whether this parameter is mandatory.
@@ -472,13 +449,6 @@ data Param = Param
472449 -- a parameter with an empty value. Default value is @false@.
473450 , _paramAllowEmptyValue :: Maybe Bool
474451
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−
482452 -- | Parameter schema.
483453 , _paramSchema :: Maybe (Referenced Schema)
484454
@@ -509,10 +479,6 @@ data Param = Param
509479 -- Furthermore, if referencing a schema that contains an example,
510480 -- the examples value SHALL override the example provided by the schema.
511481 , _paramExamples :: InsOrdHashMap Text (Referenced Example)
512−
513− -- TODO
514− -- _paramContent :: InsOrdHashMap MediaType MediaTypeObject
515− -- should be singleton. mutually exclusive with _paramSchema.
516482 } deriving (Eq, Show, Generic, Data, Typeable)
517483
518484data Example = Example
@@ -537,63 +503,27 @@ data Example = Example
537503 , _exampleExternalValue :: Maybe URL
538504 } deriving (Eq, Show, Generic, Typeable, Data)
539505
540−data ExpressionOrValue
541− = Expression Text
542− | Value Value
543− deriving (Eq, Show, Generic, Typeable, Data)
544−
545−-- | The Link object represents a possible design-time link for a response.
546−-- The presence of a link does not guarantee the caller's ability to successfully invoke it,
547−-- rather it provides a known relationship and traversal mechanism between responses and other operations.
548−data Link = Link
549− { -- | A relative or absolute URI reference to an OAS operation.
550− -- This field is mutually exclusive of the '_linkOperationId' field,
551− -- and MUST point to an 'Operation' Object. Relative '_linkOperationRef'
552− -- values MAY be used to locate an existing 'Operation' Object in the OpenAPI definition.
553− _linkOperationRef :: Maybe Text
554−
555− -- | The name of an /existing/, resolvable OAS operation, as defined with a unique
556− -- '_operationOperationId'. This field is mutually exclusive of the '_linkOperationRef' field.
557− , _linkOperationId :: Maybe Text
558−
559− -- | A map representing parameters to pass to an operation as specified with '_linkOperationId'
560− -- or identified via '_linkOperationRef'. The key is the parameter name to be used, whereas
561− -- the value can be a constant or an expression to be evaluated and passed to the linked operation.
562− -- The parameter name can be qualified using the parameter location @[{in}.]{name}@
563− -- for operations that use the same parameter name in different locations (e.g. path.id).
564− , _linkParameters :: InsOrdHashMap Text ExpressionOrValue
565−
566− -- | A literal value or @{expression}@ to use as a request body when calling the target operation.
567− , _linkRequestBody :: Maybe ExpressionOrValue
568−
569− -- | A description of the link.
570− , _linkDescription :: Maybe Text
571−
572− -- | A server object to be used by the target operation.
573− , _linkServer :: Maybe Server
574− } deriving (Eq, Show, Generic, Typeable, Data)
575−
576−-- | Items for @'OpenApiArray'@ schemas.
506+-- | Items for @'SwaggerArray'@ schemas.
577507--
578508-- __Warning__: OpenAPI 3.0 does not support tuple arrays. However, OpenAPI 3.1 will, as
579509-- it will incorporate Json Schema mostly verbatim.
580510--
581−-- @'OpenApiItemsObject'@ should be used to specify homogenous array @'Schema'@s.
511+-- @'SwaggerItemsObject'@ should be used to specify homogenous array @'Schema'@s.
582512--
583−-- @'OpenApiItemsArray'@ should be used to specify tuple @'Schema'@s.
584−data OpenApiItems where
585− OpenApiItemsObject :: Referenced Schema -> OpenApiItems
586− OpenApiItemsArray :: [Referenced Schema] -> OpenApiItems
513+-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
514+data SwaggerItems where
515+ SwaggerItemsObject :: Referenced Schema -> SwaggerItems
516+ SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems
587517 deriving (Eq, Show, Typeable, Data)
588518
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
519+data SwaggerType where
520+ SwaggerString :: SwaggerType
521+ SwaggerNumber :: SwaggerType
522+ SwaggerInteger :: SwaggerType
523+ SwaggerBoolean :: SwaggerType
524+ SwaggerArray :: SwaggerType
525+ SwaggerNull :: SwaggerType
526+ SwaggerObject :: SwaggerType
597527 deriving (Eq, Show, Typeable, Generic, Data)
598528
599529data ParamLocation
@@ -638,33 +568,9 @@ data Schema = Schema
638568 , _schemaMaxProperties :: Maybe Integer
639569 , _schemaMinProperties :: Maybe Integer
640570
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
571+ , _schemaParamSchema :: ParamSchema
663572 } deriving (Eq, Show, Generic, Data, Typeable)
664573
665−-- | Regex pattern for @string@ type.
666−type Pattern = Text
667−
668574data Discriminator = Discriminator
669575 { -- | The name of the property in the payload that will hold the discriminator value.
670576 _discriminatorPropertyName :: Text
@@ -680,9 +586,37 @@ data NamedSchema = NamedSchema
680586 , _namedSchemaSchema :: Schema
681587 } deriving (Eq, Show, Generic, Data, Typeable)
682588
589+-- | Regex pattern for @string@ type.
590+type Pattern = Text
591+
592+data ParamSchema = ParamSchema
593+ { -- | Declares the value of the parameter that the server will use if none is provided,
594+ -- for example a @"count"@ to control the number of results per page might default to @100@
595+ -- if not supplied by the client in the request.
596+ -- (Note: "default" has no meaning for required parameters.)
597+ -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
598+ _paramSchemaDefault :: Maybe Value
599+
600+ , _paramSchemaType :: Maybe SwaggerType
601+ , _paramSchemaFormat :: Maybe Format
602+ , _paramSchemaItems :: Maybe SwaggerItems
603+ , _paramSchemaMaximum :: Maybe Scientific
604+ , _paramSchemaExclusiveMaximum :: Maybe Bool
605+ , _paramSchemaMinimum :: Maybe Scientific
606+ , _paramSchemaExclusiveMinimum :: Maybe Bool
607+ , _paramSchemaMaxLength :: Maybe Integer
608+ , _paramSchemaMinLength :: Maybe Integer
609+ , _paramSchemaPattern :: Maybe Pattern
610+ , _paramSchemaMaxItems :: Maybe Integer
611+ , _paramSchemaMinItems :: Maybe Integer
612+ , _paramSchemaUniqueItems :: Maybe Bool
613+ , _paramSchemaEnum :: Maybe [Value]
614+ , _paramSchemaMultipleOf :: Maybe Scientific
615+ } deriving (Eq, Show, Generic, Typeable, Data)
616+
683617data Xml = Xml
684618 { -- | Replaces the name of the element/attribute used for the described schema property.
685− -- When defined within the @'OpenApiItems'@ (items), it will affect the name of the individual XML elements within the list.
619+ -- When defined within the @'SwaggerItems'@ (items), it will affect the name of the individual XML elements within the list.
686620 -- When defined alongside type being array (outside the items),
687621 -- it will affect the wrapping element and only if wrapped is true.
688622 -- If wrapped is false, it will be ignored.
@@ -734,44 +668,25 @@ data Response = Response
734668 -- | A map containing descriptions of potential response payloads.
735669 -- The key is a media type or media type range and the value describes it.
736670 -- For responses that match multiple keys, only the most specific key is applicable.
737− -- e.g. @text/plain@ overrides @text/\*@.
738− , _responseContent :: InsOrdHashMap MediaType MediaTypeObject
671+ -- e.g. @text/plain@ overrides @text/*@.
672+ , _responseContent :: InsOrdHashMap Text MediaTypeObject
739673
740674 -- | Maps a header name to its definition.
741675 , _responseHeaders :: InsOrdHashMap HeaderName (Referenced Header)
742676
743− -- | A map of operations links that can be followed from the response.
744− -- The key of the map is a short name for the link, following the naming
745− -- constraints of the names for 'Component' Objects.
746− , _responseLinks :: InsOrdHashMap Text (Referenced Link)
677+ -- TODO links
747678 } deriving (Eq, Show, Generic, Data, Typeable)
748679
749680instance IsString Response where
750− fromString s = Response (fromString s) mempty mempty mempty
751−
752−-- | A map of possible out-of band callbacks related to the parent operation.
753−-- Each value in the map is a 'PathItem' Object that describes a set of requests that
754−-- may be initiated by the API provider and the expected responses.
755−-- The key value used to identify the path item object is an expression, evaluated at runtime,
756−-- that identifies a URL to use for the callback operation.
757−newtype Callback = Callback (InsOrdHashMap Text PathItem)
758− deriving (Eq, Show, Generic, Data, Typeable)
681+ fromString s = Response (fromString s) mempty mempty
759682
760683type HeaderName = Text
761684
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.
685+
686+-- TODO this is mostly a copy of 'Param'.
765687data Header = Header
766688 { -- | A short description of the header.
767− _headerDescription :: Maybe HeaderName
768−
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)
689+ _headerDescription :: Maybe Text
775690
776691 , _headerSchema :: Maybe (Referenced Schema)
777692 } deriving (Eq, Show, Generic, Data, Typeable)
@@ -797,17 +712,17 @@ type AuthorizationURL = Text
797712-- | The token URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
798713type TokenURL = Text
799714
800−newtype OAuth2ImplicitFlow
801− = OAuth2ImplicitFlow {_oAuth2ImplicitFlowAuthorizationUrl :: AuthorizationURL}
802− deriving (Eq, Show, Generic, Data, Typeable)
715+data OAuth2ImplicitFlow = OAuth2ImplicitFlow
716+ { _oAuth2ImplicitFlowAuthorizationUrl :: AuthorizationURL
717+ } deriving (Eq, Show, Generic, Data, Typeable)
803718
804−newtype OAuth2PasswordFlow
805− = OAuth2PasswordFlow {_oAuth2PasswordFlowTokenUrl :: TokenURL}
806− deriving (Eq, Show, Generic, Data, Typeable)
719+data OAuth2PasswordFlow = OAuth2PasswordFlow
720+ { _oAuth2PasswordFlowTokenUrl :: TokenURL
721+ } deriving (Eq, Show, Generic, Data, Typeable)
807722
808−newtype OAuth2ClientCredentialsFlow
809− = OAuth2ClientCredentialsFlow {_oAuth2ClientCredentialsFlowTokenUrl :: TokenURL}
810− deriving (Eq, Show, Generic, Data, Typeable)
723+data OAuth2ClientCredentialsFlow = OAuth2ClientCredentialsFlow
724+ { _oAuth2ClientCredentialsFlowTokenUrl :: TokenURL
725+ } deriving (Eq, Show, Generic, Data, Typeable)
811726
812727data OAuth2AuthorizationCodeFlow = OAuth2AuthorizationCodeFlow
813728 { _oAuth2AuthorizationCodeFlowAuthorizationUrl :: AuthorizationURL
@@ -840,50 +755,8 @@ data OAuth2Flows = OAuth2Flows
840755 , _oAuth2FlowsAuthorizationCode :: Maybe (OAuth2Flow OAuth2AuthorizationCodeFlow)
841756 } deriving (Eq, Show, Generic, Data, Typeable)
842757
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−--
885758data SecuritySchemeType
886− = SecuritySchemeHttp HttpSchemeType
759+ = SecuritySchemeHttp
887760 | SecuritySchemeApiKey ApiKeyParams
888761 | SecuritySchemeOAuth2 OAuth2Flows
889762 | SecuritySchemeOpenIdConnect URL
@@ -918,7 +791,7 @@ data Tag = Tag
918791 _tagName :: TagName
919792
920793 -- | A short description for the tag.
921− -- [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation.
794+ -- GFM syntax can be used for rich text representation.
922795 , _tagDescription :: Maybe Text
923796
924797 -- | Additional external documentation for this tag.
@@ -933,7 +806,7 @@ instance IsString Tag where
933806-- | Allows referencing an external resource for extended documentation.
934807data ExternalDocs = ExternalDocs
935808 { -- | A short description of the target documentation.
936− -- [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation.
809+ -- GFM syntax can be used for rich text representation.
937810 _externalDocsDescription :: Maybe Text
938811
939812 -- | The URL for the target documentation.
@@ -980,18 +853,18 @@ deriveGeneric ''MediaTypeObject
980853deriveGeneric ''Responses
981854deriveGeneric ''SecurityScheme
982855deriveGeneric ''Schema
983−deriveGeneric ''OpenApi
856+deriveGeneric ''ParamSchema
857+deriveGeneric ''Swagger
984858deriveGeneric ''Example
985859deriveGeneric ''Encoding
986−deriveGeneric ''Link
987860
988861-- =======================================================================
989862-- Monoid instances
990863-- =======================================================================
991864
992−instance Semigroup OpenApi where
865+instance Semigroup Swagger where
993866 (<>) = genericMappend
994−instance Monoid OpenApi where
867+instance Monoid Swagger where
995868 mempty = genericMempty
996869 mappend = (<>)
997870
@@ -1025,6 +898,12 @@ instance Monoid Schema where
1025898 mempty = genericMempty
1026899 mappend = (<>)
1027900
901+instance Semigroup ParamSchema where
902+ (<>) = genericMappend
903+instance Monoid ParamSchema where
904+ mempty = genericMempty
905+ mappend = (<>)
906+
1028907instance Semigroup Param where
1029908 (<>) = genericMappend
1030909instance Monoid Param where
@@ -1119,19 +998,19 @@ instance SwaggerMonoid Info
1119998instance SwaggerMonoid Components
1120999instance SwaggerMonoid PathItem
11211000instance SwaggerMonoid Schema
1001+instance SwaggerMonoid ParamSchema
11221002instance SwaggerMonoid Param
11231003instance SwaggerMonoid Responses
11241004instance SwaggerMonoid Response
11251005instance SwaggerMonoid ExternalDocs
11261006instance SwaggerMonoid Operation
11271007instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)
1128−instance SwaggerMonoid SecurityDefinitions
11291008
11301009instance SwaggerMonoid MimeList
11311010deriving instance SwaggerMonoid URL
11321011
1133−instance SwaggerMonoid OpenApiType where
1134− swaggerMempty = OpenApiString
1012+instance SwaggerMonoid SwaggerType where
1013+ swaggerMempty = SwaggerString
11351014 swaggerMappend _ y = y
11361015
11371016instance SwaggerMonoid ParamLocation where
@@ -1154,7 +1033,7 @@ instance Monoid a => SwaggerMonoid (Referenced a) where
11541033instance ToJSON Style where
11551034 toJSON = genericToJSON (jsonPrefix "Style")
11561035
1157−instance ToJSON OpenApiType where
1036+instance ToJSON SwaggerType where
11581037 toJSON = genericToJSON (jsonPrefix "Swagger")
11591038
11601039instance ToJSON ParamLocation where
@@ -1209,7 +1088,7 @@ instance ToJSON OAuth2AuthorizationCodeFlow where
12091088instance FromJSON Style where
12101089 parseJSON = genericParseJSON (jsonPrefix "Style")
12111090
1212−instance FromJSON OpenApiType where
1091+instance FromJSON SwaggerType where
12131092 parseJSON = genericParseJSON (jsonPrefix "Swagger")
12141093
12151094instance FromJSON ParamLocation where
@@ -1258,14 +1137,7 @@ instance FromJSON OAuth2AuthorizationCodeFlow where
12581137-- Manual ToJSON instances
12591138-- =======================================================================
12601139
1261−instance ToJSON MediaType where
1262− toJSON = toJSON . show
1263− toEncoding = toEncoding . show
1264−
1265−instance ToJSONKey MediaType where
1266− toJSONKey = JSON.toJSONKeyText (Text.pack . show)
1267−
1268−instance (Eq p, ToJSON p, AesonDefaultValue p) => ToJSON (OAuth2Flow p) where
1140+instance (Eq p, ToJSON p, AesonDefaultValue p) =>ToJSON (OAuth2Flow p) where
12691141 toJSON = sopSwaggerGenericToJSON
12701142 toEncoding = sopSwaggerGenericToEncoding
12711143
@@ -1274,19 +1146,8 @@ instance ToJSON OAuth2Flows where
12741146 toEncoding = sopSwaggerGenericToEncoding
12751147
12761148instance ToJSON SecuritySchemeType where
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− ]
1149+ toJSON SecuritySchemeHttp
1150+ = object [ "type" .= ("http" :: Text) ]
12901151 toJSON (SecuritySchemeApiKey params)
12911152 = toJSON params
12921153 <+> object [ "type" .= ("apiKey" :: Text) ]
@@ -1299,9 +1160,9 @@ instance ToJSON SecuritySchemeType where
12991160 , "openIdConnectUrl" .= url
13001161 ]
13011162
1302−instance ToJSON OpenApi where
1163+instance ToJSON Swagger where
13031164 toJSON a = sopSwaggerGenericToJSON a &
1304− if InsOrdHashMap.null (_openApiPaths a)
1165+ if InsOrdHashMap.null (_swaggerPaths a)
13051166 then (<+> object ["paths" .= object []])
13061167 else id
13071168 toEncoding = sopSwaggerGenericToEncoding
@@ -1315,8 +1176,8 @@ instance ToJSON SecurityScheme where
13151176 toEncoding = sopSwaggerGenericToEncoding
13161177
13171178instance ToJSON Schema where
1318− toJSON = sopSwaggerGenericToJSONWithOpts $
1319− mkSwaggerAesonOptions "schema" & saoSubObject ?~ "items"
1179+ toJSON = sopSwaggerGenericToJSON
1180+ toEncoding = sopSwaggerGenericToEncoding
13201181
13211182instance ToJSON Header where
13221183 toJSON = sopSwaggerGenericToJSON
@@ -1325,21 +1186,17 @@ instance ToJSON Header where
13251186-- | As for nullary schema for 0-arity type constructors, see
13261187-- <https://github.com/GetShopTV/swagger2/issues/167>.
13271188--
1328−-- >>> BSL.putStrLn $ encodePretty (OpenApiItemsArray [])
1329−-- {
1330−-- "example": [],
1331−-- "items": {},
1332−-- "maxItems": 0
1333−-- }
1189+-- >>> encode (SwaggerItemsArray [])
1190+-- "{\"example\":[],\"items\":{},\"maxItems\":0}"
13341191--
1335−instance ToJSON OpenApiItems where
1336− toJSON (OpenApiItemsObject x) = object [ "items" .= x ]
1337− toJSON (OpenApiItemsArray []) = object
1192+instance ToJSON SwaggerItems where
1193+ toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
1194+ toJSON (SwaggerItemsArray []) = object
13381195 [ "items" .= object []
13391196 , "maxItems" .= (0 :: Int)
13401197 , "example" .= Array mempty
13411198 ]
1342− toJSON (OpenApiItemsArray x) = object [ "items" .= x ]
1199+ toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
13431200
13441201instance ToJSON Components where
13451202 toJSON = sopSwaggerGenericToJSON
@@ -1384,10 +1241,6 @@ instance ToJSON Encoding where
13841241 toJSON = sopSwaggerGenericToJSON
13851242 toEncoding = sopSwaggerGenericToEncoding
13861243
1387−instance ToJSON Link where
1388− toJSON = sopSwaggerGenericToJSON
1389− toEncoding = sopSwaggerGenericToEncoding
1390−
13911244instance ToJSON SecurityDefinitions where
13921245 toJSON (SecurityDefinitions sd) = toJSON sd
13931246
@@ -1404,31 +1257,20 @@ instance ToJSON (Referenced Response) where toJSON = referencedToJSON "#/compone
14041257instance ToJSON (Referenced RequestBody) where toJSON = referencedToJSON "#/components/requestBodies/"
14051258instance ToJSON (Referenced Example) where toJSON = referencedToJSON "#/components/examples/"
14061259instance ToJSON (Referenced Header) where toJSON = referencedToJSON "#/components/headers/"
1407−instance ToJSON (Referenced Link) where toJSON = referencedToJSON "#/components/links/"
1408−instance ToJSON (Referenced Callback) where toJSON = referencedToJSON "#/components/callbacks/"
1260+
1261+instance ToJSON ParamSchema where
1262+ -- TODO: this is a bit fishy, why we need sub object only in `ToJSON`?
1263+ toJSON = sopSwaggerGenericToJSONWithOpts $
1264+ mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
14091265
14101266instance ToJSON AdditionalProperties where
14111267 toJSON (AdditionalPropertiesAllowed b) = toJSON b
14121268 toJSON (AdditionalPropertiesSchema s) = toJSON s
14131269
1414−instance ToJSON ExpressionOrValue where
1415− toJSON (Expression expr) = toJSON expr
1416− toJSON (Value val) = toJSON val
1417−
1418−instance ToJSON Callback where
1419− toJSON (Callback ps) = toJSON ps
1420−
14211270-- =======================================================================
14221271-- Manual FromJSON instances
14231272-- =======================================================================
14241273
1425−instance FromJSON MediaType where
1426− parseJSON = withText "MediaType" $ \str ->
1427− maybe (fail $ "Invalid media type literal " <> Text.unpack str) pure $ parseAccept $ encodeUtf8 str
1428−
1429−instance FromJSONKey MediaType where
1430− fromJSONKey = FromJSONKeyTextParser (parseJSON . String)
1431−
14321274instance (Eq p, FromJSON p, AesonDefaultValue p) => FromJSON (OAuth2Flow p) where
14331275 parseJSON = sopSwaggerGenericParseJSON
14341276
@@ -1439,19 +1281,14 @@ instance FromJSON SecuritySchemeType where
14391281 parseJSON js@(Object o) = do
14401282 (t :: Text) <- o .: "type"
14411283 case t of
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
1284+ "http" -> pure SecuritySchemeHttp
14481285 "apiKey" -> SecuritySchemeApiKey <$> parseJSON js
14491286 "oauth2" -> SecuritySchemeOAuth2 <$> (o .: "flows")
14501287 "openIdConnect" -> SecuritySchemeOpenIdConnect <$> (o .: "openIdConnectUrl")
14511288 _ -> empty
14521289 parseJSON _ = empty
14531290
1454−instance FromJSON OpenApi where
1291+instance FromJSON Swagger where
14551292 parseJSON = sopSwaggerGenericParseJSON
14561293
14571294instance FromJSON Server where
@@ -1463,28 +1300,27 @@ instance FromJSON SecurityScheme where
14631300instance FromJSON Schema where
14641301 parseJSON = fmap nullaryCleanup . sopSwaggerGenericParseJSON
14651302 where nullaryCleanup :: Schema -> Schema
1466− nullaryCleanup s =
1467− if _schemaItems s == Just (OpenApiItemsArray [])
1303+ nullaryCleanup s@Schema{_schemaParamSchema=ps} =
1304+ if _paramSchemaItems ps == Just (SwaggerItemsArray [])
14681305 then s { _schemaExample = Nothing
1469− , _schemaMaxItems = Nothing
1470− }
1306+ , _schemaParamSchema = ps { _paramSchemaMaxItems = Nothing } }
14711307 else s
14721308
14731309instance FromJSON Header where
14741310 parseJSON = sopSwaggerGenericParseJSON
14751311
1476−instance FromJSON OpenApiItems where
1312+instance FromJSON SwaggerItems where
14771313 parseJSON js@(Object obj)
1478− | null obj = pure $ OpenApiItemsArray [] -- Nullary schema.
1479− | otherwise = OpenApiItemsObject <$> parseJSON js
1480− parseJSON js@(Array _) = OpenApiItemsArray <$> parseJSON js
1314+ | null obj = pure $ SwaggerItemsArray [] -- Nullary schema.
1315+ | otherwise = SwaggerItemsObject <$> parseJSON js
1316+ parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
14811317 parseJSON _ = empty
14821318
14831319instance FromJSON Components where
14841320 parseJSON = sopSwaggerGenericParseJSON
14851321
14861322instance FromJSON MimeList where
1487− parseJSON js = MimeList . map fromString <$> parseJSON js
1323+ parseJSON js = (MimeList . map fromString) <$> parseJSON js
14881324
14891325instance FromJSON Param where
14901326 parseJSON = sopSwaggerGenericParseJSON
@@ -1492,7 +1328,7 @@ instance FromJSON Param where
14921328instance FromJSON Responses where
14931329 parseJSON (Object o) = Responses
14941330 <$> o .:? "default"
1495− <*> parseJSON (Object (deleteKey "default" o))
1331+ <*> (parseJSON (Object (HashMap.delete "default" o)))
14961332 parseJSON _ = empty
14971333
14981334instance FromJSON Example where
@@ -1519,9 +1355,6 @@ instance FromJSON MediaTypeObject where
15191355instance FromJSON Encoding where
15201356 parseJSON = sopSwaggerGenericParseJSON
15211357
1522−instance FromJSON Link where
1523− parseJSON = sopSwaggerGenericParseJSON
1524−
15251358instance FromJSON Reference where
15261359 parseJSON (Object o) = Reference <$> o .: "$ref"
15271360 parseJSON _ = empty
@@ -1545,24 +1378,17 @@ instance FromJSON (Referenced Response) where parseJSON = referencedParseJSON "#
15451378instance FromJSON (Referenced RequestBody) where parseJSON = referencedParseJSON "#/components/requestBodies/"
15461379instance FromJSON (Referenced Example) where parseJSON = referencedParseJSON "#/components/examples/"
15471380instance FromJSON (Referenced Header) where parseJSON = referencedParseJSON "#/components/headers/"
1548−instance FromJSON (Referenced Link) where parseJSON = referencedParseJSON "#/components/links/"
1549−instance FromJSON (Referenced Callback) where parseJSON = referencedParseJSON "#/components/callbacks/"
15501381
15511382instance FromJSON Xml where
15521383 parseJSON = genericParseJSON (jsonPrefix "xml")
15531384
1385+instance FromJSON ParamSchema where
1386+ parseJSON = sopSwaggerGenericParseJSON
1387+
15541388instance FromJSON AdditionalProperties where
15551389 parseJSON (Bool b) = pure $ AdditionalPropertiesAllowed b
15561390 parseJSON js = AdditionalPropertiesSchema <$> parseJSON js
15571391
1558−-- | All strings are parsed as expressions
1559−instance FromJSON ExpressionOrValue where
1560− parseJSON (String expr) = pure $ Expression expr
1561− parseJSON v = pure $ Value v
1562−
1563−instance FromJSON Callback where
1564− parseJSON = fmap Callback . parseJSON
1565−
15661392instance HasSwaggerAesonOptions Server where
15671393 swaggerAesonOptions _ = mkSwaggerAesonOptions "server"
15681394instance HasSwaggerAesonOptions Components where
@@ -1591,18 +1417,19 @@ instance HasSwaggerAesonOptions SecurityScheme where
15911417 swaggerAesonOptions _ = mkSwaggerAesonOptions "securityScheme" & saoSubObject ?~ "type"
15921418instance HasSwaggerAesonOptions Schema where
15931419 swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject ?~ "paramSchema"
1594−instance HasSwaggerAesonOptions OpenApi where
1420+instance HasSwaggerAesonOptions Swagger where
15951421 swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.0")]
15961422instance HasSwaggerAesonOptions Example where
15971423 swaggerAesonOptions _ = mkSwaggerAesonOptions "example"
15981424instance HasSwaggerAesonOptions Encoding where
15991425 swaggerAesonOptions _ = mkSwaggerAesonOptions "encoding"
16001426
1601−instance HasSwaggerAesonOptions Link where
1602− swaggerAesonOptions _ = mkSwaggerAesonOptions "link"
1427+instance HasSwaggerAesonOptions ParamSchema where
1428+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema"
16031429
16041430instance AesonDefaultValue Server
16051431instance AesonDefaultValue Components
1432+instance AesonDefaultValue ParamSchema
16061433instance AesonDefaultValue OAuth2ImplicitFlow
16071434instance AesonDefaultValue OAuth2PasswordFlow
16081435instance AesonDefaultValue OAuth2ClientCredentialsFlow
@@ -1610,10 +1437,7 @@ instance AesonDefaultValue OAuth2AuthorizationCodeFlow
16101437instance AesonDefaultValue p => AesonDefaultValue (OAuth2Flow p)
16111438instance AesonDefaultValue Responses
16121439instance AesonDefaultValue SecuritySchemeType
1613−instance AesonDefaultValue OpenApiType
1440+instance AesonDefaultValue SwaggerType
16141441instance AesonDefaultValue MimeList where defaultValue = Just mempty
16151442instance AesonDefaultValue Info
16161443instance AesonDefaultValue ParamLocation
1617−instance AesonDefaultValue Link
1618−instance AesonDefaultValue SecurityDefinitions where
1619− defaultValue = Just mempty