@@ -1,4 +1,7 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DataKinds #-}
13{-# LANGUAGE DeriveDataTypeable #-}
4+{-# LANGUAGE DeriveFunctor #-}
25{-# LANGUAGE DeriveGeneric #-}
36{-# LANGUAGE DeriveTraversable #-}
47{-# LANGUAGE FlexibleContexts #-}
@@ -10,90 +13,127 @@
1013{-# LANGUAGE ScopedTypeVariables #-}
1114{-# LANGUAGE StandaloneDeriving #-}
1215{-# LANGUAGE TemplateHaskell #-}
16+{-# LANGUAGE TypeFamilies #-}
1317{-# LANGUAGE UndecidableInstances #-}
18+#if __GLASGOW_HASKELL__ <710
19+{-# LANGUAGE PolyKinds #-}
20+#endif
21+#include "overlapping-compat.h"
1422module Data.Swagger.Internal where
1523
24+import Prelude ()
25+import Prelude.Compat
26+
27+import Control.Lens ((&), (.~), (?~))
1628import Control.Applicative
17−import Control.Monad
1829import Data.Aeson
19−import Data.Aeson.TH (deriveJSON)
2030import qualified Data.Aeson.Types as JSON
2131import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
22−import Data.HashMap.Strict (HashMap)
32+import Data.Hashable (Hashable)
2333import qualified Data.HashMap.Strict as HashMap
34+import Data.HashSet.InsOrd (InsOrdHashSet)
2435import Data.Map (Map)
2536import qualified Data.Map as Map
26−import Data.Monoid
37+import Data.Monoid (Monoid (..))
38+import Data.Semigroup.Compat (Semigroup (..))
2739import Data.Scientific (Scientific)
28−import Data.String (fromString)
40+import Data.String (IsString(..))
2941import Data.Text (Text)
3042import qualified Data.Text as Text
3143import GHC.Generics (Generic)
32−import Network (HostName, PortNumber)
44+import Network.Socket (HostName, PortNumber)
3345import Network.HTTP.Media (MediaType)
3446import Text.Read (readMaybe)
3547
48+import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
49+import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
50+
51+import Generics.SOP.TH (deriveGeneric)
52+import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
53+ ,sopSwaggerGenericToJSONWithOpts
54+ ,sopSwaggerGenericParseJSON
55+ ,HasSwaggerAesonOptions(..)
56+ ,AesonDefaultValue(..)
57+ ,mkSwaggerAesonOptions
58+ ,saoAdditionalPairs
59+ ,saoSubObject)
3660import Data.Swagger.Internal.Utils
3761
62+#if MIN_VERSION_aeson(0,10,0)
63+import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToEncoding)
64+#define DEFINE_TOENCODING toEncoding = sopSwaggerGenericToEncoding
65+#else
66+#define DEFINE_TOENCODING
67+#endif
68+
69+-- $setup
70+-- >>> :seti -XDataKinds
71+-- >>> import Data.Aeson
72+
73+-- | A list of definitions that can be used in references.
74+type Definitions = InsOrdHashMap Text
75+
3876-- | This is the root document object for the API specification.
3977data Swagger = Swagger
4078 { -- | Provides metadata about the API.
4179 -- The metadata can be used by the clients if needed.
42− _info :: Info
80+ _swaggerInfo :: Info
4381
4482 -- | The host (name or ip) serving the API. It MAY include a port.
4583 -- If the host is not included, the host serving the documentation is to be used (including the port).
46− , _host :: Maybe Host
84+ , _swaggerHost :: Maybe Host
4785
4886 -- | The base path on which the API is served, which is relative to the host.
4987 -- If it is not included, the API is served directly under the host.
5088 -- The value MUST start with a leading slash (/).
51− , _basePath :: Maybe FilePath
89+ , _swaggerBasePath :: Maybe FilePath
5290
5391 -- | The transfer protocol of the API.
5492 -- If the schemes is not included, the default scheme to be used is the one used to access the Swagger definition itself.
55− , _schemes :: Maybe [Scheme]
93+ , _swaggerSchemes :: Maybe [Scheme]
5694
5795 -- | A list of MIME types the APIs can consume.
5896 -- This is global to all APIs but can be overridden on specific API calls.
59− , _consumes :: MimeList
97+ , _swaggerConsumes :: MimeList
6098
6199 -- | A list of MIME types the APIs can produce.
62− -- This is global to all APIs but can be overridden on specific API calls.
63− , _produces :: MimeList
100+ -- This is global to all APIs but can be overridden on specific API calls.
101+ , _swaggerProduces :: MimeList
64102
65103 -- | The available paths and operations for the API.
66− , _paths :: Paths
104+ -- Holds the relative paths to the individual endpoints.
105+ -- The path is appended to the @'basePath'@ in order to construct the full URL.
106+ , _swaggerPaths :: InsOrdHashMap FilePath PathItem
67107
68108 -- | An object to hold data types produced and consumed by operations.
69− , _definitions :: HashMap Text Schema
109+ , _swaggerDefinitions :: Definitions Schema
70110
71111 -- | An object to hold parameters that can be used across operations.
72112 -- This property does not define global parameters for all operations.
73− , _parameters :: HashMap Text Param
113+ , _swaggerParameters :: Definitions Param
74114
75115 -- | An object to hold responses that can be used across operations.
76116 -- This property does not define global responses for all operations.
77− , _responses :: HashMap Text Response
117+ , _swaggerResponses :: Definitions Response
78118
79119 -- | Security scheme definitions that can be used across the specification.
80− , _securityDefinitions :: HashMap Text SecurityScheme
120+ , _swaggerSecurityDefinitions :: SecurityDefinitions
81121
82122 -- | A declaration of which security schemes are applied for the API as a whole.
83123 -- The list of values describes alternative security schemes that can be used
84124 -- (that is, there is a logical OR between the security requirements).
85125 -- Individual operations can override this definition.
86− , _security :: [SecurityRequirement]
126+ , _swaggerSecurity :: [SecurityRequirement]
87127
88128 -- | A list of tags used by the specification with additional metadata.
89129 -- The order of the tags can be used to reflect on their order by the parsing tools.
90130 -- Not all tags that are used by the Operation Object must be declared.
91131 -- The tags that are not declared may be organized randomly or based on the tools' logic.
92132 -- Each tag name in the list MUST be unique.
93− , _tags :: [Tag]
133+ , _swaggerTags :: InsOrdHashSet Tag
94134
95135 -- | Additional external documentation.
96− , _externalDocs :: Maybe ExternalDocs
136+ , _swaggerExternalDocs :: Maybe ExternalDocs
97137 } deriving (Eq, Show, Generic, Data, Typeable)
98138
99139-- | The object provides metadata about the API.
@@ -142,12 +182,18 @@ data License = License
142182 , _licenseUrl :: Maybe URL
143183 } deriving (Eq, Show, Generic, Data, Typeable)
144184
185+instance IsString License where
186+ fromString s = License (fromString s) Nothing
187+
145188-- | The host (name or ip) serving the API. It MAY include a port.
146189data Host = Host
147190 { _hostName :: HostName -- ^ Host name.
148191 , _hostPort :: Maybe PortNumber -- ^ Optional port.
149192 } deriving (Eq, Show, Generic, Typeable)
150193
194+instance IsString Host where
195+ fromString s = Host s Nothing
196+
151197hostConstr :: Constr
152198hostConstr = mkConstr hostDataType "Host" [] Prefix
153199
@@ -169,13 +215,6 @@ data Scheme
169215 | Wss
170216 deriving (Eq, Show, Generic, Data, Typeable)
171217
172−-- | The available paths and operations for the API.
173−data Paths = Paths
174− { -- | Holds the relative paths to the individual endpoints.
175− -- The path is appended to the @'basePath'@ in order to construct the full URL.
176− _pathsMap :: HashMap FilePath PathItem
177− } deriving (Eq, Show, Generic, Data, Typeable)
178−
179218-- | Describes the operations available on a single path.
180219-- A @'PathItem'@ may be empty, due to ACL constraints.
181220-- The path itself is still exposed to the documentation viewer
@@ -213,7 +252,7 @@ data PathItem = PathItem
213252data Operation = Operation
214253 { -- | A list of tags for API documentation control.
215254 -- Tags can be used for logical grouping of operations by resources or any other qualifier.
216− _operationTags :: [Text]
255+ _operationTags :: InsOrdHashSet TagName
217256
218257 -- | A short summary of what the operation does.
219258 -- For maximum readability in the swagger-ui, this field SHOULD be less than 120 characters.
@@ -270,7 +309,7 @@ data Operation = Operation
270309 } deriving (Eq, Show, Generic, Data, Typeable)
271310
272311newtype MimeList = MimeList { getMimeList :: [MediaType] }
273− deriving (Eq, Show, Monoid, Typeable)
312+ deriving (Eq, Show, Semigroup, Monoid, Typeable)
274313
275314mimeListConstr :: Constr
276315mimeListConstr = mkConstr mimeListDataType "MimeList" ["getMimeList"] Prefix
@@ -321,36 +360,92 @@ data ParamOtherSchema = ParamOtherSchema
321360 -- Default value is @False@.
322361 , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
323362
324− -- | Determines the format of the array if @'ParamArray'@ is used.
325− -- Default value is csv.
326− , _paramOtherSchemaCollectionFormat :: Maybe (CollectionFormat Param)
327−
328− , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema
329− } deriving (Eq, Show, Generic, Data, Typeable)
330−
363+ , _paramOtherSchemaParamSchema :: ParamSchema 'SwaggerKindParamOtherSchema
364+ } deriving (Eq, Show, Generic, Typeable, Data)
365+
366+-- | Items for @'SwaggerArray'@ schemas.
367+--
368+-- @'SwaggerItemsPrimitive'@ should be used only for query params, headers and path pieces.
369+-- The @'CollectionFormat' t@ parameter specifies how elements of an array should be displayed.
370+-- Note that @fmt@ in @'SwaggerItemsPrimitive' fmt schema@ specifies format for elements of type @schema@.
371+-- This is different from the original Swagger's <http://swagger.io/specification/#itemsObject Items Object>.
372+--
373+-- @'SwaggerItemsObject'@ should be used to specify homogenous array @'Schema'@s.
374+--
375+-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
331376data SwaggerItems t where
332− SwaggerItemsPrimitive :: Items -> SwaggerItems t
333− SwaggerItemsObject :: Referenced Schema -> SwaggerItems Schema
334− SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems Schema
377+ SwaggerItemsPrimitive :: Maybe (CollectionFormat k) -> ParamSchema k-> SwaggerItems k
378+ SwaggerItemsObject :: Referenced Schema -> SwaggerItems 'SwaggerKindSchema
379+ SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems 'SwaggerKindSchema
380+ deriving (Typeable)
335381
336382deriving instance Eq (SwaggerItems t)
337383deriving instance Show (SwaggerItems t)
338−deriving instance Typeable (SwaggerItems t)
384+--deriving instance Typeable (SwaggerItems t)
339385
340386swaggerItemsPrimitiveConstr :: Constr
341387swaggerItemsPrimitiveConstr = mkConstr swaggerItemsDataType "SwaggerItemsPrimitive" [] Prefix
342388
389+swaggerItemsObjectConstr :: Constr
390+swaggerItemsObjectConstr = mkConstr swaggerItemsDataType "SwaggerItemsObject" [] Prefix
391+
392+swaggerItemsArrayConstr :: Constr
393+swaggerItemsArrayConstr = mkConstr swaggerItemsDataType "SwaggerItemsArray" [] Prefix
394+
343395swaggerItemsDataType :: DataType
344396swaggerItemsDataType = mkDataType "Data.Swagger.SwaggerItems" [swaggerItemsPrimitiveConstr]
345397
346−instance {-# OVERLAPPABLE #-} Typeable t => Data (SwaggerItems t) where
398+-- Note: unfortunately we have to write these Data instances by hand,
399+-- to get better contexts / avoid duplicate name when using standalone deriving
400+
401+instance Data t => Data (SwaggerItems ('SwaggerKindNormal t)) where
402+ -- TODO: define gfoldl
347403 gunfold k z c = case constrIndex c of
348− 1 -> k (z SwaggerItemsPrimitive)
404+ 1 -> k (k (z SwaggerItemsPrimitive))
349405 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems t)."
350406 toConstr _ = swaggerItemsPrimitiveConstr
351407 dataTypeOf _ = swaggerItemsDataType
352408
353−deriving instance Data (SwaggerItems Schema)
409+-- SwaggerItems SwaggerKindParamOtherSchema can be constructed using SwaggerItemsPrimitive only
410+instance Data (SwaggerItems 'SwaggerKindParamOtherSchema) where
411+ -- TODO: define gfoldl
412+ gunfold k z c = case constrIndex c of
413+ 1 -> k (k (z SwaggerItemsPrimitive))
414+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindParamOtherSchema)."
415+ toConstr _ = swaggerItemsPrimitiveConstr
416+ dataTypeOf _ = swaggerItemsDataType
417+
418+instance Data (SwaggerItems 'SwaggerKindSchema) where
419+ gfoldl _ _ (SwaggerItemsPrimitive _ _) = error $ " Data.Data.gfoldl: Constructor SwaggerItemsPrimitive used to construct SwaggerItems SwaggerKindSchema"
420+ gfoldl k z (SwaggerItemsObject ref) = z SwaggerItemsObject `k` ref
421+ gfoldl k z (SwaggerItemsArray ref) = z SwaggerItemsArray `k` ref
422+
423+ gunfold k z c = case constrIndex c of
424+ 2 -> k (z SwaggerItemsObject)
425+ 3 -> k (z SwaggerItemsArray)
426+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindSchema)."
427+
428+ toConstr (SwaggerItemsPrimitive _ _) = error "Not supported"
429+ toConstr (SwaggerItemsObject _) = swaggerItemsObjectConstr
430+ toConstr (SwaggerItemsArray _) = swaggerItemsArrayConstr
431+
432+ dataTypeOf _ = swaggerItemsDataType
433+
434+-- | Type used as a kind to avoid overlapping instances.
435+data SwaggerKind t
436+ = SwaggerKindNormal t
437+ | SwaggerKindParamOtherSchema
438+ | SwaggerKindSchema
439+ deriving (Typeable)
440+
441+deriving instance Typeable 'SwaggerKindNormal
442+deriving instance Typeable 'SwaggerKindParamOtherSchema
443+deriving instance Typeable 'SwaggerKindSchema
444+
445+type family SwaggerKindType (k :: SwaggerKind *) :: *
446+type instance SwaggerKindType ('SwaggerKindNormal t) = t
447+type instance SwaggerKindType 'SwaggerKindSchema = Schema
448+type instance SwaggerKindType 'SwaggerKindParamOtherSchema = ParamOtherSchema
354449
355450data SwaggerType t where
356451 SwaggerString :: SwaggerType t
@@ -358,44 +453,44 @@ data SwaggerType t where
358453 SwaggerInteger :: SwaggerType t
359454 SwaggerBoolean :: SwaggerType t
360455 SwaggerArray :: SwaggerType t
361− SwaggerFile :: SwaggerType ParamOtherSchema
362− SwaggerNull :: SwaggerType Schema
363− SwaggerObject :: SwaggerType Schema
456+ SwaggerFile :: SwaggerType 'SwaggerKindParamOtherSchema
457+ SwaggerNull :: SwaggerType 'SwaggerKindSchema
458+ SwaggerObject :: SwaggerType 'SwaggerKindSchema
459+ deriving (Typeable)
364460
365461deriving instance Eq (SwaggerType t)
366462deriving instance Show (SwaggerType t)
367−deriving instance Typeable (SwaggerType t)
368463
369464swaggerTypeConstr :: Data (SwaggerType t) => SwaggerType t -> Constr
370465swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
371466
372−swaggerTypeDataType :: Data (SwaggerType t) => SwaggerType t -> DataType
467+swaggerTypeDataType :: {- Data (SwaggerType t) => -} SwaggerType t -> DataType
373468swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
374469
375−swaggerCommonTypes :: [SwaggerType t]
470+swaggerCommonTypes :: [SwaggerType k]
376471swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
377472
378−swaggerParamTypes :: [SwaggerType ParamOtherSchema]
473+swaggerParamTypes :: [SwaggerType 'SwaggerKindParamOtherSchema]
379474swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
380475
381−swaggerSchemaTypes :: [SwaggerType Schema]
476+swaggerSchemaTypes :: [SwaggerType 'SwaggerKindSchema]
382477swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
383478
384479swaggerTypeConstrs :: [Constr]
385−swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType Schema])
480+swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType 'SwaggerKindSchema])
386481 ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
387482
388−instance {-# OVERLAPPABLE #-} Typeable t => Data (SwaggerType t) where
483+instance Typeable t => Data (SwaggerType ('SwaggerKindNormal t)) where
389484 gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
390485 toConstr = swaggerTypeConstr
391486 dataTypeOf = swaggerTypeDataType
392487
393−instance {-# OVERLAPPING #-} Data (SwaggerType ParamOtherSchema) where
488+instance Data (SwaggerType 'SwaggerKindParamOtherSchema) where
394489 gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
395490 toConstr = swaggerTypeConstr
396491 dataTypeOf = swaggerTypeDataType
397492
398−instance {-# OVERLAPPING #-} Data (SwaggerType Schema) where
493+instance Data (SwaggerType 'SwaggerKindSchema) where
399494 gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
400495 toConstr = swaggerTypeConstr
401496 dataTypeOf = swaggerTypeDataType
@@ -435,11 +530,11 @@ data CollectionFormat t where
435530 -- Corresponds to multiple parameter instances
436531 -- instead of multiple values for a single instance @foo=bar&foo=baz@.
437532 -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
438− CollectionMulti :: CollectionFormat Param
533+ CollectionMulti :: CollectionFormat 'SwaggerKindParamOtherSchema
534+ deriving (Typeable)
439535
440536deriving instance Eq (CollectionFormat t)
441537deriving instance Show (CollectionFormat t)
442−deriving instance Typeable (CollectionFormat t)
443538
444539collectionFormatConstr :: CollectionFormat t -> Constr
445540collectionFormatConstr cf = mkConstr collectionFormatDataType (show cf) [] Prefix
@@ -451,12 +546,12 @@ collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat" $
451546collectionCommonFormats :: [CollectionFormat t]
452547collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
453548
454−instance {-# OVERLAPPABLE #-} Data t => Data (CollectionFormat t) where
549+instance Data t => Data (CollectionFormat ('SwaggerKindNormal t)) where
455550 gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
456551 toConstr = collectionFormatConstr
457552 dataTypeOf _ = collectionFormatDataType
458553
459−deriving instance {-# OVERLAPPING #-} Data (CollectionFormat Param)
554+deriving instance Data (CollectionFormat 'SwaggerKindParamOtherSchema)
460555
461556type ParamName = Text
462557
@@ -465,9 +560,9 @@ data Schema = Schema
465560 , _schemaDescription :: Maybe Text
466561 , _schemaRequired :: [ParamName]
467562
468− , _schemaAllOf :: Maybe [Schema]
469− , _schemaProperties :: HashMap Text (Referenced Schema)
470− , _schemaAdditionalProperties :: Maybe Schema
563+ , _schemaAllOf :: Maybe [Referenced Schema]
564+ , _schemaProperties :: InsOrdHashMap Text (Referenced Schema)
565+ , _schemaAdditionalProperties :: Maybe AdditionalProperties
471566
472567 , _schemaDiscriminator :: Maybe Text
473568 , _schemaReadOnly :: Maybe Bool
@@ -478,10 +573,20 @@ data Schema = Schema
478573 , _schemaMaxProperties :: Maybe Integer
479574 , _schemaMinProperties :: Maybe Integer
480575
481− , _schemaParamSchema :: ParamSchema Schema
576+ , _schemaParamSchema :: ParamSchema 'SwaggerKindSchema
482577 } deriving (Eq, Show, Generic, Data, Typeable)
483578
484−data ParamSchema t = ParamSchema
579+-- | A @'Schema'@ with an optional name.
580+-- This name can be used in references.
581+data NamedSchema = NamedSchema
582+ { _namedSchemaName :: Maybe Text
583+ , _namedSchemaSchema :: Schema
584+ } deriving (Eq, Show, Generic, Data, Typeable)
585+
586+-- | Regex pattern for @string@ type.
587+type Pattern = Text
588+
589+data ParamSchema (t :: SwaggerKind *) = ParamSchema
485590 { -- | Declares the value of the parameter that the server will use if none is provided,
486591 -- for example a @"count"@ to control the number of results per page might default to @100@
487592 -- if not supplied by the client in the request.
@@ -489,7 +594,7 @@ data ParamSchema t = ParamSchema
489594 -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
490595 _paramSchemaDefault :: Maybe Value
491596
492− , _paramSchemaType :: SwaggerType t
597+ , _paramSchemaType :: Maybe (SwaggerType t)
493598 , _paramSchemaFormat :: Maybe Format
494599 , _paramSchemaItems :: Maybe (SwaggerItems t)
495600 , _paramSchemaMaximum :: Maybe Scientific
@@ -498,7 +603,7 @@ data ParamSchema t = ParamSchema
498603 , _paramSchemaExclusiveMinimum :: Maybe Bool
499604 , _paramSchemaMaxLength :: Maybe Integer
500605 , _paramSchemaMinLength :: Maybe Integer
501− , _paramSchemaPattern :: Maybe Text
606+ , _paramSchemaPattern :: Maybe Pattern
502607 , _paramSchemaMaxItems :: Maybe Integer
503608 , _paramSchemaMinItems :: Maybe Integer
504609 , _paramSchemaUniqueItems :: Maybe Bool
@@ -506,11 +611,11 @@ data ParamSchema t = ParamSchema
506611 , _paramSchemaMultipleOf :: Maybe Scientific
507612 } deriving (Eq, Show, Generic, Typeable)
508613
509−deriving instance (Data t, Data (SwaggerType t), Data (SwaggerItems t)) => Data (ParamSchema t)
614+deriving instance (Typeable k, Data (Maybe (SwaggerType k)), Data (SwaggerItems k)) => Data (ParamSchema k)
510615
511616data Xml = Xml
512617 { -- | Replaces the name of the element/attribute used for the described schema property.
513− -- When defined within the @'Items'@ (items), it will affect the name of the individual XML elements within the list.
618+ -- When defined within the @'SwaggerItems'@ (items), it will affect the name of the individual XML elements within the list.
514619 -- When defined alongside type being array (outside the items),
515620 -- it will affect the wrapping element and only if wrapped is true.
516621 -- If wrapped is false, it will be ignored.
@@ -536,14 +641,6 @@ data Xml = Xml
536641 , _xmlWrapped :: Maybe Bool
537642 } deriving (Eq, Show, Generic, Data, Typeable)
538643
539−data Items = Items
540− { -- | Determines the format of the array if type array is used.
541− -- Default value is @'ItemsCollectionCSV'@.
542− _itemsCollectionFormat :: Maybe (CollectionFormat Items)
543−
544− , _itemsParamSchema :: ParamSchema Items
545− } deriving (Eq, Show, Generic, Data, Typeable)
546−
547644-- | A container for the expected responses of an operation.
548645-- The container maps a HTTP response code to the expected response.
549646-- It is not expected from the documentation to necessarily cover all possible HTTP response codes,
@@ -556,7 +653,7 @@ data Responses = Responses
556653
557654 -- | Any HTTP status code can be used as the property name (one property per HTTP status code).
558655 -- Describes the expected response for those HTTP status codes.
559− , _responsesResponses :: HashMap HttpStatusCode (Referenced Response)
656+ , _responsesResponses :: InsOrdHashMap HttpStatusCode (Referenced Response)
560657 } deriving (Eq, Show, Generic, Data, Typeable)
561658
562659type HttpStatusCode = Int
@@ -575,23 +672,22 @@ data Response = Response
575672 , _responseSchema :: Maybe (Referenced Schema)
576673
577674 -- | A list of headers that are sent with the response.
578− , _responseHeaders :: HashMap HeaderName Header
675+ , _responseHeaders :: InsOrdHashMap HeaderName Header
579676
580677 -- | An example of the response message.
581678 , _responseExamples :: Maybe Example
582679 } deriving (Eq, Show, Generic, Data, Typeable)
583680
681+instance IsString Response where
682+ fromString s = Response (fromString s) Nothing mempty Nothing
683+
584684type HeaderName = Text
585685
586686data Header = Header
587687 { -- | A short description of the header.
588688 _headerDescription :: Maybe Text
589689
590− -- | Determines the format of the array if type array is used.
591− -- Default value is @'ItemsCollectionCSV'@.
592− , _headerCollectionFormat :: Maybe (CollectionFormat Items)
593−
594− , _headerParamSchema :: ParamSchema Header
690+ , _headerParamSchema :: ParamSchema ('SwaggerKindNormal Header)
595691 } deriving (Eq, Show, Generic, Data, Typeable)
596692
597693data Example = Example { getExample :: Map MediaType Value }
@@ -642,7 +738,7 @@ data OAuth2Params = OAuth2Params
642738 _oauth2Flow :: OAuth2Flow
643739
644740 -- | The available scopes for the OAuth2 security scheme.
645− , _oauth2Scopes :: HashMap Text Text
741+ , _oauth2Scopes :: InsOrdHashMap Text Text
646742 } deriving (Eq, Show, Generic, Data, Typeable)
647743
648744data SecuritySchemeType
@@ -659,18 +755,37 @@ data SecurityScheme = SecurityScheme
659755 , _securitySchemeDescription :: Maybe Text
660756 } deriving (Eq, Show, Generic, Data, Typeable)
661757
758+
759+-- | merge scopes of two OAuth2 security schemes when their flows are identical.
760+-- In other case returns first security scheme
761+mergeSecurityScheme :: SecurityScheme -> SecurityScheme -> SecurityScheme
762+mergeSecurityScheme s1@(SecurityScheme (SecuritySchemeOAuth2 (OAuth2Params flow1 scopes1)) desc)
763+ s2@(SecurityScheme (SecuritySchemeOAuth2 (OAuth2Params flow2 scopes2)) _)
764+ = if flow1 == flow2 then
765+ SecurityScheme (SecuritySchemeOAuth2 (OAuth2Params flow1 (scopes1 <> scopes2))) desc
766+ else
767+ s1
768+mergeSecurityScheme s1 _ = s1
769+
770+newtype SecurityDefinitions
771+ = SecurityDefinitions (Definitions SecurityScheme)
772+ deriving (Eq, Show, Generic, Data, Typeable)
773+
662774-- | Lists the required security schemes to execute this operation.
663775-- The object can have multiple security schemes declared in it which are all required
664776-- (that is, there is a logical AND between the schemes).
665777newtype SecurityRequirement = SecurityRequirement
666− { getSecurityRequirement :: HashMap Text [Text]
667− } deriving (Eq, Read, Show, Monoid, ToJSON, FromJSON, Data, Typeable)
778+ { getSecurityRequirement :: InsOrdHashMap Text [Text]
779+ } deriving (Eq, Read, Show, Semigroup, Monoid, ToJSON, FromJSON, Data, Typeable)
780+
781+-- | Tag name.
782+type TagName = Text
668783
669784-- | Allows adding meta data to a single tag that is used by @Operation@.
670785-- It is not mandatory to have a @Tag@ per tag used there.
671786data Tag = Tag
672787 { -- | The name of the tag.
673− _tagName :: Text
788+ _tagName :: TagName
674789
675790 -- | A short description for the tag.
676791 -- GFM syntax can be used for rich text representation.
@@ -678,7 +793,12 @@ data Tag = Tag
678793
679794 -- | Additional external documentation for this tag.
680795 , _tagExternalDocs :: Maybe ExternalDocs
681− } deriving (Eq, Show, Generic, Data, Typeable)
796+ } deriving (Eq, Ord, Show, Generic, Data, Typeable)
797+
798+instance Hashable Tag
799+
800+instance IsString Tag where
801+ fromString s = Tag (fromString s) Nothing Nothing
682802
683803-- | Allows referencing an external resource for extended documentation.
684804data ExternalDocs = ExternalDocs
@@ -688,7 +808,9 @@ data ExternalDocs = ExternalDocs
688808
689809 -- | The URL for the target documentation.
690810 , _externalDocsUrl :: URL
691− } deriving (Eq, Show, Generic, Data, Typeable)
811+ } deriving (Eq, Ord, Show, Generic, Data, Typeable)
812+
813+instance Hashable ExternalDocs
692814
693815-- | A simple object to allow referencing other definitions in the specification.
694816-- It can be used to reference parameters and responses that are defined at the top level for reuse.
@@ -698,68 +820,122 @@ newtype Reference = Reference { getReference :: Text }
698820data Referenced a
699821 = Ref Reference
700822 | Inline a
701− deriving (Eq, Show, Data, Typeable)
823+ deriving (Eq, Show, Functor, Data, Typeable)
824+
825+instance IsString a => IsString (Referenced a) where
826+ fromString = Inline . fromString
827+
828+newtype URL = URL { getUrl :: Text } deriving (Eq, Ord, Show, Hashable, ToJSON, FromJSON, Data, Typeable)
702829
703−newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON, Data, Typeable)
830+data AdditionalProperties
831+ = AdditionalPropertiesAllowed Bool
832+ | AdditionalPropertiesSchema (Referenced Schema)
833+ deriving (Eq, Show, Data, Typeable)
704834
705835-- =======================================================================
706836-- Monoid instances
707837-- =======================================================================
708838
839+instance Semigroup Swagger where
840+ (<>) = genericMappend
709841instance Monoid Swagger where
710842 mempty = genericMempty
711− mappend = genericMappend
843+ mappend = (<>)
712844
845+instance Semigroup Info where
846+ (<>) = genericMappend
713847instance Monoid Info where
714848 mempty = genericMempty
715− mappend = genericMappend
849+ mappend = (<>)
716850
717−instance Monoid Paths where
851+instance Semigroup Contact where
852+ (<>) = genericMappend
853+instance Monoid Contact where
718854 mempty = genericMempty
719− mappend = genericMappend
855+ mappend = (<>)
720856
857+instance Semigroup PathItem where
858+ (<>) = genericMappend
721859instance Monoid PathItem where
722860 mempty = genericMempty
723− mappend = genericMappend
861+ mappend = (<>)
724862
863+instance Semigroup Schema where
864+ (<>) = genericMappend
725865instance Monoid Schema where
726866 mempty = genericMempty
727− mappend = genericMappend
867+ mappend = (<>)
728868
869+instance Semigroup (ParamSchema t) where
870+ (<>) = genericMappend
729871instance Monoid (ParamSchema t) where
730872 mempty = genericMempty
731− mappend = genericMappend
873+ mappend = (<>)
732874
875+instance Semigroup Param where
876+ (<>) = genericMappend
733877instance Monoid Param where
734878 mempty = genericMempty
735− mappend = genericMappend
879+ mappend = (<>)
736880
881+instance Semigroup ParamOtherSchema where
882+ (<>) = genericMappend
737883instance Monoid ParamOtherSchema where
738884 mempty = genericMempty
739− mappend = genericMappend
885+ mappend = (<>)
886+
887+instance Semigroup Header where
888+ (<>) = genericMappend
889+instance Monoid Header where
890+ mempty = genericMempty
891+ mappend = (<>)
740892
893+instance Semigroup Responses where
894+ (<>) = genericMappend
741895instance Monoid Responses where
742896 mempty = genericMempty
743− mappend = genericMappend
897+ mappend = (<>)
744898
899+instance Semigroup Response where
900+ (<>) = genericMappend
745901instance Monoid Response where
746902 mempty = genericMempty
747− mappend = genericMappend
903+ mappend = (<>)
748904
905+instance Semigroup ExternalDocs where
906+ (<>) = genericMappend
749907instance Monoid ExternalDocs where
750908 mempty = genericMempty
751− mappend = genericMappend
909+ mappend = (<>)
752910
911+instance Semigroup Operation where
912+ (<>) = genericMappend
753913instance Monoid Operation where
754914 mempty = genericMempty
755− mappend = genericMappend
915+ mappend = (<>)
916+
917+instance Semigroup Example where
918+ (<>) = genericMappend
919+instance Monoid Example where
920+ mempty = genericMempty
921+ mappend = (<>)
922+
923+instance Semigroup SecurityScheme where
924+ (<>) = mergeSecurityScheme
925+
926+instance Semigroup SecurityDefinitions where
927+ (SecurityDefinitions sd1) <> (SecurityDefinitions sd2) =
928+ SecurityDefinitions $ InsOrdHashMap.unionWith (<>) sd1 sd2
929+
930+instance Monoid SecurityDefinitions where
931+ mempty = SecurityDefinitions $ InsOrdHashMap.empty
932+ mappend = (<>)
756933
757934-- =======================================================================
758935-- SwaggerMonoid helper instances
759936-- =======================================================================
760937
761938instance SwaggerMonoid Info
762−instance SwaggerMonoid Paths
763939instance SwaggerMonoid PathItem
764940instance SwaggerMonoid Schema
765941instance SwaggerMonoid (ParamSchema t)
@@ -769,6 +945,8 @@ instance SwaggerMonoid Responses
769945instance SwaggerMonoid Response
770946instance SwaggerMonoid ExternalDocs
771947instance SwaggerMonoid Operation
948+instance SwaggerMonoid SecurityDefinitions
949+instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)
772950
773951instance SwaggerMonoid MimeList
774952deriving instance SwaggerMonoid URL
@@ -781,64 +959,85 @@ instance SwaggerMonoid ParamLocation where
781959 swaggerMempty = ParamQuery
782960 swaggerMappend _ y = y
783961
784−instance SwaggerMonoid (HashMap Text Schema) where
785− swaggerMempty = HashMap.empty
786− swaggerMappend = HashMap.unionWith mappend
787−
788−instance SwaggerMonoid (HashMap Text (Referenced Schema)) where
789− swaggerMempty = HashMap.empty
790− swaggerMappend = HashMap.unionWith swaggerMappend
962+instance OVERLAPPING_ SwaggerMonoid (InsOrdHashMap FilePath PathItem) where
963+ swaggerMempty = InsOrdHashMap.empty
964+ swaggerMappend = InsOrdHashMap.unionWith mappend
791965
792966instance Monoid a => SwaggerMonoid (Referenced a) where
793967 swaggerMempty = Inline mempty
794− swaggerMappend (Inline x) (Inline y) = Inline (x <> y)
968+ swaggerMappend (Inline x) (Inline y) = Inline (mappend x y)
795969 swaggerMappend _ y = y
796970
797−instance SwaggerMonoid (HashMap Text Param) where
798− swaggerMempty = HashMap.empty
799− swaggerMappend = HashMap.unionWith mappend
971+instance SwaggerMonoid ParamAnySchema where
972+ swaggerMempty = ParamOther swaggerMempty
973+ swaggerMappend (ParamBody x) (ParamBody y) = ParamBody (swaggerMappend x y)
974+ swaggerMappend (ParamOther x) (ParamOther y) = ParamOther (swaggerMappend x y)
975+ swaggerMappend _ y = y
800976
801−instance SwaggerMonoid (HashMap Text Response) where
802− swaggerMempty = HashMap.empty
803− swaggerMappend = flip HashMap.union
977+-- =======================================================================
978+-- Simple Generic-based ToJSON instances
979+-- =======================================================================
804980
805−instance SwaggerMonoid (HashMap Text SecurityScheme) where
806− swaggerMempty = HashMap.empty
807− swaggerMappend = flip HashMap.union
981+instance ToJSON ParamLocation where
982+ toJSON = genericToJSON (jsonPrefix "Param")
808983
809−instance SwaggerMonoid (HashMap FilePath PathItem) where
810− swaggerMempty = HashMap.empty
811− swaggerMappend = HashMap.unionWith mappend
984+instance ToJSON Info where
985+ toJSON = genericToJSON (jsonPrefix "Info")
812986
813−instance SwaggerMonoid (HashMap HeaderName Header) where
814− swaggerMempty = HashMap.empty
815− swaggerMappend = flip HashMap.union
987+instance ToJSON Contact where
988+ toJSON = genericToJSON (jsonPrefix "Contact")
816989
817−instance SwaggerMonoid (HashMap HttpStatusCode (Referenced Response)) where
818− swaggerMempty = HashMap.empty
819− swaggerMappend = flip HashMap.union
990+instance ToJSON License where
991+ toJSON = genericToJSON (jsonPrefix "License")
820992
821−instance SwaggerMonoid ParamAnySchema where
822− swaggerMempty = ParamOther swaggerMempty
823− swaggerMappend (ParamBody x) (ParamBody y) = ParamBody (swaggerMappend x y)
824− swaggerMappend (ParamOther x) (ParamOther y) = ParamOther (swaggerMappend x y)
825− swaggerMappend _ y = y
993+instance ToJSON ApiKeyLocation where
994+ toJSON = genericToJSON (jsonPrefix "ApiKey")
995+
996+instance ToJSON ApiKeyParams where
997+ toJSON = genericToJSON (jsonPrefix "apiKey")
998+
999+instance ToJSON Scheme where
1000+ toJSON = genericToJSON (jsonPrefix "")
1001+
1002+instance ToJSON Tag where
1003+ toJSON = genericToJSON (jsonPrefix "Tag")
1004+
1005+instance ToJSON ExternalDocs where
1006+ toJSON = genericToJSON (jsonPrefix "ExternalDocs")
1007+
1008+instance ToJSON Xml where
1009+ toJSON = genericToJSON (jsonPrefix "Xml")
8261010
8271011-- =======================================================================
828−-- TH derived ToJSON and FromJSON instances
1012+-- Simple Generic-based FromJSON instances
8291013-- =======================================================================
8301014
831−deriveJSON (jsonPrefix "Param") ''ParamLocation
832−deriveJSON' ''Info
833−deriveJSON' ''Contact
834−deriveJSON' ''License
835−deriveJSON (jsonPrefix "ApiKey") ''ApiKeyLocation
836−deriveJSON (jsonPrefix "apiKey") ''ApiKeyParams
837−deriveJSONDefault ''Scheme
838−deriveJSON' ''Tag
839−deriveJSON' ''ExternalDocs
1015+instance FromJSON ParamLocation where
1016+ parseJSON = genericParseJSON (jsonPrefix "Param")
1017+
1018+instance FromJSON Info where
1019+ parseJSON = genericParseJSON (jsonPrefix "Info")
1020+
1021+instance FromJSON Contact where
1022+ parseJSON = genericParseJSON (jsonPrefix "Contact")
1023+
1024+instance FromJSON License where
1025+ parseJSON = genericParseJSON (jsonPrefix "License")
1026+
1027+instance FromJSON ApiKeyLocation where
1028+ parseJSON = genericParseJSON (jsonPrefix "ApiKey")
1029+
1030+instance FromJSON ApiKeyParams where
1031+ parseJSON = genericParseJSON (jsonPrefix "apiKey")
1032+
1033+instance FromJSON Scheme where
1034+ parseJSON = genericParseJSON (jsonPrefix "")
8401035
841−deriveToJSON' ''Xml
1036+instance FromJSON Tag where
1037+ parseJSON = genericParseJSON (jsonPrefix "Tag")
1038+
1039+instance FromJSON ExternalDocs where
1040+ parseJSON = genericParseJSON (jsonPrefix "ExternalDocs")
8421041
8431042-- =======================================================================
8441043-- Manual ToJSON instances
@@ -860,7 +1059,8 @@ instance ToJSON OAuth2Flow where
8601059 , "tokenUrl" .= tokenUrl ]
8611060
8621061instance ToJSON OAuth2Params where
863− toJSON = omitEmpties . genericToJSONWithSub "flow" (jsonPrefix "oauth2")
1062+ toJSON = sopSwaggerGenericToJSON
1063+ DEFINE_TOENCODING
8641064
8651065instance ToJSON SecuritySchemeType where
8661066 toJSON SecuritySchemeBasic
@@ -873,27 +1073,38 @@ instance ToJSON SecuritySchemeType where
8731073 <+> object [ "type" .= ("oauth2" :: Text) ]
8741074
8751075instance ToJSON Swagger where
876− toJSON = omitEmpties . addVersion . genericToJSON (jsonPrefix "")
877− where
878− addVersion (Object o) = Object (HashMap.insert "swagger" "2.0" o)
879− addVersion _ = error "impossible"
1076+ toJSON = sopSwaggerGenericToJSON
1077+ DEFINE_TOENCODING
8801078
8811079instance ToJSON SecurityScheme where
882− toJSON = genericToJSONWithSub "type" (jsonPrefix "securityScheme")
1080+ toJSON = sopSwaggerGenericToJSON
1081+ DEFINE_TOENCODING
8831082
8841083instance ToJSON Schema where
885− toJSON = omitEmpties . genericToJSONWithSub "paramSchema" (jsonPrefix "schema")
1084+ toJSON = sopSwaggerGenericToJSON
1085+ DEFINE_TOENCODING
8861086
8871087instance ToJSON Header where
888− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "header")
889−
890−instance ToJSON Items where
891− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "items")
892−
893−instance ToJSON (SwaggerItems t) where
894− toJSON (SwaggerItemsPrimitive x) = toJSON x
895− toJSON (SwaggerItemsObject x) = toJSON x
896− toJSON (SwaggerItemsArray x) = toJSON x
1088+ toJSON = sopSwaggerGenericToJSON
1089+ DEFINE_TOENCODING
1090+
1091+-- | As for nullary schema for 0-arity type constructors, see
1092+-- <https://github.com/GetShopTV/swagger2/issues/167>.
1093+--
1094+-- >>> encode (SwaggerItemsArray [])
1095+-- "{\"example\":[],\"items\":{},\"maxItems\":0}"
1096+--
1097+instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
1098+ toJSON (SwaggerItemsPrimitive fmt schema) = object
1099+ [ "collectionFormat" .= fmt
1100+ , "items" .= schema ]
1101+ toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
1102+ toJSON (SwaggerItemsArray []) = object
1103+ [ "items" .= object []
1104+ , "maxItems" .= (0 :: Int)
1105+ , "example" .= Array mempty
1106+ ]
1107+ toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
8971108
8981109instance ToJSON Host where
8991110 toJSON (Host host mport) = toJSON $
@@ -901,38 +1112,43 @@ instance ToJSON Host where
9011112 Nothing -> host
9021113 Just port -> host ++ ":" ++ show port
9031114
904−instance ToJSON Paths where
905− toJSON (Paths m) = toJSON m
906−
9071115instance ToJSON MimeList where
9081116 toJSON (MimeList xs) = toJSON (map show xs)
9091117
9101118instance ToJSON Param where
911− toJSON = genericToJSONWithSub "schema" (jsonPrefix "param")
1119+ toJSON = sopSwaggerGenericToJSON
1120+ DEFINE_TOENCODING
9121121
9131122instance ToJSON ParamAnySchema where
9141123 toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
9151124 toJSON (ParamOther s) = toJSON s
9161125
9171126instance ToJSON ParamOtherSchema where
918− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
1127+ toJSON = sopSwaggerGenericToJSON
1128+ DEFINE_TOENCODING
9191129
9201130instance ToJSON Responses where
921− toJSON (Responses def rs) = omitEmpties $
922− toJSON (hashMapMapKeys show rs) <+> object [ "default" .= def ]
1131+ toJSON = sopSwaggerGenericToJSON
1132+ DEFINE_TOENCODING
9231133
9241134instance ToJSON Response where
925− toJSON = omitEmpties . genericToJSON (jsonPrefix "response")
1135+ toJSON = sopSwaggerGenericToJSON
1136+ DEFINE_TOENCODING
9261137
9271138instance ToJSON Operation where
928− toJSON = omitEmpties . genericToJSON (jsonPrefix "operation")
1139+ toJSON = sopSwaggerGenericToJSON
1140+ DEFINE_TOENCODING
9291141
9301142instance ToJSON PathItem where
931− toJSON = omitEmpties . genericToJSON (jsonPrefix "pathItem")
1143+ toJSON = sopSwaggerGenericToJSON
1144+ DEFINE_TOENCODING
9321145
9331146instance ToJSON Example where
9341147 toJSON = toJSON . Map.mapKeys show . getExample
9351148
1149+instance ToJSON SecurityDefinitions where
1150+ toJSON (SecurityDefinitions sd) = toJSON sd
1151+
9361152instance ToJSON Reference where
9371153 toJSON (Reference ref) = object [ "$ref" .= ref ]
9381154
@@ -961,8 +1177,14 @@ instance ToJSON (CollectionFormat t) where
9611177 toJSON CollectionPipes = "pipes"
9621178 toJSON CollectionMulti = "multi"
9631179
964−instance ToJSON (ParamSchema t) where
965− toJSON = genericToJSON (jsonPrefix "paramSchema")
1180+instance ToJSON (ParamSchema k) where
1181+ -- TODO: this is a bit fishy, why we need sub object only in `ToJSON`?
1182+ toJSON = sopSwaggerGenericToJSONWithOpts $
1183+ mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1184+
1185+instance ToJSON AdditionalProperties where
1186+ toJSON (AdditionalPropertiesAllowed b) = toJSON b
1187+ toJSON (AdditionalPropertiesSchema s) = toJSON s
9661188
9671189-- =======================================================================
9681190-- Manual FromJSON instances
@@ -982,7 +1204,7 @@ instance FromJSON OAuth2Flow where
9821204 parseJSON _ = empty
9831205
9841206instance FromJSON OAuth2Params where
985− parseJSON = genericParseJSONWithSub "flow" (jsonPrefix "oauth2")
1207+ parseJSON = sopSwaggerGenericParseJSON
9861208
9871209instance FromJSON SecuritySchemeType where
9881210 parseJSON js@(Object o) = do
@@ -995,61 +1217,65 @@ instance FromJSON SecuritySchemeType where
9951217 parseJSON _ = empty
9961218
9971219instance FromJSON Swagger where
998− parseJSON js@(Object o) = do
999− (version :: Text) <- o .: "swagger"
1000− when (version /= "2.0") empty
1001− (genericParseJSON (jsonPrefix "")
1002− `withDefaults` [ "consumes" .= (mempty :: MimeList)
1003− , "produces" .= (mempty :: MimeList)
1004− , "security" .= ([] :: [SecurityRequirement])
1005− , "tags" .= ([] :: [Tag])
1006− , "definitions" .= (mempty :: HashMap Text Schema)
1007− , "parameters" .= (mempty :: HashMap Text Param)
1008− , "responses" .= (mempty :: HashMap Text Response)
1009− , "securityDefinitions" .= (mempty :: HashMap Text SecurityScheme)
1010− ] ) js
1011− parseJSON _ = empty
1220+ parseJSON = sopSwaggerGenericParseJSON
10121221
10131222instance FromJSON SecurityScheme where
1014− parseJSON = genericParseJSONWithSub "type" (jsonPrefix "securityScheme")
1223+ parseJSON = sopSwaggerGenericParseJSON
10151224
10161225instance FromJSON Schema where
1017− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "schema")
1018− `withDefaults` [ "properties" .= (mempty :: HashMap Text Schema)
1019− , "required" .= ([] :: [ParamName]) ]
1226+ parseJSON = fmap nullaryCleanup . sopSwaggerGenericParseJSON
1227+ where nullaryCleanup :: Schema -> Schema
1228+ nullaryCleanup s@Schema{_schemaParamSchema=ps} =
1229+ if _paramSchemaItems ps == Just (SwaggerItemsArray [])
1230+ then s { _schemaExample = Nothing
1231+ , _schemaParamSchema = ps { _paramSchemaMaxItems = Nothing } }
1232+ else s
10201233
10211234instance FromJSON Header where
1022− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "header")
1023−
1024−instance FromJSON Items where
1025− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "items")
1026−
1027−instance {-# OVERLAPPABLE #-} FromJSON (SwaggerItems t) where
1028− parseJSON js = SwaggerItemsPrimitive <$> parseJSON js
1029−
1030−instance {-# OVERLAPPING #-} FromJSON (SwaggerItems Schema) where
1031− parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1235+ parseJSON = sopSwaggerGenericParseJSON
1236+
1237+instance (FromJSON (CollectionFormat ('SwaggerKindNormal t)), FromJSON (ParamSchema ('SwaggerKindNormal t))) => FromJSON (SwaggerItems ('SwaggerKindNormal t)) where
1238+ parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1239+ <$> o .:? "collectionFormat"
1240+ <*> (o .: "items" >>= parseJSON)
1241+
1242+instance FromJSON (SwaggerItems 'SwaggerKindParamOtherSchema) where
1243+ parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1244+ <$> o .:? "collectionFormat"
1245+ <*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))
1246+
1247+-- |
1248+--
1249+-- >>> decode "{}" :: Maybe (SwaggerItems 'SwaggerKindSchema)
1250+-- Just (SwaggerItemsArray [])
1251+--
1252+-- >>> eitherDecode "{\"$ref\":\"#/definitions/example\"}" :: Either String (SwaggerItems 'SwaggerKindSchema)
1253+-- Right (SwaggerItemsObject (Ref (Reference {getReference = "example"})))
1254+--
1255+-- >>> eitherDecode "[{\"$ref\":\"#/definitions/example\"}]" :: Either String (SwaggerItems 'SwaggerKindSchema)
1256+-- Right (SwaggerItemsArray [Ref (Reference {getReference = "example"})])
1257+--
1258+instance FromJSON (SwaggerItems 'SwaggerKindSchema) where
1259+ parseJSON js@(Object obj)
1260+ | null obj = pure $ SwaggerItemsArray [] -- Nullary schema.
1261+ | otherwise = SwaggerItemsObject <$> parseJSON js
10321262 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
10331263 parseJSON _ = empty
10341264
10351265instance FromJSON Host where
1036− parseJSON (String s) =
1037− case fromInteger <$> readMaybe portStr of
1038− Nothing | not (null portStr) -> fail $ "Invalid port `" ++ portStr ++ "'"
1039− mport -> pure $ Host host mport
1040− where
1041− (hostText, portText) = Text.breakOn ":" s
1042− [host, portStr] = map Text.unpack [hostText, portText]
1266+ parseJSON (String s) = case map Text.unpack $ Text.split (== ':') s of
1267+ [host] -> return $ Host host Nothing
1268+ [host, port] -> case readMaybe port of
1269+ Nothing -> fail $ "Invalid port `" ++ port ++ "'"
1270+ Just p -> return $ Host host (Just (fromInteger p))
1271+ _ -> fail $ "Invalid host `" ++ Text.unpack s ++ "'"
10431272 parseJSON _ = empty
10441273
1045−instance FromJSON Paths where
1046− parseJSON js = Paths <$> parseJSON js
1047−
10481274instance FromJSON MimeList where
10491275 parseJSON js = (MimeList . map fromString) <$> parseJSON js
10501276
10511277instance FromJSON Param where
1052− parseJSON = genericParseJSONWithSub "schema" (jsonPrefix "param")
1278+ parseJSON = sopSwaggerGenericParseJSON
10531279
10541280instance FromJSON ParamAnySchema where
10551281 parseJSON js@(Object o) = do
@@ -1062,12 +1288,12 @@ instance FromJSON ParamAnySchema where
10621288 parseJSON _ = empty
10631289
10641290instance FromJSON ParamOtherSchema where
1065− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
1291+ parseJSON = sopSwaggerGenericParseJSON
10661292
10671293instance FromJSON Responses where
10681294 parseJSON (Object o) = Responses
10691295 <$> o .:? "default"
1070− <*> (parseJSON (Object (HashMap.delete "default" o)) >>= hashMapReadKeys)
1296+ <*> (parseJSON (Object (HashMap.delete "default" o)))
10711297 parseJSON _ = empty
10721298
10731299instance FromJSON Example where
@@ -1076,16 +1302,16 @@ instance FromJSON Example where
10761302 pure $ Example (Map.mapKeys fromString m)
10771303
10781304instance FromJSON Response where
1079− parseJSON = genericParseJSON (jsonPrefix "response")
1080− `withDefaults` [ "headers" .= (mempty :: HashMap HeaderName Header) ]
1305+ parseJSON = sopSwaggerGenericParseJSON
10811306
10821307instance FromJSON Operation where
1083− parseJSON = genericParseJSON (jsonPrefix "operation")
1084− `withDefaults` [ "security" .= ([] :: [SecurityRequirement]) ]
1308+ parseJSON = sopSwaggerGenericParseJSON
10851309
10861310instance FromJSON PathItem where
1087− parseJSON = genericParseJSON (jsonPrefix "pathItem")
1088− `withDefaults` [ "parameters" .= ([] :: [Param]) ]
1311+ parseJSON = sopSwaggerGenericParseJSON
1312+
1313+instance FromJSON SecurityDefinitions where
1314+ parseJSON js = SecurityDefinitions <$> parseJSON js
10891315
10901316instance FromJSON Reference where
10911317 parseJSON (Object o) = Reference <$> o .: "$ref"
@@ -1102,6 +1328,7 @@ referencedParseJSON prefix js@(Object o) = do
11021328 case Text.stripPrefix prefix s of
11031329 Nothing -> fail $ "expected $ref of the form \"" <> Text.unpack prefix <> "*\", but got " <> show s
11041330 Just suffix -> pure (Reference suffix)
1331+referencedParseJSON _ _ = fail "referenceParseJSON: not an object"
11051332
11061333instance FromJSON (Referenced Schema) where parseJSON = referencedParseJSON "#/definitions/"
11071334instance FromJSON (Referenced Param) where parseJSON = referencedParseJSON "#/parameters/"
@@ -1110,25 +1337,91 @@ instance FromJSON (Referenced Response) where parseJSON = referencedParseJSON "#
11101337instance FromJSON Xml where
11111338 parseJSON = genericParseJSON (jsonPrefix "xml")
11121339
1113−instance FromJSON (SwaggerType Schema) where
1340+instance FromJSON (SwaggerType 'SwaggerKindSchema) where
11141341 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
11151342
1116−instance FromJSON (SwaggerType ParamOtherSchema) where
1343+instance FromJSON (SwaggerType 'SwaggerKindParamOtherSchema) where
11171344 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerFile]
11181345
1119−instance {-# OVERLAPPABLE #-} FromJSON (SwaggerType t) where
1346+instance FromJSON (SwaggerType ('SwaggerKindNormal t)) where
11201347 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
11211348
1122−instance FromJSON (CollectionFormat Param) where
1123− parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
1124−
1125−instance FromJSON (CollectionFormat Items) where
1349+instance FromJSON (CollectionFormat ('SwaggerKindNormal t)) where
11261350 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
11271351
1128−-- NOTE: The constraints @FromJSON (SwaggerType t)@ and
1129−-- @FromJSON (SwaggerItems t)@ are necessary here!
1130−-- Without the constraint the general instance will be used
1131−-- that only accepts common types (i.e. NOT object, null or file)
1132−-- and primitive array items.
1133−instance (FromJSON (SwaggerType t), FromJSON (SwaggerItems t)) => FromJSON (ParamSchema t) where
1134− parseJSON = genericParseJSON (jsonPrefix "ParamSchema")
1352+-- NOTE: There aren't collections of 'Schema'
1353+--instance FromJSON (CollectionFormat (SwaggerKindSchema)) where
1354+-- parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1355+
1356+instance FromJSON (CollectionFormat 'SwaggerKindParamOtherSchema) where
1357+ parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
1358+
1359+instance (FromJSON (SwaggerType ('SwaggerKindNormal t)), FromJSON (SwaggerItems ('SwaggerKindNormal t))) => FromJSON (ParamSchema ('SwaggerKindNormal t)) where
1360+ parseJSON = sopSwaggerGenericParseJSON
1361+instance FromJSON (ParamSchema 'SwaggerKindParamOtherSchema) where
1362+ parseJSON = sopSwaggerGenericParseJSON
1363+instance FromJSON (ParamSchema 'SwaggerKindSchema) where
1364+ parseJSON = sopSwaggerGenericParseJSON
1365+
1366+instance FromJSON AdditionalProperties where
1367+ parseJSON (Bool b) = pure $ AdditionalPropertiesAllowed b
1368+ parseJSON js = AdditionalPropertiesSchema <$> parseJSON js
1369+
1370+-------------------------------------------------------------------------------
1371+-- TH splices
1372+-------------------------------------------------------------------------------
1373+
1374+deriveGeneric ''Header
1375+deriveGeneric ''OAuth2Params
1376+deriveGeneric ''Operation
1377+deriveGeneric ''Param
1378+deriveGeneric ''ParamOtherSchema
1379+deriveGeneric ''PathItem
1380+deriveGeneric ''Response
1381+deriveGeneric ''Responses
1382+deriveGeneric ''SecurityScheme
1383+deriveGeneric ''Schema
1384+deriveGeneric ''ParamSchema
1385+deriveGeneric ''Swagger
1386+
1387+instance HasSwaggerAesonOptions Header where
1388+ swaggerAesonOptions _ = mkSwaggerAesonOptions "header" & saoSubObject ?~ "paramSchema"
1389+instance HasSwaggerAesonOptions OAuth2Params where
1390+ swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2" & saoSubObject ?~ "flow"
1391+instance HasSwaggerAesonOptions Operation where
1392+ swaggerAesonOptions _ = mkSwaggerAesonOptions "operation"
1393+instance HasSwaggerAesonOptions Param where
1394+ swaggerAesonOptions _ = mkSwaggerAesonOptions "param" & saoSubObject ?~ "schema"
1395+instance HasSwaggerAesonOptions ParamOtherSchema where
1396+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramOtherSchema" & saoSubObject ?~ "paramSchema"
1397+instance HasSwaggerAesonOptions PathItem where
1398+ swaggerAesonOptions _ = mkSwaggerAesonOptions "pathItem"
1399+instance HasSwaggerAesonOptions Response where
1400+ swaggerAesonOptions _ = mkSwaggerAesonOptions "response"
1401+instance HasSwaggerAesonOptions Responses where
1402+ swaggerAesonOptions _ = mkSwaggerAesonOptions "responses" & saoSubObject ?~ "responses"
1403+instance HasSwaggerAesonOptions SecurityScheme where
1404+ swaggerAesonOptions _ = mkSwaggerAesonOptions "securityScheme" & saoSubObject ?~ "type"
1405+instance HasSwaggerAesonOptions Schema where
1406+ swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject ?~ "paramSchema"
1407+instance HasSwaggerAesonOptions Swagger where
1408+ swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("swagger", "2.0")]
1409+
1410+instance HasSwaggerAesonOptions (ParamSchema ('SwaggerKindNormal t)) where
1411+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1412+instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindParamOtherSchema) where
1413+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1414+-- NOTE: Schema doesn't have 'items' sub object!
1415+instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindSchema) where
1416+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema"
1417+
1418+instance AesonDefaultValue (ParamSchema s)
1419+instance AesonDefaultValue OAuth2Flow
1420+instance AesonDefaultValue Responses
1421+instance AesonDefaultValue ParamAnySchema
1422+instance AesonDefaultValue SecuritySchemeType
1423+instance AesonDefaultValue (SwaggerType a)
1424+instance AesonDefaultValue MimeList where defaultValue = Just mempty
1425+instance AesonDefaultValue Info
1426+instance AesonDefaultValue ParamLocation
1427+instance AesonDefaultValue SecurityDefinitions where defaultValue = Just $ SecurityDefinitions mempty