@@ -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
@@ -364,29 +338,6 @@ data MediaTypeObject = MediaTypeObject
364338 , _mediaTypeObjectEncoding :: InsOrdHashMap Text Encoding
365339 } deriving (Eq, Show, Generic, Data, Typeable)
366340
367−-- | In order to support common ways of serializing simple parameters, a set of style values are defined.
368−data Style
369− = StyleMatrix
370− -- ^ Path-style parameters defined by [RFC6570](https://tools.ietf.org/html/rfc6570#section-3.2.7).
371− | StyleLabel
372− -- ^ Label style parameters defined by [RFC6570](https://tools.ietf.org/html/rfc6570#section-3.2.7).
373− | StyleForm
374− -- ^ Form style parameters defined by [RFC6570](https://tools.ietf.org/html/rfc6570#section-3.2.7).
375− -- This option replaces @collectionFormat@ with a @csv@ (when @explode@ is false) or @multi@
376− -- (when explode is true) value from OpenAPI 2.0.
377− | StyleSimple
378− -- ^ Simple style parameters defined by [RFC6570](https://tools.ietf.org/html/rfc6570#section-3.2.7).
379− -- This option replaces @collectionFormat@ with a @csv@ value from OpenAPI 2.0.
380− | StyleSpaceDelimited
381− -- ^ Space separated array values.
382− -- This option replaces @collectionFormat@ equal to @ssv@ from OpenAPI 2.0.
383− | StylePipeDelimited
384− -- ^ Pipe separated array values.
385− -- This option replaces @collectionFormat@ equal to @pipes@ from OpenAPI 2.0.
386− | StyleDeepObject
387− -- ^ Provides a simple way of rendering nested objects using form parameters.
388− deriving (Eq, Show, Generic, Data, Typeable)
389−
390341data Encoding = Encoding
391342 { -- | The Content-Type for encoding a specific property.
392343 -- Default value depends on the property type: for @string@
@@ -394,8 +345,8 @@ data Encoding = Encoding
394345 -- for other primitive types – @text/plain@; for object - @application/json@;
395346 -- for array – the default is defined based on the inner type.
396347 -- 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
348+ -- a wildcard media type (e.g. @image/*@), or a comma-separated list of the two types.
349+ _encodingContentType :: Maybe Text
399350
400351 -- | A map allowing additional information to be provided as headers,
401352 -- for example @Content-Disposition@. @Content-Type@ is described separately
@@ -404,11 +355,11 @@ data Encoding = Encoding
404355 , _encodingHeaders :: InsOrdHashMap Text (Referenced Header)
405356
406357 -- | Describes how a specific property value will be serialized depending on its type.
407− -- See 'Param' Object for details on the style property.
358+ -- See Parameter Object for details on the style property.
408359 -- The behavior follows the same values as query parameters, including default values.
409360 -- This property SHALL be ignored if the request body media type
410361 -- is not @application/x-www-form-urlencoded@.
411− , _encodingStyle :: Maybe Style
362+ , _encodingStyle :: Maybe Text -- TODO enum
412363
413364 -- | When this is true, property values of type @array@ or @object@ generate
414365 -- separate parameters for each value of the array,
@@ -434,15 +385,18 @@ mimeListConstr :: Constr
434385mimeListConstr = mkConstr mimeListDataType "MimeList" ["getMimeList"] Prefix
435386
436387mimeListDataType :: DataType
437−mimeListDataType = mkDataType "Data.OpenApi.MimeList" [mimeListConstr]
388+mimeListDataType = mkDataType "Data.Swagger.MimeList" [mimeListConstr]
438389
439390instance Data MimeList where
440391 gunfold k z c = case constrIndex c of
441− 1 -> k (z (MimeList . map fromString))
392+ 1 -> k (z (\xs -> MimeList (map fromString xs)))
442393 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type MimeList."
443394 toConstr (MimeList _) = mimeListConstr
444395 dataTypeOf _ = mimeListDataType
445396
397+-- TODO style
398+-- TODO example
399+
446400-- | Describes a single operation parameter.
447401-- A unique parameter is defined by a combination of a name and location.
448402data Param = Param
@@ -452,7 +406,7 @@ data Param = Param
452406
453407 -- | A brief description of the parameter.
454408 -- This could contain examples of use.
455− -- CommonMark syntax MAY be used for rich text representation.
409+ -- GFM syntax can be used for rich text representation.
456410 , _paramDescription :: Maybe Text
457411
458412 -- | Determines whether this parameter is mandatory.
@@ -470,49 +424,10 @@ data Param = Param
470424 -- | Sets the ability to pass empty-valued parameters.
471425 -- This is valid only for 'ParamQuery' parameters and allows sending
472426 -- a parameter with an empty value. Default value is @false@.
473− , _paramAllowEmptyValue :: Maybe Bool
474−
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
427+ , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
481428
482429 -- | Parameter schema.
483430 , _paramSchema :: Maybe (Referenced Schema)
484−
485− -- | Describes how the parameter value will be serialized depending
486− -- on the type of the parameter value. Default values (based on value of '_paramIn'):
487− -- for 'ParamQuery' - 'StyleForm'; for 'ParamPath' - 'StyleSimple'; for 'ParamHeader' - 'StyleSimple';
488− -- for 'ParamCookie' - 'StyleForm'.
489− , _paramStyle :: Maybe Style
490−
491− -- | When this is true, parameter values of type @array@ or @object@
492− -- generate separate parameters for each value of the array or key-value pair of the map.
493− -- For other types of parameters this property has no effect.
494− -- When style is @form@, the default value is true. For all other styles, the default value is false.
495− , _paramExplode :: Maybe Bool
496−
497− -- | Example of the parameter's potential value.
498− -- The example SHOULD match the specified schema and encoding properties if present.
499− -- The '_paramExample' field is mutually exclusive of the '_paramExamples' field.
500− -- Furthermore, if referencing a schema that contains an example, the example value
501− -- SHALL override the example provided by the schema. To represent examples of media types
502− -- that cannot naturally be represented in JSON or YAML, a string value can contain
503− -- the example with escaping where necessary.
504− , _paramExample :: Maybe Value
505−
506− -- | Examples of the parameter's potential value.
507− -- Each example SHOULD contain a value in the correct format as specified
508− -- in the parameter encoding. The '_paramExamples' field is mutually exclusive of the '_paramExample' field.
509− -- Furthermore, if referencing a schema that contains an example,
510− -- the examples value SHALL override the example provided by the schema.
511− , _paramExamples :: InsOrdHashMap Text (Referenced Example)
512−
513− -- TODO
514− -- _paramContent :: InsOrdHashMap MediaType MediaTypeObject
515− -- should be singleton. mutually exclusive with _paramSchema.
516431 } deriving (Eq, Show, Generic, Data, Typeable)
517432
518433data Example = Example
@@ -537,64 +452,138 @@ data Example = Example
537452 , _exampleExternalValue :: Maybe URL
538453 } deriving (Eq, Show, Generic, Typeable, Data)
539454
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.
455+-- | Items for @'SwaggerArray'@ schemas.
577456--
578−-- __Warning__: OpenAPI 3.0 does not support tuple arrays. However, OpenAPI 3.1 will, as
579−-- it will incorporate Json Schema mostly verbatim.
457+-- @'SwaggerItemsPrimitive'@ should be used only for query params, headers and path pieces.
458+-- The @'CollectionFormat' t@ parameter specifies how elements of an array should be displayed.
459+-- Note that @fmt@ in @'SwaggerItemsPrimitive' fmt schema@ specifies format for elements of type @schema@.
460+-- This is different from the original Swagger's <http://swagger.io/specification/#itemsObject Items Object>.
580461--
581−-- @'OpenApiItemsObject'@ should be used to specify homogenous array @'Schema'@s.
462+-- @'SwaggerItemsObject'@ should be used to specify homogenous array @'Schema'@s.
582463--
583−-- @'OpenApiItemsArray'@ should be used to specify tuple @'Schema'@s.
584−data OpenApiItems where
585− OpenApiItemsObject :: Referenced Schema -> OpenApiItems
586− OpenApiItemsArray :: [Referenced Schema] -> OpenApiItems
587− deriving (Eq, Show, Typeable, Data)
588−
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
597− deriving (Eq, Show, Typeable, Generic, Data)
464+-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
465+data SwaggerItems t where
466+ SwaggerItemsPrimitive :: Maybe (CollectionFormat k) -> ParamSchema k-> SwaggerItems k
467+ SwaggerItemsObject :: Referenced Schema -> SwaggerItems 'SwaggerKindSchema
468+ SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems 'SwaggerKindSchema
469+ deriving (Typeable)
470+
471+deriving instance Eq (SwaggerItems t)
472+deriving instance Show (SwaggerItems t)
473+--deriving instance Typeable (SwaggerItems t)
474+
475+swaggerItemsPrimitiveConstr :: Constr
476+swaggerItemsPrimitiveConstr = mkConstr swaggerItemsDataType "SwaggerItemsPrimitive" [] Prefix
477+
478+swaggerItemsObjectConstr :: Constr
479+swaggerItemsObjectConstr = mkConstr swaggerItemsDataType "SwaggerItemsObject" [] Prefix
480+
481+swaggerItemsArrayConstr :: Constr
482+swaggerItemsArrayConstr = mkConstr swaggerItemsDataType "SwaggerItemsArray" [] Prefix
483+
484+swaggerItemsDataType :: DataType
485+swaggerItemsDataType = mkDataType "Data.Swagger.SwaggerItems" [swaggerItemsPrimitiveConstr]
486+
487+-- Note: unfortunately we have to write these Data instances by hand,
488+-- to get better contexts / avoid duplicate name when using standalone deriving
489+
490+instance Data t => Data (SwaggerItems ('SwaggerKindNormal t)) where
491+ -- TODO: define gfoldl
492+ gunfold k z c = case constrIndex c of
493+ 1 -> k (k (z SwaggerItemsPrimitive))
494+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems t)."
495+ toConstr _ = swaggerItemsPrimitiveConstr
496+ dataTypeOf _ = swaggerItemsDataType
497+
498+-- SwaggerItems SwaggerKindParamOtherSchema can be constructed using SwaggerItemsPrimitive only
499+instance Data (SwaggerItems 'SwaggerKindParamOtherSchema) where
500+ -- TODO: define gfoldl
501+ gunfold k z c = case constrIndex c of
502+ 1 -> k (k (z SwaggerItemsPrimitive))
503+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindParamOtherSchema)."
504+ toConstr _ = swaggerItemsPrimitiveConstr
505+ dataTypeOf _ = swaggerItemsDataType
506+
507+instance Data (SwaggerItems 'SwaggerKindSchema) where
508+ gfoldl _ _ (SwaggerItemsPrimitive _ _) = error " Data.Data.gfoldl: Constructor SwaggerItemsPrimitive used to construct SwaggerItems SwaggerKindSchema"
509+ gfoldl k z (SwaggerItemsObject ref) = z SwaggerItemsObject `k` ref
510+ gfoldl k z (SwaggerItemsArray ref) = z SwaggerItemsArray `k` ref
511+
512+ gunfold k z c = case constrIndex c of
513+ 2 -> k (z SwaggerItemsObject)
514+ 3 -> k (z SwaggerItemsArray)
515+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindSchema)."
516+
517+ toConstr (SwaggerItemsPrimitive _ _) = error "Not supported"
518+ toConstr (SwaggerItemsObject _) = swaggerItemsObjectConstr
519+ toConstr (SwaggerItemsArray _) = swaggerItemsArrayConstr
520+
521+ dataTypeOf _ = swaggerItemsDataType
522+
523+-- | Type used as a kind to avoid overlapping instances.
524+data SwaggerKind t
525+ = SwaggerKindNormal t
526+ | SwaggerKindParamOtherSchema
527+ | SwaggerKindSchema
528+ deriving (Typeable)
529+
530+deriving instance Typeable 'SwaggerKindNormal
531+deriving instance Typeable 'SwaggerKindParamOtherSchema
532+deriving instance Typeable 'SwaggerKindSchema
533+
534+-- TODO remove
535+type family SwaggerKindType (k :: SwaggerKind *) :: *
536+type instance SwaggerKindType ('SwaggerKindNormal t) = t
537+type instance SwaggerKindType 'SwaggerKindSchema = Schema
538+--type instance SwaggerKindType 'SwaggerKindParamOtherSchema = ParamOtherSchema
539+
540+data SwaggerType t where
541+ SwaggerString :: SwaggerType t
542+ SwaggerNumber :: SwaggerType t
543+ SwaggerInteger :: SwaggerType t
544+ SwaggerBoolean :: SwaggerType t
545+ SwaggerArray :: SwaggerType t
546+ SwaggerFile :: SwaggerType 'SwaggerKindParamOtherSchema
547+ SwaggerNull :: SwaggerType 'SwaggerKindSchema
548+ SwaggerObject :: SwaggerType 'SwaggerKindSchema
549+ deriving (Typeable)
550+
551+deriving instance Eq (SwaggerType t)
552+deriving instance Show (SwaggerType t)
553+
554+swaggerTypeConstr :: Data (SwaggerType t) => SwaggerType t -> Constr
555+swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
556+
557+swaggerTypeDataType :: {- Data (SwaggerType t) => -} SwaggerType t -> DataType
558+swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
559+
560+swaggerCommonTypes :: [SwaggerType k]
561+swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
562+
563+swaggerParamTypes :: [SwaggerType 'SwaggerKindParamOtherSchema]
564+swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
565+
566+swaggerSchemaTypes :: [SwaggerType 'SwaggerKindSchema]
567+swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
568+
569+swaggerTypeConstrs :: [Constr]
570+swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType 'SwaggerKindSchema])
571+ ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
572+
573+instance Typeable t => Data (SwaggerType ('SwaggerKindNormal t)) where
574+ gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
575+ toConstr = swaggerTypeConstr
576+ dataTypeOf = swaggerTypeDataType
577+
578+instance Data (SwaggerType 'SwaggerKindParamOtherSchema) where
579+ gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
580+ toConstr = swaggerTypeConstr
581+ dataTypeOf = swaggerTypeDataType
582+
583+instance Data (SwaggerType 'SwaggerKindSchema) where
584+ gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
585+ toConstr = swaggerTypeConstr
586+ dataTypeOf = swaggerTypeDataType
598587
599588data ParamLocation
600589 = -- | Parameters that are appended to the URL.
@@ -612,6 +601,42 @@ data ParamLocation
612601
613602type Format = Text
614603
604+-- | Determines the format of the array.
605+data CollectionFormat t where
606+ -- Comma separated values: @foo,bar@.
607+ CollectionCSV :: CollectionFormat t
608+ -- Space separated values: @foo bar@.
609+ CollectionSSV :: CollectionFormat t
610+ -- Tab separated values: @foo\\tbar@.
611+ CollectionTSV :: CollectionFormat t
612+ -- Pipe separated values: @foo|bar@.
613+ CollectionPipes :: CollectionFormat t
614+ -- Corresponds to multiple parameter instances
615+ -- instead of multiple values for a single instance @foo=bar&foo=baz@.
616+ -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
617+ CollectionMulti :: CollectionFormat 'SwaggerKindParamOtherSchema
618+ deriving (Typeable)
619+
620+deriving instance Eq (CollectionFormat t)
621+deriving instance Show (CollectionFormat t)
622+
623+collectionFormatConstr :: CollectionFormat t -> Constr
624+collectionFormatConstr cf = mkConstr collectionFormatDataType (show cf) [] Prefix
625+
626+collectionFormatDataType :: DataType
627+collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat" $
628+ map collectionFormatConstr collectionCommonFormats
629+
630+collectionCommonFormats :: [CollectionFormat t]
631+collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
632+
633+instance Data t => Data (CollectionFormat ('SwaggerKindNormal t)) where
634+ gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
635+ toConstr = collectionFormatConstr
636+ dataTypeOf _ = collectionFormatDataType
637+
638+deriving instance Data (CollectionFormat 'SwaggerKindParamOtherSchema)
639+
615640type ParamName = Text
616641
617642data Schema = Schema
@@ -638,33 +663,9 @@ data Schema = Schema
638663 , _schemaMaxProperties :: Maybe Integer
639664 , _schemaMinProperties :: Maybe Integer
640665
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
666+ , _schemaParamSchema :: ParamSchema 'SwaggerKindSchema
663667 } deriving (Eq, Show, Generic, Data, Typeable)
664668
665−-- | Regex pattern for @string@ type.
666−type Pattern = Text
667−
668669data Discriminator = Discriminator
669670 { -- | The name of the property in the payload that will hold the discriminator value.
670671 _discriminatorPropertyName :: Text
@@ -680,9 +681,41 @@ data NamedSchema = NamedSchema
680681 , _namedSchemaSchema :: Schema
681682 } deriving (Eq, Show, Generic, Data, Typeable)
682683
684+-- | Regex pattern for @string@ type.
685+type Pattern = Text
686+
687+-- TODO examples for params
688+
689+data ParamSchema (t :: SwaggerKind *) = ParamSchema
690+ { -- | Declares the value of the parameter that the server will use if none is provided,
691+ -- for example a @"count"@ to control the number of results per page might default to @100@
692+ -- if not supplied by the client in the request.
693+ -- (Note: "default" has no meaning for required parameters.)
694+ -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
695+ _paramSchemaDefault :: Maybe Value
696+
697+ , _paramSchemaType :: Maybe (SwaggerType t)
698+ , _paramSchemaFormat :: Maybe Format
699+ , _paramSchemaItems :: Maybe (SwaggerItems t)
700+ , _paramSchemaMaximum :: Maybe Scientific
701+ , _paramSchemaExclusiveMaximum :: Maybe Bool
702+ , _paramSchemaMinimum :: Maybe Scientific
703+ , _paramSchemaExclusiveMinimum :: Maybe Bool
704+ , _paramSchemaMaxLength :: Maybe Integer
705+ , _paramSchemaMinLength :: Maybe Integer
706+ , _paramSchemaPattern :: Maybe Pattern
707+ , _paramSchemaMaxItems :: Maybe Integer
708+ , _paramSchemaMinItems :: Maybe Integer
709+ , _paramSchemaUniqueItems :: Maybe Bool
710+ , _paramSchemaEnum :: Maybe [Value]
711+ , _paramSchemaMultipleOf :: Maybe Scientific
712+ } deriving (Eq, Show, Generic, Typeable)
713+
714+deriving instance (Typeable k, Data (Maybe (SwaggerType k)), Data (SwaggerItems k)) => Data (ParamSchema k)
715+
683716data Xml = Xml
684717 { -- | 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.
718+ -- When defined within the @'SwaggerItems'@ (items), it will affect the name of the individual XML elements within the list.
686719 -- When defined alongside type being array (outside the items),
687720 -- it will affect the wrapping element and only if wrapped is true.
688721 -- If wrapped is false, it will be ignored.
@@ -734,46 +767,25 @@ data Response = Response
734767 -- | A map containing descriptions of potential response payloads.
735768 -- The key is a media type or media type range and the value describes it.
736769 -- 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
770+ -- e.g. @text/plain@ overrides @text/*@.
771+ , _responseContent :: InsOrdHashMap Text MediaTypeObject
739772
740773 -- | Maps a header name to its definition.
741774 , _responseHeaders :: InsOrdHashMap HeaderName (Referenced Header)
742775
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)
776+ -- TODO links
747777 } deriving (Eq, Show, Generic, Data, Typeable)
748778
749779instance 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)
780+ fromString s = Response (fromString s) mempty mempty
759781
760782type HeaderName = Text
761783
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.
765784data Header = Header
766785 { -- | 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)
786+ _headerDescription :: Maybe Text
775787
776− , _headerSchema :: Maybe (Referenced Schema)
788+ , _headerParamSchema :: ParamSchema ('SwaggerKindNormal Header)
777789 } deriving (Eq, Show, Generic, Data, Typeable)
778790
779791-- | The location of the API key.
@@ -797,17 +809,17 @@ type AuthorizationURL = Text
797809-- | The token URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
798810type TokenURL = Text
799811
800−newtype OAuth2ImplicitFlow
801− = OAuth2ImplicitFlow {_oAuth2ImplicitFlowAuthorizationUrl :: AuthorizationURL}
802− deriving (Eq, Show, Generic, Data, Typeable)
812+data OAuth2ImplicitFlow = OAuth2ImplicitFlow
813+ { _oAuth2ImplicitFlowAuthorizationUrl :: AuthorizationURL
814+ } deriving (Eq, Show, Generic, Data, Typeable)
803815
804−newtype OAuth2PasswordFlow
805− = OAuth2PasswordFlow {_oAuth2PasswordFlowTokenUrl :: TokenURL}
806− deriving (Eq, Show, Generic, Data, Typeable)
816+data OAuth2PasswordFlow = OAuth2PasswordFlow
817+ { _oAuth2PasswordFlowTokenUrl :: TokenURL
818+ } deriving (Eq, Show, Generic, Data, Typeable)
807819
808−newtype OAuth2ClientCredentialsFlow
809− = OAuth2ClientCredentialsFlow {_oAuth2ClientCredentialsFlowTokenUrl :: TokenURL}
810− deriving (Eq, Show, Generic, Data, Typeable)
820+data OAuth2ClientCredentialsFlow = OAuth2ClientCredentialsFlow
821+ { _oAuth2ClientCredentialsFlowTokenUrl :: TokenURL
822+ } deriving (Eq, Show, Generic, Data, Typeable)
811823
812824data OAuth2AuthorizationCodeFlow = OAuth2AuthorizationCodeFlow
813825 { _oAuth2AuthorizationCodeFlowAuthorizationUrl :: AuthorizationURL
@@ -840,50 +852,8 @@ data OAuth2Flows = OAuth2Flows
840852 , _oAuth2FlowsAuthorizationCode :: Maybe (OAuth2Flow OAuth2AuthorizationCodeFlow)
841853 } deriving (Eq, Show, Generic, Data, Typeable)
842854
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−--
885855data SecuritySchemeType
886− = SecuritySchemeHttp HttpSchemeType
856+ = SecuritySchemeHttp
887857 | SecuritySchemeApiKey ApiKeyParams
888858 | SecuritySchemeOAuth2 OAuth2Flows
889859 | SecuritySchemeOpenIdConnect URL
@@ -897,10 +867,6 @@ data SecurityScheme = SecurityScheme
897867 , _securitySchemeDescription :: Maybe Text
898868 } deriving (Eq, Show, Generic, Data, Typeable)
899869
900−newtype SecurityDefinitions
901− = SecurityDefinitions (Definitions SecurityScheme)
902− deriving (Eq, Show, Generic, Data, Typeable)
903−
904870-- | Lists the required security schemes to execute this operation.
905871-- The object can have multiple security schemes declared in it which are all required
906872-- (that is, there is a logical AND between the schemes).
@@ -918,7 +884,7 @@ data Tag = Tag
918884 _tagName :: TagName
919885
920886 -- | A short description for the tag.
921− -- [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation.
887+ -- GFM syntax can be used for rich text representation.
922888 , _tagDescription :: Maybe Text
923889
924890 -- | Additional external documentation for this tag.
@@ -933,7 +899,7 @@ instance IsString Tag where
933899-- | Allows referencing an external resource for extended documentation.
934900data ExternalDocs = ExternalDocs
935901 { -- | A short description of the target documentation.
936− -- [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation.
902+ -- GFM syntax can be used for rich text representation.
937903 _externalDocsDescription :: Maybe Text
938904
939905 -- | The URL for the target documentation.
@@ -966,7 +932,6 @@ data AdditionalProperties
966932-- Generic instances
967933-------------------------------------------------------------------------------
968934
969−deriveGeneric ''Server
970935deriveGeneric ''Components
971936deriveGeneric ''Header
972937deriveGeneric ''OAuth2Flow
@@ -980,18 +945,18 @@ deriveGeneric ''MediaTypeObject
980945deriveGeneric ''Responses
981946deriveGeneric ''SecurityScheme
982947deriveGeneric ''Schema
983−deriveGeneric ''OpenApi
948+deriveGeneric ''ParamSchema
949+deriveGeneric ''Swagger
984950deriveGeneric ''Example
985951deriveGeneric ''Encoding
986−deriveGeneric ''Link
987952
988953-- =======================================================================
989954-- Monoid instances
990955-- =======================================================================
991956
992−instance Semigroup OpenApi where
957+instance Semigroup Swagger where
993958 (<>) = genericMappend
994−instance Monoid OpenApi where
959+instance Monoid Swagger where
995960 mempty = genericMempty
996961 mappend = (<>)
997962
@@ -1025,6 +990,12 @@ instance Monoid Schema where
1025990 mempty = genericMempty
1026991 mappend = (<>)
1027992
993+instance Semigroup (ParamSchema t) where
994+ (<>) = genericMappend
995+instance Monoid (ParamSchema t) where
996+ mempty = genericMempty
997+ mappend = (<>)
998+
1028999instance Semigroup Param where
10291000 (<>) = genericMappend
10301001instance Monoid Param where
@@ -1073,38 +1044,12 @@ instance Monoid Operation where
10731044 mempty = genericMempty
10741045 mappend = (<>)
10751046
1076−instance Semigroup (OAuth2Flow p) where
1077− l@OAuth2Flow{ _oAath2RefreshUrl = lUrl, _oAuth2Scopes = lScopes }
1078− <> OAuth2Flow { _oAath2RefreshUrl = rUrl, _oAuth2Scopes = rScopes } =
1079− l { _oAath2RefreshUrl = swaggerMappend lUrl rUrl, _oAuth2Scopes = lScopes <> rScopes }
1080−
1081−-- swaggerMappend has First-like semantics, and here we need mappend'ing under Maybes.
10821047instance Semigroup OAuth2Flows where
1083− l <> r = OAuth2Flows
1084− { _oAuth2FlowsImplicit = _oAuth2FlowsImplicit l <> _oAuth2FlowsImplicit r
1085− , _oAuth2FlowsPassword = _oAuth2FlowsPassword l <> _oAuth2FlowsPassword r
1086− , _oAuth2FlowsClientCredentials = _oAuth2FlowsClientCredentials l <> _oAuth2FlowsClientCredentials r
1087− , _oAuth2FlowsAuthorizationCode = _oAuth2FlowsAuthorizationCode l <> _oAuth2FlowsAuthorizationCode r
1088− }
1089−
1048+ (<>) = genericMappend
10901049instance Monoid OAuth2Flows where
10911050 mempty = genericMempty
10921051 mappend = (<>)
10931052
1094−instance Semigroup SecurityScheme where
1095− SecurityScheme (SecuritySchemeOAuth2 lFlows) lDesc
1096− <> SecurityScheme (SecuritySchemeOAuth2 rFlows) rDesc =
1097− SecurityScheme (SecuritySchemeOAuth2 $ lFlows <> rFlows) (swaggerMappend lDesc rDesc)
1098− l <> _ = l
1099−
1100−instance Semigroup SecurityDefinitions where
1101− (SecurityDefinitions sd1) <> (SecurityDefinitions sd2) =
1102− SecurityDefinitions $ InsOrdHashMap.unionWith (<>) sd1 sd2
1103−
1104−instance Monoid SecurityDefinitions where
1105− mempty = SecurityDefinitions InsOrdHashMap.empty
1106− mappend = (<>)
1107−
11081053instance Semigroup RequestBody where
11091054 (<>) = genericMappend
11101055instance Monoid RequestBody where
@@ -1119,19 +1064,19 @@ instance SwaggerMonoid Info
11191064instance SwaggerMonoid Components
11201065instance SwaggerMonoid PathItem
11211066instance SwaggerMonoid Schema
1067+instance SwaggerMonoid (ParamSchema t)
11221068instance SwaggerMonoid Param
11231069instance SwaggerMonoid Responses
11241070instance SwaggerMonoid Response
11251071instance SwaggerMonoid ExternalDocs
11261072instance SwaggerMonoid Operation
11271073instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)
1128−instance SwaggerMonoid SecurityDefinitions
11291074
11301075instance SwaggerMonoid MimeList
11311076deriving instance SwaggerMonoid URL
11321077
1133−instance SwaggerMonoid OpenApiType where
1134− swaggerMempty = OpenApiString
1078+instance SwaggerMonoid (SwaggerType t) where
1079+ swaggerMempty = SwaggerString
11351080 swaggerMappend _ y = y
11361081
11371082instance SwaggerMonoid ParamLocation where
@@ -1151,12 +1096,6 @@ instance Monoid a => SwaggerMonoid (Referenced a) where
11511096-- Simple Generic-based ToJSON instances
11521097-- =======================================================================
11531098
1154−instance ToJSON Style where
1155− toJSON = genericToJSON (jsonPrefix "Style")
1156−
1157−instance ToJSON OpenApiType where
1158− toJSON = genericToJSON (jsonPrefix "Swagger")
1159−
11601099instance ToJSON ParamLocation where
11611100 toJSON = genericToJSON (jsonPrefix "Param")
11621101
@@ -1169,6 +1108,9 @@ instance ToJSON Contact where
11691108instance ToJSON License where
11701109 toJSON = genericToJSON (jsonPrefix "License")
11711110
1111+instance ToJSON Server where
1112+ toJSON = genericToJSON (jsonPrefix "Server")
1113+
11721114instance ToJSON ServerVariable where
11731115 toJSON = genericToJSON (jsonPrefix "ServerVariable")
11741116
@@ -1206,12 +1148,6 @@ instance ToJSON OAuth2AuthorizationCodeFlow where
12061148-- Simple Generic-based FromJSON instances
12071149-- =======================================================================
12081150
1209−instance FromJSON Style where
1210− parseJSON = genericParseJSON (jsonPrefix "Style")
1211−
1212−instance FromJSON OpenApiType where
1213− parseJSON = genericParseJSON (jsonPrefix "Swagger")
1214−
12151151instance FromJSON ParamLocation where
12161152 parseJSON = genericParseJSON (jsonPrefix "Param")
12171153
@@ -1224,6 +1160,9 @@ instance FromJSON Contact where
12241160instance FromJSON License where
12251161 parseJSON = genericParseJSON (jsonPrefix "License")
12261162
1163+instance FromJSON Server where
1164+ parseJSON = genericParseJSON (jsonPrefix "Server")
1165+
12271166instance FromJSON ServerVariable where
12281167 parseJSON = genericParseJSON (jsonPrefix "ServerVariable")
12291168
@@ -1258,14 +1197,7 @@ instance FromJSON OAuth2AuthorizationCodeFlow where
12581197-- Manual ToJSON instances
12591198-- =======================================================================
12601199
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
1200+instance (Eq p, ToJSON p, AesonDefaultValue p) =>ToJSON (OAuth2Flow p) where
12691201 toJSON = sopSwaggerGenericToJSON
12701202 toEncoding = sopSwaggerGenericToEncoding
12711203
@@ -1274,49 +1206,33 @@ instance ToJSON OAuth2Flows where
12741206 toEncoding = sopSwaggerGenericToEncoding
12751207
12761208instance 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− ]
1209+ toJSON SecuritySchemeHttp
1210+ = object [ "type" .= ("http" :: Text) ]
12901211 toJSON (SecuritySchemeApiKey params)
12911212 = toJSON params
12921213 <+> object [ "type" .= ("apiKey" :: Text) ]
1293− toJSON (SecuritySchemeOAuth2 params) = object
1294− [ "type" .= ("oauth2" :: Text)
1295− , "flows" .= toJSON params
1296− ]
1214+ toJSON (SecuritySchemeOAuth2 params)
1215+ = toJSON params
1216+ <+> object [ "type" .= ("oauth2" :: Text) ]
12971217 toJSON (SecuritySchemeOpenIdConnect url) = object
12981218 [ "type" .= ("openIdConnect" :: Text)
12991219 , "openIdConnectUrl" .= url
13001220 ]
13011221
1302−instance ToJSON OpenApi where
1222+instance ToJSON Swagger where
13031223 toJSON a = sopSwaggerGenericToJSON a &
1304− if InsOrdHashMap.null (_openApiPaths a)
1224+ if InsOrdHashMap.null (_swaggerPaths a)
13051225 then (<+> object ["paths" .= object []])
13061226 else id
13071227 toEncoding = sopSwaggerGenericToEncoding
13081228
1309−instance ToJSON Server where
1310− toJSON = sopSwaggerGenericToJSON
1311− toEncoding = sopSwaggerGenericToEncoding
1312−
13131229instance ToJSON SecurityScheme where
13141230 toJSON = sopSwaggerGenericToJSON
13151231 toEncoding = sopSwaggerGenericToEncoding
13161232
13171233instance ToJSON Schema where
1318− toJSON = sopSwaggerGenericToJSONWithOpts $
1319− mkSwaggerAesonOptions "schema" & saoSubObject ?~ "items"
1234+ toJSON = sopSwaggerGenericToJSON
1235+ toEncoding = sopSwaggerGenericToEncoding
13201236
13211237instance ToJSON Header where
13221238 toJSON = sopSwaggerGenericToJSON
@@ -1325,21 +1241,20 @@ instance ToJSON Header where
13251241-- | As for nullary schema for 0-arity type constructors, see
13261242-- <https://github.com/GetShopTV/swagger2/issues/167>.
13271243--
1328−-- >>> BSL.putStrLn $ encodePretty (OpenApiItemsArray [])
1329−-- {
1330−-- "example": [],
1331−-- "items": {},
1332−-- "maxItems": 0
1333−-- }
1244+-- >>> encode (SwaggerItemsArray [])
1245+-- "{\"example\":[],\"items\":{},\"maxItems\":0}"
13341246--
1335−instance ToJSON OpenApiItems where
1336− toJSON (OpenApiItemsObject x) = object [ "items" .= x ]
1337− toJSON (OpenApiItemsArray []) = object
1247+instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
1248+ toJSON (SwaggerItemsPrimitive fmt schema) = object
1249+ [ "collectionFormat" .= fmt
1250+ , "items" .= schema ]
1251+ toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
1252+ toJSON (SwaggerItemsArray []) = object
13381253 [ "items" .= object []
13391254 , "maxItems" .= (0 :: Int)
13401255 , "example" .= Array mempty
13411256 ]
1342− toJSON (OpenApiItemsArray x) = object [ "items" .= x ]
1257+ toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
13431258
13441259instance ToJSON Components where
13451260 toJSON = sopSwaggerGenericToJSON
@@ -1384,13 +1299,6 @@ instance ToJSON Encoding where
13841299 toJSON = sopSwaggerGenericToJSON
13851300 toEncoding = sopSwaggerGenericToEncoding
13861301
1387−instance ToJSON Link where
1388− toJSON = sopSwaggerGenericToJSON
1389− toEncoding = sopSwaggerGenericToEncoding
1390−
1391−instance ToJSON SecurityDefinitions where
1392− toJSON (SecurityDefinitions sd) = toJSON sd
1393−
13941302instance ToJSON Reference where
13951303 toJSON (Reference ref) = object [ "$ref" .= ref ]
13961304
@@ -1404,31 +1312,37 @@ instance ToJSON (Referenced Response) where toJSON = referencedToJSON "#/compone
14041312instance ToJSON (Referenced RequestBody) where toJSON = referencedToJSON "#/components/requestBodies/"
14051313instance ToJSON (Referenced Example) where toJSON = referencedToJSON "#/components/examples/"
14061314instance 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/"
1315+
1316+instance ToJSON (SwaggerType t) where
1317+ toJSON SwaggerArray = "array"
1318+ toJSON SwaggerString = "string"
1319+ toJSON SwaggerInteger = "integer"
1320+ toJSON SwaggerNumber = "number"
1321+ toJSON SwaggerBoolean = "boolean"
1322+ toJSON SwaggerFile = "file"
1323+ toJSON SwaggerNull = "null"
1324+ toJSON SwaggerObject = "object"
1325+
1326+instance ToJSON (CollectionFormat t) where
1327+ toJSON CollectionCSV = "csv"
1328+ toJSON CollectionSSV = "ssv"
1329+ toJSON CollectionTSV = "tsv"
1330+ toJSON CollectionPipes = "pipes"
1331+ toJSON CollectionMulti = "multi"
1332+
1333+instance ToJSON (ParamSchema k) where
1334+ -- TODO: this is a bit fishy, why we need sub object only in `ToJSON`?
1335+ toJSON = sopSwaggerGenericToJSONWithOpts $
1336+ mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
14091337
14101338instance ToJSON AdditionalProperties where
14111339 toJSON (AdditionalPropertiesAllowed b) = toJSON b
14121340 toJSON (AdditionalPropertiesSchema s) = toJSON s
14131341
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−
14211342-- =======================================================================
14221343-- Manual FromJSON instances
14231344-- =======================================================================
14241345
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−
14321346instance (Eq p, FromJSON p, AesonDefaultValue p) => FromJSON (OAuth2Flow p) where
14331347 parseJSON = sopSwaggerGenericParseJSON
14341348
@@ -1439,22 +1353,14 @@ instance FromJSON SecuritySchemeType where
14391353 parseJSON js@(Object o) = do
14401354 (t :: Text) <- o .: "type"
14411355 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
1356+ "http" -> pure SecuritySchemeHttp
14481357 "apiKey" -> SecuritySchemeApiKey <$> parseJSON js
1449− "oauth2" -> SecuritySchemeOAuth2 <$> (o .: "flows")
1358+ "oauth2" -> SecuritySchemeOAuth2 <$> parseJSON js
14501359 "openIdConnect" -> SecuritySchemeOpenIdConnect <$> (o .: "openIdConnectUrl")
14511360 _ -> empty
14521361 parseJSON _ = empty
14531362
1454−instance FromJSON OpenApi where
1455− parseJSON = sopSwaggerGenericParseJSON
1456−
1457−instance FromJSON Server where
1363+instance FromJSON Swagger where
14581364 parseJSON = sopSwaggerGenericParseJSON
14591365
14601366instance FromJSON SecurityScheme where
@@ -1463,28 +1369,48 @@ instance FromJSON SecurityScheme where
14631369instance FromJSON Schema where
14641370 parseJSON = fmap nullaryCleanup . sopSwaggerGenericParseJSON
14651371 where nullaryCleanup :: Schema -> Schema
1466− nullaryCleanup s =
1467− if _schemaItems s == Just (OpenApiItemsArray [])
1372+ nullaryCleanup s@Schema{_schemaParamSchema=ps} =
1373+ if _paramSchemaItems ps == Just (SwaggerItemsArray [])
14681374 then s { _schemaExample = Nothing
1469− , _schemaMaxItems = Nothing
1470− }
1375+ , _schemaParamSchema = ps { _paramSchemaMaxItems = Nothing } }
14711376 else s
14721377
14731378instance FromJSON Header where
14741379 parseJSON = sopSwaggerGenericParseJSON
14751380
1476−instance FromJSON OpenApiItems where
1381+instance (FromJSON (CollectionFormat ('SwaggerKindNormal t)), FromJSON (ParamSchema ('SwaggerKindNormal t))) => FromJSON (SwaggerItems ('SwaggerKindNormal t)) where
1382+ parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1383+ <$> o .:? "collectionFormat"
1384+ <*> (o .: "items" >>= parseJSON)
1385+
1386+instance FromJSON (SwaggerItems 'SwaggerKindParamOtherSchema) where
1387+ parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1388+ <$> o .:? "collectionFormat"
1389+ <*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))
1390+
1391+-- |
1392+--
1393+-- >>> decode "{}" :: Maybe (SwaggerItems 'SwaggerKindSchema)
1394+-- Just (SwaggerItemsArray [])
1395+--
1396+-- >>> eitherDecode "{\"$ref\":\"#/definitions/example\"}" :: Either String (SwaggerItems 'SwaggerKindSchema)
1397+-- Right (SwaggerItemsObject (Ref (Reference {getReference = "example"})))
1398+--
1399+-- >>> eitherDecode "[{\"$ref\":\"#/definitions/example\"}]" :: Either String (SwaggerItems 'SwaggerKindSchema)
1400+-- Right (SwaggerItemsArray [Ref (Reference {getReference = "example"})])
1401+--
1402+instance FromJSON (SwaggerItems 'SwaggerKindSchema) where
14771403 parseJSON js@(Object obj)
1478− | null obj = pure $ OpenApiItemsArray [] -- Nullary schema.
1479− | otherwise = OpenApiItemsObject <$> parseJSON js
1480− parseJSON js@(Array _) = OpenApiItemsArray <$> parseJSON js
1404+ | null obj = pure $ SwaggerItemsArray [] -- Nullary schema.
1405+ | otherwise = SwaggerItemsObject <$> parseJSON js
1406+ parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
14811407 parseJSON _ = empty
14821408
14831409instance FromJSON Components where
14841410 parseJSON = sopSwaggerGenericParseJSON
14851411
14861412instance FromJSON MimeList where
1487− parseJSON js = MimeList . map fromString <$> parseJSON js
1413+ parseJSON js = (MimeList . map fromString) <$> parseJSON js
14881414
14891415instance FromJSON Param where
14901416 parseJSON = sopSwaggerGenericParseJSON
@@ -1492,7 +1418,7 @@ instance FromJSON Param where
14921418instance FromJSON Responses where
14931419 parseJSON (Object o) = Responses
14941420 <$> o .:? "default"
1495− <*> parseJSON (Object (deleteKey "default" o))
1421+ <*> (parseJSON (Object (HashMap.delete "default" o)))
14961422 parseJSON _ = empty
14971423
14981424instance FromJSON Example where
@@ -1507,9 +1433,6 @@ instance FromJSON Operation where
15071433instance FromJSON PathItem where
15081434 parseJSON = sopSwaggerGenericParseJSON
15091435
1510−instance FromJSON SecurityDefinitions where
1511− parseJSON js = SecurityDefinitions <$> parseJSON js
1512−
15131436instance FromJSON RequestBody where
15141437 parseJSON = sopSwaggerGenericParseJSON
15151438
@@ -1519,9 +1442,6 @@ instance FromJSON MediaTypeObject where
15191442instance FromJSON Encoding where
15201443 parseJSON = sopSwaggerGenericParseJSON
15211444
1522−instance FromJSON Link where
1523− parseJSON = sopSwaggerGenericParseJSON
1524−
15251445instance FromJSON Reference where
15261446 parseJSON (Object o) = Reference <$> o .: "$ref"
15271447 parseJSON _ = empty
@@ -1545,30 +1465,44 @@ instance FromJSON (Referenced Response) where parseJSON = referencedParseJSON "#
15451465instance FromJSON (Referenced RequestBody) where parseJSON = referencedParseJSON "#/components/requestBodies/"
15461466instance FromJSON (Referenced Example) where parseJSON = referencedParseJSON "#/components/examples/"
15471467instance 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/"
15501468
15511469instance FromJSON Xml where
15521470 parseJSON = genericParseJSON (jsonPrefix "xml")
15531471
1472+instance FromJSON (SwaggerType 'SwaggerKindSchema) where
1473+ parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
1474+
1475+instance FromJSON (SwaggerType 'SwaggerKindParamOtherSchema) where
1476+ parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerFile]
1477+
1478+instance FromJSON (SwaggerType ('SwaggerKindNormal t)) where
1479+ parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
1480+
1481+instance FromJSON (CollectionFormat ('SwaggerKindNormal t)) where
1482+ parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1483+
1484+-- NOTE: There aren't collections of 'Schema'
1485+--instance FromJSON (CollectionFormat (SwaggerKindSchema)) where
1486+-- parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1487+
1488+instance FromJSON (CollectionFormat 'SwaggerKindParamOtherSchema) where
1489+ parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
1490+
1491+instance (FromJSON (SwaggerType ('SwaggerKindNormal t)), FromJSON (SwaggerItems ('SwaggerKindNormal t))) => FromJSON (ParamSchema ('SwaggerKindNormal t)) where
1492+ parseJSON = sopSwaggerGenericParseJSON
1493+instance FromJSON (ParamSchema 'SwaggerKindParamOtherSchema) where
1494+ parseJSON = sopSwaggerGenericParseJSON
1495+instance FromJSON (ParamSchema 'SwaggerKindSchema) where
1496+ parseJSON = sopSwaggerGenericParseJSON
1497+
15541498instance FromJSON AdditionalProperties where
15551499 parseJSON (Bool b) = pure $ AdditionalPropertiesAllowed b
15561500 parseJSON js = AdditionalPropertiesSchema <$> parseJSON js
15571501
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−
1566−instance HasSwaggerAesonOptions Server where
1567− swaggerAesonOptions _ = mkSwaggerAesonOptions "server"
15681502instance HasSwaggerAesonOptions Components where
15691503 swaggerAesonOptions _ = mkSwaggerAesonOptions "components"
15701504instance HasSwaggerAesonOptions Header where
1571− swaggerAesonOptions _ = mkSwaggerAesonOptions "header"
1505+ swaggerAesonOptions _ = mkSwaggerAesonOptions "header" & saoSubObject ?~ "paramSchema"
15721506instance AesonDefaultValue p => HasSwaggerAesonOptions (OAuth2Flow p) where
15731507 swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2" & saoSubObject ?~ "params"
15741508instance HasSwaggerAesonOptions OAuth2Flows where
@@ -1591,18 +1525,23 @@ instance HasSwaggerAesonOptions SecurityScheme where
15911525 swaggerAesonOptions _ = mkSwaggerAesonOptions "securityScheme" & saoSubObject ?~ "type"
15921526instance HasSwaggerAesonOptions Schema where
15931527 swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject ?~ "paramSchema"
1594−instance HasSwaggerAesonOptions OpenApi where
1595− swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.0")]
1528+instance HasSwaggerAesonOptions Swagger where
1529+ swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.3")]
15961530instance HasSwaggerAesonOptions Example where
15971531 swaggerAesonOptions _ = mkSwaggerAesonOptions "example"
15981532instance HasSwaggerAesonOptions Encoding where
15991533 swaggerAesonOptions _ = mkSwaggerAesonOptions "encoding"
16001534
1601−instance HasSwaggerAesonOptions Link where
1602− swaggerAesonOptions _ = mkSwaggerAesonOptions "link"
1535+instance HasSwaggerAesonOptions (ParamSchema ('SwaggerKindNormal t)) where
1536+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1537+instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindParamOtherSchema) where
1538+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1539+-- NOTE: Schema doesn't have 'items' sub object!
1540+instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindSchema) where
1541+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema"
16031542
1604−instance AesonDefaultValue Server
16051543instance AesonDefaultValue Components
1544+instance AesonDefaultValue (ParamSchema s)
16061545instance AesonDefaultValue OAuth2ImplicitFlow
16071546instance AesonDefaultValue OAuth2PasswordFlow
16081547instance AesonDefaultValue OAuth2ClientCredentialsFlow
@@ -1610,10 +1549,7 @@ instance AesonDefaultValue OAuth2AuthorizationCodeFlow
16101549instance AesonDefaultValue p => AesonDefaultValue (OAuth2Flow p)
16111550instance AesonDefaultValue Responses
16121551instance AesonDefaultValue SecuritySchemeType
1613−instance AesonDefaultValue OpenApiType
1552+instance AesonDefaultValue (SwaggerType a)
16141553instance AesonDefaultValue MimeList where defaultValue = Just mempty
16151554instance AesonDefaultValue Info
16161555instance AesonDefaultValue ParamLocation
1617−instance AesonDefaultValue Link
1618−instance AesonDefaultValue SecurityDefinitions where
1619− defaultValue = Just mempty