@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DataKinds #-}
23{-# LANGUAGE DeriveDataTypeable #-}
34{-# LANGUAGE DeriveFunctor #-}
45{-# LANGUAGE DeriveGeneric #-}
@@ -11,38 +12,64 @@
1112{-# LANGUAGE RecordWildCards #-}
1213{-# LANGUAGE ScopedTypeVariables #-}
1314{-# LANGUAGE StandaloneDeriving #-}
15+{-# LANGUAGE TemplateHaskell #-}
16+{-# LANGUAGE TypeFamilies #-}
1417{-# LANGUAGE UndecidableInstances #-}
18+#if __GLASGOW_HASKELL__ <710
19+{-# LANGUAGE PolyKinds #-}
20+#endif
1521#include "overlapping-compat.h"
1622module Data.Swagger.Internal where
1723
1824import Prelude ()
1925import Prelude.Compat
2026
27+import Control.Lens ((&), (.~), (?~))
2128import Control.Applicative
22−import Control.Monad
2329import Data.Aeson
2430import qualified Data.Aeson.Types as JSON
2531import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
26−import Data.HashMap.Strict (HashMap)
32+import Data.Hashable (Hashable)
2733import qualified Data.HashMap.Strict as HashMap
34+import Data.HashSet.InsOrd (InsOrdHashSet)
2835import Data.Map (Map)
2936import qualified Data.Map as Map
30−import Data.Monoid
37+import Data.Monoid (Monoid (..))
38+import Data.Semigroup.Compat (Semigroup (..))
3139import Data.Scientific (Scientific)
32−import Data.Set (Set)
3340import Data.String (IsString(..))
3441import Data.Text (Text)
3542import qualified Data.Text as Text
3643import GHC.Generics (Generic)
37−import Network (HostName, PortNumber)
44+import Network.Socket (HostName, PortNumber)
3845import Network.HTTP.Media (MediaType)
3946import Text.Read (readMaybe)
4047
4148import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
4249import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4350
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)
4460import Data.Swagger.Internal.Utils
4561
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+
4673-- | A list of definitions that can be used in references.
4774type Definitions = InsOrdHashMap Text
4875
@@ -90,7 +117,7 @@ data Swagger = Swagger
90117 , _swaggerResponses :: Definitions Response
91118
92119 -- | Security scheme definitions that can be used across the specification.
93− , _swaggerSecurityDefinitions :: Definitions SecurityScheme
120+ , _swaggerSecurityDefinitions :: SecurityDefinitions
94121
95122 -- | A declaration of which security schemes are applied for the API as a whole.
96123 -- The list of values describes alternative security schemes that can be used
@@ -103,7 +130,7 @@ data Swagger = Swagger
103130 -- Not all tags that are used by the Operation Object must be declared.
104131 -- The tags that are not declared may be organized randomly or based on the tools' logic.
105132 -- Each tag name in the list MUST be unique.
106− , _swaggerTags :: Set Tag
133+ , _swaggerTags :: InsOrdHashSet Tag
107134
108135 -- | Additional external documentation.
109136 , _swaggerExternalDocs :: Maybe ExternalDocs
@@ -225,7 +252,7 @@ data PathItem = PathItem
225252data Operation = Operation
226253 { -- | A list of tags for API documentation control.
227254 -- Tags can be used for logical grouping of operations by resources or any other qualifier.
228− _operationTags :: Set TagName
255+ _operationTags :: InsOrdHashSet TagName
229256
230257 -- | A short summary of what the operation does.
231258 -- For maximum readability in the swagger-ui, this field SHOULD be less than 120 characters.
@@ -282,7 +309,7 @@ data Operation = Operation
282309 } deriving (Eq, Show, Generic, Data, Typeable)
283310
284311newtype MimeList = MimeList { getMimeList :: [MediaType] }
285− deriving (Eq, Show, Monoid, Typeable)
312+ deriving (Eq, Show, Semigroup, Monoid, Typeable)
286313
287314mimeListConstr :: Constr
288315mimeListConstr = mkConstr mimeListDataType "MimeList" ["getMimeList"] Prefix
@@ -333,8 +360,8 @@ data ParamOtherSchema = ParamOtherSchema
333360 -- Default value is @False@.
334361 , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
335362
336− , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema
337− } deriving (Eq, Show, Generic, Data, Typeable)
363+ , _paramOtherSchemaParamSchema :: ParamSchema 'SwaggerKindParamOtherSchema
364+ } deriving (Eq, Show, Generic, Typeable, Data)
338365
339366-- | Items for @'SwaggerArray'@ schemas.
340367--
@@ -347,9 +374,9 @@ data ParamOtherSchema = ParamOtherSchema
347374--
348375-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
349376data SwaggerItems t where
350− SwaggerItemsPrimitive :: Maybe (CollectionFormat t) -> ParamSchema t -> SwaggerItems t
351− SwaggerItemsObject :: Referenced Schema -> SwaggerItems Schema
352− 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
353380 deriving (Typeable)
354381
355382deriving instance Eq (SwaggerItems t)
@@ -359,17 +386,66 @@ deriving instance Show (SwaggerItems t)
359386swaggerItemsPrimitiveConstr :: Constr
360387swaggerItemsPrimitiveConstr = mkConstr swaggerItemsDataType "SwaggerItemsPrimitive" [] Prefix
361388
389+swaggerItemsObjectConstr :: Constr
390+swaggerItemsObjectConstr = mkConstr swaggerItemsDataType "SwaggerItemsObject" [] Prefix
391+
392+swaggerItemsArrayConstr :: Constr
393+swaggerItemsArrayConstr = mkConstr swaggerItemsDataType "SwaggerItemsArray" [] Prefix
394+
362395swaggerItemsDataType :: DataType
363396swaggerItemsDataType = mkDataType "Data.Swagger.SwaggerItems" [swaggerItemsPrimitiveConstr]
364397
365−instance OVERLAPPABLE_ Data 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
366403 gunfold k z c = case constrIndex c of
367404 1 -> k (k (z SwaggerItemsPrimitive))
368405 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems t)."
369406 toConstr _ = swaggerItemsPrimitiveConstr
370407 dataTypeOf _ = swaggerItemsDataType
371408
372−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
373449
374450data SwaggerType t where
375451 SwaggerString :: SwaggerType t
@@ -377,9 +453,9 @@ data SwaggerType t where
377453 SwaggerInteger :: SwaggerType t
378454 SwaggerBoolean :: SwaggerType t
379455 SwaggerArray :: SwaggerType t
380− SwaggerFile :: SwaggerType ParamOtherSchema
381− SwaggerNull :: SwaggerType Schema
382− SwaggerObject :: SwaggerType Schema
456+ SwaggerFile :: SwaggerType 'SwaggerKindParamOtherSchema
457+ SwaggerNull :: SwaggerType 'SwaggerKindSchema
458+ SwaggerObject :: SwaggerType 'SwaggerKindSchema
383459 deriving (Typeable)
384460
385461deriving instance Eq (SwaggerType t)
@@ -388,33 +464,33 @@ deriving instance Show (SwaggerType t)
388464swaggerTypeConstr :: Data (SwaggerType t) => SwaggerType t -> Constr
389465swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
390466
391−swaggerTypeDataType :: Data (SwaggerType t) => SwaggerType t -> DataType
467+swaggerTypeDataType :: {- Data (SwaggerType t) => -} SwaggerType t -> DataType
392468swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
393469
394−swaggerCommonTypes :: [SwaggerType t]
470+swaggerCommonTypes :: [SwaggerType k]
395471swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
396472
397−swaggerParamTypes :: [SwaggerType ParamOtherSchema]
473+swaggerParamTypes :: [SwaggerType 'SwaggerKindParamOtherSchema]
398474swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
399475
400−swaggerSchemaTypes :: [SwaggerType Schema]
476+swaggerSchemaTypes :: [SwaggerType 'SwaggerKindSchema]
401477swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
402478
403479swaggerTypeConstrs :: [Constr]
404−swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType Schema])
480+swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType 'SwaggerKindSchema])
405481 ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
406482
407−instance OVERLAPPABLE_ Typeable t => Data (SwaggerType t) where
483+instance Typeable t => Data (SwaggerType ('SwaggerKindNormal t)) where
408484 gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
409485 toConstr = swaggerTypeConstr
410486 dataTypeOf = swaggerTypeDataType
411487
412−instance OVERLAPPABLE_ Data (SwaggerType ParamOtherSchema) where
488+instance Data (SwaggerType 'SwaggerKindParamOtherSchema) where
413489 gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
414490 toConstr = swaggerTypeConstr
415491 dataTypeOf = swaggerTypeDataType
416492
417−instance OVERLAPPABLE_ Data (SwaggerType Schema) where
493+instance Data (SwaggerType 'SwaggerKindSchema) where
418494 gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
419495 toConstr = swaggerTypeConstr
420496 dataTypeOf = swaggerTypeDataType
@@ -454,7 +530,7 @@ data CollectionFormat t where
454530 -- Corresponds to multiple parameter instances
455531 -- instead of multiple values for a single instance @foo=bar&foo=baz@.
456532 -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
457− CollectionMulti :: CollectionFormat ParamOtherSchema
533+ CollectionMulti :: CollectionFormat 'SwaggerKindParamOtherSchema
458534 deriving (Typeable)
459535
460536deriving instance Eq (CollectionFormat t)
@@ -470,12 +546,12 @@ collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat" $
470546collectionCommonFormats :: [CollectionFormat t]
471547collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
472548
473−instance OVERLAPPABLE_ Data t => Data (CollectionFormat t) where
549+instance Data t => Data (CollectionFormat ('SwaggerKindNormal t)) where
474550 gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
475551 toConstr = collectionFormatConstr
476552 dataTypeOf _ = collectionFormatDataType
477553
478−deriving instance OVERLAPPABLE_ Data (CollectionFormat ParamOtherSchema)
554+deriving instance Data (CollectionFormat 'SwaggerKindParamOtherSchema)
479555
480556type ParamName = Text
481557
@@ -484,9 +560,9 @@ data Schema = Schema
484560 , _schemaDescription :: Maybe Text
485561 , _schemaRequired :: [ParamName]
486562
487− , _schemaAllOf :: Maybe [Schema]
563+ , _schemaAllOf :: Maybe [Referenced Schema]
488564 , _schemaProperties :: InsOrdHashMap Text (Referenced Schema)
489− , _schemaAdditionalProperties :: Maybe (Referenced Schema)
565+ , _schemaAdditionalProperties :: Maybe AdditionalProperties
490566
491567 , _schemaDiscriminator :: Maybe Text
492568 , _schemaReadOnly :: Maybe Bool
@@ -497,7 +573,7 @@ data Schema = Schema
497573 , _schemaMaxProperties :: Maybe Integer
498574 , _schemaMinProperties :: Maybe Integer
499575
500− , _schemaParamSchema :: ParamSchema Schema
576+ , _schemaParamSchema :: ParamSchema 'SwaggerKindSchema
501577 } deriving (Eq, Show, Generic, Data, Typeable)
502578
503579-- | A @'Schema'@ with an optional name.
@@ -510,7 +586,7 @@ data NamedSchema = NamedSchema
510586-- | Regex pattern for @string@ type.
511587type Pattern = Text
512588
513−data ParamSchema t = ParamSchema
589+data ParamSchema (t :: SwaggerKind *) = ParamSchema
514590 { -- | Declares the value of the parameter that the server will use if none is provided,
515591 -- for example a @"count"@ to control the number of results per page might default to @100@
516592 -- if not supplied by the client in the request.
@@ -518,7 +594,7 @@ data ParamSchema t = ParamSchema
518594 -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
519595 _paramSchemaDefault :: Maybe Value
520596
521− , _paramSchemaType :: SwaggerType t
597+ , _paramSchemaType :: Maybe (SwaggerType t)
522598 , _paramSchemaFormat :: Maybe Format
523599 , _paramSchemaItems :: Maybe (SwaggerItems t)
524600 , _paramSchemaMaximum :: Maybe Scientific
@@ -535,7 +611,7 @@ data ParamSchema t = ParamSchema
535611 , _paramSchemaMultipleOf :: Maybe Scientific
536612 } deriving (Eq, Show, Generic, Typeable)
537613
538−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)
539615
540616data Xml = Xml
541617 { -- | Replaces the name of the element/attribute used for the described schema property.
@@ -611,7 +687,7 @@ data Header = Header
611687 { -- | A short description of the header.
612688 _headerDescription :: Maybe Text
613689
614− , _headerParamSchema :: ParamSchema Header
690+ , _headerParamSchema :: ParamSchema ('SwaggerKindNormal Header)
615691 } deriving (Eq, Show, Generic, Data, Typeable)
616692
617693data Example = Example { getExample :: Map MediaType Value }
@@ -679,12 +755,28 @@ data SecurityScheme = SecurityScheme
679755 , _securitySchemeDescription :: Maybe Text
680756 } deriving (Eq, Show, Generic, Data, Typeable)
681757
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+
682774-- | Lists the required security schemes to execute this operation.
683775-- The object can have multiple security schemes declared in it which are all required
684776-- (that is, there is a logical AND between the schemes).
685777newtype SecurityRequirement = SecurityRequirement
686778 { getSecurityRequirement :: InsOrdHashMap Text [Text]
687− } deriving (Eq, Read, Show, Monoid, ToJSON, FromJSON, Data, Typeable)
779+ } deriving (Eq, Read, Show, Semigroup, Monoid, ToJSON, FromJSON, Data, Typeable)
688780
689781-- | Tag name.
690782type TagName = Text
@@ -703,6 +795,8 @@ data Tag = Tag
703795 , _tagExternalDocs :: Maybe ExternalDocs
704796 } deriving (Eq, Ord, Show, Generic, Data, Typeable)
705797
798+instance Hashable Tag
799+
706800instance IsString Tag where
707801 fromString s = Tag (fromString s) Nothing Nothing
708802
@@ -716,6 +810,8 @@ data ExternalDocs = ExternalDocs
716810 , _externalDocsUrl :: URL
717811 } deriving (Eq, Ord, Show, Generic, Data, Typeable)
718812
813+instance Hashable ExternalDocs
814+
719815-- | A simple object to allow referencing other definitions in the specification.
720816-- It can be used to reference parameters and responses that are defined at the top level for reuse.
721817newtype Reference = Reference { getReference :: Text }
@@ -729,67 +825,111 @@ data Referenced a
729825instance IsString a => IsString (Referenced a) where
730826 fromString = Inline . fromString
731827
732−newtype URL = URL { getUrl :: Text } deriving (Eq, Ord, Show, ToJSON, FromJSON, Data, Typeable)
828+newtype URL = URL { getUrl :: Text } deriving (Eq, Ord, Show, Hashable, ToJSON, FromJSON, Data, Typeable)
829+
830+data AdditionalProperties
831+ = AdditionalPropertiesAllowed Bool
832+ | AdditionalPropertiesSchema (Referenced Schema)
833+ deriving (Eq, Show, Data, Typeable)
733834
734835-- =======================================================================
735836-- Monoid instances
736837-- =======================================================================
737838
839+instance Semigroup Swagger where
840+ (<>) = genericMappend
738841instance Monoid Swagger where
739842 mempty = genericMempty
740− mappend = genericMappend
843+ mappend = (<>)
741844
845+instance Semigroup Info where
846+ (<>) = genericMappend
742847instance Monoid Info where
743848 mempty = genericMempty
744− mappend = genericMappend
849+ mappend = (<>)
745850
851+instance Semigroup Contact where
852+ (<>) = genericMappend
746853instance Monoid Contact where
747854 mempty = genericMempty
748− mappend = genericMappend
855+ mappend = (<>)
749856
857+instance Semigroup PathItem where
858+ (<>) = genericMappend
750859instance Monoid PathItem where
751860 mempty = genericMempty
752− mappend = genericMappend
861+ mappend = (<>)
753862
863+instance Semigroup Schema where
864+ (<>) = genericMappend
754865instance Monoid Schema where
755866 mempty = genericMempty
756− mappend = genericMappend
867+ mappend = (<>)
757868
869+instance Semigroup (ParamSchema t) where
870+ (<>) = genericMappend
758871instance Monoid (ParamSchema t) where
759872 mempty = genericMempty
760− mappend = genericMappend
873+ mappend = (<>)
761874
875+instance Semigroup Param where
876+ (<>) = genericMappend
762877instance Monoid Param where
763878 mempty = genericMempty
764− mappend = genericMappend
879+ mappend = (<>)
765880
881+instance Semigroup ParamOtherSchema where
882+ (<>) = genericMappend
766883instance Monoid ParamOtherSchema where
767884 mempty = genericMempty
768− mappend = genericMappend
885+ mappend = (<>)
769886
887+instance Semigroup Header where
888+ (<>) = genericMappend
770889instance Monoid Header where
771890 mempty = genericMempty
772− mappend = genericMappend
891+ mappend = (<>)
773892
893+instance Semigroup Responses where
894+ (<>) = genericMappend
774895instance Monoid Responses where
775896 mempty = genericMempty
776− mappend = genericMappend
897+ mappend = (<>)
777898
899+instance Semigroup Response where
900+ (<>) = genericMappend
778901instance Monoid Response where
779902 mempty = genericMempty
780− mappend = genericMappend
903+ mappend = (<>)
781904
905+instance Semigroup ExternalDocs where
906+ (<>) = genericMappend
782907instance Monoid ExternalDocs where
783908 mempty = genericMempty
784− mappend = genericMappend
909+ mappend = (<>)
785910
911+instance Semigroup Operation where
912+ (<>) = genericMappend
786913instance Monoid Operation where
787914 mempty = genericMempty
788− mappend = genericMappend
915+ mappend = (<>)
789916
917+instance Semigroup Example where
918+ (<>) = genericMappend
790919instance Monoid Example where
791920 mempty = genericMempty
792− mappend = genericMappend
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 = (<>)
793933
794934-- =======================================================================
795935-- SwaggerMonoid helper instances
@@ -805,6 +945,8 @@ instance SwaggerMonoid Responses
805945instance SwaggerMonoid Response
806946instance SwaggerMonoid ExternalDocs
807947instance SwaggerMonoid Operation
948+instance SwaggerMonoid SecurityDefinitions
949+instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)
808950
809951instance SwaggerMonoid MimeList
810952deriving instance SwaggerMonoid URL
@@ -817,17 +959,13 @@ instance SwaggerMonoid ParamLocation where
817959 swaggerMempty = ParamQuery
818960 swaggerMappend _ y = y
819961
820−instance OVERLAPPING_ SwaggerMonoid (HashMap FilePath PathItem) where
821− swaggerMempty = HashMap.empty
822− swaggerMappend = HashMap.unionWith mappend
823−
824962instance OVERLAPPING_ SwaggerMonoid (InsOrdHashMap FilePath PathItem) where
825963 swaggerMempty = InsOrdHashMap.empty
826964 swaggerMappend = InsOrdHashMap.unionWith mappend
827965
828966instance Monoid a => SwaggerMonoid (Referenced a) where
829967 swaggerMempty = Inline mempty
830− swaggerMappend (Inline x) (Inline y) = Inline (x <> y)
968+ swaggerMappend (Inline x) (Inline y) = Inline (mappend x y)
831969 swaggerMappend _ y = y
832970
833971instance SwaggerMonoid ParamAnySchema where
@@ -921,7 +1059,8 @@ instance ToJSON OAuth2Flow where
9211059 , "tokenUrl" .= tokenUrl ]
9221060
9231061instance ToJSON OAuth2Params where
924− toJSON = omitEmpties . genericToJSONWithSub "flow" (jsonPrefix "oauth2")
1062+ toJSON = sopSwaggerGenericToJSON
1063+ DEFINE_TOENCODING
9251064
9261065instance ToJSON SecuritySchemeType where
9271066 toJSON SecuritySchemeBasic
@@ -934,28 +1073,37 @@ instance ToJSON SecuritySchemeType where
9341073 <+> object [ "type" .= ("oauth2" :: Text) ]
9351074
9361075instance ToJSON Swagger where
937− toJSON = omitEmpties . addVersion . genericToJSON (jsonPrefix "swagger")
938− where
939− addVersion (Object o) = Object (HashMap.insert "swagger" "2.0" o)
940− addVersion _ = error "impossible"
1076+ toJSON = sopSwaggerGenericToJSON
1077+ DEFINE_TOENCODING
9411078
9421079instance ToJSON SecurityScheme where
943− toJSON = genericToJSONWithSub "type" (jsonPrefix "securityScheme")
1080+ toJSON = sopSwaggerGenericToJSON
1081+ DEFINE_TOENCODING
9441082
9451083instance ToJSON Schema where
946− toJSON = omitEmptiesExcept f . genericToJSONWithSub "paramSchema" (jsonPrefix "schema")
947− where
948− f "items" (Array _) = True
949− f _ _ = False
1084+ toJSON = sopSwaggerGenericToJSON
1085+ DEFINE_TOENCODING
9501086
9511087instance ToJSON Header where
952− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "header")
1088+ toJSON = sopSwaggerGenericToJSON
1089+ DEFINE_TOENCODING
9531090
954−instance ToJSON (SwaggerItems t) where
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
9551098 toJSON (SwaggerItemsPrimitive fmt schema) = object
9561099 [ "collectionFormat" .= fmt
9571100 , "items" .= schema ]
9581101 toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
1102+ toJSON (SwaggerItemsArray []) = object
1103+ [ "items" .= object []
1104+ , "maxItems" .= (0 :: Int)
1105+ , "example" .= Array mempty
1106+ ]
9591107 toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
9601108
9611109instance ToJSON Host where
@@ -968,31 +1116,39 @@ instance ToJSON MimeList where
9681116 toJSON (MimeList xs) = toJSON (map show xs)
9691117
9701118instance ToJSON Param where
971− toJSON = genericToJSONWithSub "schema" (jsonPrefix "param")
1119+ toJSON = sopSwaggerGenericToJSON
1120+ DEFINE_TOENCODING
9721121
9731122instance ToJSON ParamAnySchema where
9741123 toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
9751124 toJSON (ParamOther s) = toJSON s
9761125
9771126instance ToJSON ParamOtherSchema where
978− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
1127+ toJSON = sopSwaggerGenericToJSON
1128+ DEFINE_TOENCODING
9791129
9801130instance ToJSON Responses where
981− toJSON (Responses def rs) = omitEmpties $
982− toJSON (InsOrdHashMap.mapKeys show rs) <+> object [ "default" .= def ]
1131+ toJSON = sopSwaggerGenericToJSON
1132+ DEFINE_TOENCODING
9831133
9841134instance ToJSON Response where
985− toJSON = omitEmpties . genericToJSON (jsonPrefix "response")
1135+ toJSON = sopSwaggerGenericToJSON
1136+ DEFINE_TOENCODING
9861137
9871138instance ToJSON Operation where
988− toJSON = omitEmpties . genericToJSON (jsonPrefix "operation")
1139+ toJSON = sopSwaggerGenericToJSON
1140+ DEFINE_TOENCODING
9891141
9901142instance ToJSON PathItem where
991− toJSON = omitEmpties . genericToJSON (jsonPrefix "pathItem")
1143+ toJSON = sopSwaggerGenericToJSON
1144+ DEFINE_TOENCODING
9921145
9931146instance ToJSON Example where
9941147 toJSON = toJSON . Map.mapKeys show . getExample
9951148
1149+instance ToJSON SecurityDefinitions where
1150+ toJSON (SecurityDefinitions sd) = toJSON sd
1151+
9961152instance ToJSON Reference where
9971153 toJSON (Reference ref) = object [ "$ref" .= ref ]
9981154
@@ -1021,11 +1177,14 @@ instance ToJSON (CollectionFormat t) where
10211177 toJSON CollectionPipes = "pipes"
10221178 toJSON CollectionMulti = "multi"
10231179
1024−instance ToJSON (ParamSchema t) where
1025− toJSON = omitEmptiesExcept f . genericToJSONWithSub "items" (jsonPrefix "paramSchema")
1026− where
1027− f "items" (Array _) = True
1028− f _ _ = False
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
10291188
10301189-- =======================================================================
10311190-- Manual FromJSON instances
@@ -1045,7 +1204,7 @@ instance FromJSON OAuth2Flow where
10451204 parseJSON _ = empty
10461205
10471206instance FromJSON OAuth2Params where
1048− parseJSON = genericParseJSONWithSub "flow" (jsonPrefix "oauth2")
1207+ parseJSON = sopSwaggerGenericParseJSON
10491208
10501209instance FromJSON SecuritySchemeType where
10511210 parseJSON js@(Object o) = do
@@ -1058,39 +1217,48 @@ instance FromJSON SecuritySchemeType where
10581217 parseJSON _ = empty
10591218
10601219instance FromJSON Swagger where
1061− parseJSON js@(Object o) = do
1062− (version :: Text) <- o .: "swagger"
1063− when (version /= "2.0") empty
1064− (genericParseJSON (jsonPrefix "swagger")
1065− `withDefaults` [ "consumes" .= (mempty :: MimeList)
1066− , "produces" .= (mempty :: MimeList)
1067− , "security" .= ([] :: [SecurityRequirement])
1068− , "tags" .= ([] :: [Tag])
1069− , "definitions" .= (mempty :: Definitions Schema)
1070− , "parameters" .= (mempty :: Definitions Param)
1071− , "responses" .= (mempty :: Definitions Response)
1072− , "securityDefinitions" .= (mempty :: Definitions SecurityScheme)
1073− ] ) js
1074− parseJSON _ = empty
1220+ parseJSON = sopSwaggerGenericParseJSON
10751221
10761222instance FromJSON SecurityScheme where
1077− parseJSON = genericParseJSONWithSub "type" (jsonPrefix "securityScheme")
1223+ parseJSON = sopSwaggerGenericParseJSON
10781224
10791225instance FromJSON Schema where
1080− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "schema")
1081− `withDefaults` [ "properties" .= (mempty :: InsOrdHashMap Text Schema)
1082− , "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
10831233
10841234instance FromJSON Header where
1085− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "header")
1235+ parseJSON = sopSwaggerGenericParseJSON
10861236
1087−instance OVERLAPPABLE_ (FromJSON (CollectionFormat t), FromJSON (ParamSchema t)) => FromJSON (SwaggerItems t) where
1237+instance (FromJSON (CollectionFormat ('SwaggerKindNormal t)), FromJSON (ParamSchema ('SwaggerKindNormal t))) => FromJSON (SwaggerItems ('SwaggerKindNormal t)) where
10881238 parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
10891239 <$> o .:? "collectionFormat"
10901240 <*> (o .: "items" >>= parseJSON)
10911241
1092−instance OVERLAPPABLE_ FromJSON (SwaggerItems Schema) where
1093− parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
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
10941262 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
10951263 parseJSON _ = empty
10961264
@@ -1107,7 +1275,7 @@ instance FromJSON MimeList where
11071275 parseJSON js = (MimeList . map fromString) <$> parseJSON js
11081276
11091277instance FromJSON Param where
1110− parseJSON = genericParseJSONWithSub "schema" (jsonPrefix "param")
1278+ parseJSON = sopSwaggerGenericParseJSON
11111279
11121280instance FromJSON ParamAnySchema where
11131281 parseJSON js@(Object o) = do
@@ -1120,7 +1288,7 @@ instance FromJSON ParamAnySchema where
11201288 parseJSON _ = empty
11211289
11221290instance FromJSON ParamOtherSchema where
1123− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
1291+ parseJSON = sopSwaggerGenericParseJSON
11241292
11251293instance FromJSON Responses where
11261294 parseJSON (Object o) = Responses
@@ -1134,18 +1302,16 @@ instance FromJSON Example where
11341302 pure $ Example (Map.mapKeys fromString m)
11351303
11361304instance FromJSON Response where
1137− parseJSON = genericParseJSON (jsonPrefix "response")
1138− `withDefaults` [ "headers" .= (mempty :: InsOrdHashMap HeaderName Header) ]
1305+ parseJSON = sopSwaggerGenericParseJSON
11391306
11401307instance FromJSON Operation where
1141− parseJSON = genericParseJSON (jsonPrefix "operation")
1142− `withDefaults` [ "security" .= ([] :: [SecurityRequirement])
1143− , "tags" .= ([] :: [Tag])
1144− , "parameters" .= ([] :: [Referenced Param]) ]
1308+ parseJSON = sopSwaggerGenericParseJSON
11451309
11461310instance FromJSON PathItem where
1147− parseJSON = genericParseJSON (jsonPrefix "pathItem")
1148− `withDefaults` [ "parameters" .= ([] :: [Param]) ]
1311+ parseJSON = sopSwaggerGenericParseJSON
1312+
1313+instance FromJSON SecurityDefinitions where
1314+ parseJSON js = SecurityDefinitions <$> parseJSON js
11491315
11501316instance FromJSON Reference where
11511317 parseJSON (Object o) = Reference <$> o .: "$ref"
@@ -1171,26 +1337,91 @@ instance FromJSON (Referenced Response) where parseJSON = referencedParseJSON "#
11711337instance FromJSON Xml where
11721338 parseJSON = genericParseJSON (jsonPrefix "xml")
11731339
1174−instance FromJSON (SwaggerType Schema) where
1340+instance FromJSON (SwaggerType 'SwaggerKindSchema) where
11751341 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
11761342
1177−instance FromJSON (SwaggerType ParamOtherSchema) where
1343+instance FromJSON (SwaggerType 'SwaggerKindParamOtherSchema) where
11781344 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerFile]
11791345
1180−instance OVERLAPPABLE_ FromJSON (SwaggerType t) where
1346+instance FromJSON (SwaggerType ('SwaggerKindNormal t)) where
11811347 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
11821348
1183−instance OVERLAPPABLE_ FromJSON (CollectionFormat t) where
1349+instance FromJSON (CollectionFormat ('SwaggerKindNormal t)) where
11841350 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
11851351
1186−instance FromJSON (CollectionFormat ParamOtherSchema) where
1187− parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
1352+-- NOTE: There aren't collections of 'Schema'
1353+--instance FromJSON (CollectionFormat (SwaggerKindSchema)) where
1354+-- parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
11881355
1189−-- NOTE: The constraints @FromJSON (SwaggerType t)@ and
1190−-- @FromJSON (SwaggerItems t)@ are necessary here!
1191−-- Without the constraint the general instance will be used
1192−-- that only accepts common types (i.e. NOT object, null or file)
1193−-- and primitive array items.
1194−instance (FromJSON (SwaggerType t), FromJSON (SwaggerItems t)) => FromJSON (ParamSchema t) where
1195− parseJSON = genericParseJSONWithSub "items" (jsonPrefix "ParamSchema")
1356+instance FromJSON (CollectionFormat 'SwaggerKindParamOtherSchema) where
1357+ parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
11961358
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