@@ -1,4 +1,5 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE DeriveFunctor #-}
4{-# LANGUAGE DeriveFunctor #-}
4{-# LANGUAGE DeriveGeneric #-}
5{-# LANGUAGE DeriveGeneric #-}
@@ -11,19 +12,23 @@
11{-# LANGUAGE RecordWildCards #-}
12{-# LANGUAGE RecordWildCards #-}
12{-# LANGUAGE ScopedTypeVariables #-}
13{-# LANGUAGE ScopedTypeVariables #-}
13{-# LANGUAGE StandaloneDeriving #-}
14{-# LANGUAGE StandaloneDeriving #-}
15+{-# LANGUAGE TemplateHaskell #-}
16+{-# LANGUAGE TypeFamilies #-}
14{-# LANGUAGE UndecidableInstances #-}
17{-# LANGUAGE UndecidableInstances #-}
18+#if __GLASGOW_HASKELL__ <710
19+{-# LANGUAGE PolyKinds #-}
15#include "overlapping-compat.h"
21#include "overlapping-compat.h"
16module Data.Swagger.Internal where
22module Data.Swagger.Internal where
18import Prelude ()
24import Prelude ()
19import Prelude.Compat
25import Prelude.Compat
27+import Control.Lens ((&), (.~), (?~))
21import Control.Applicative
28import Control.Applicative
23import Data.Aeson
29import Data.Aeson
24import qualified Data.Aeson.Types as JSON
30import qualified Data.Aeson.Types as JSON
25import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
31import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
26−import Data.HashMap.Strict (HashMap)
27import qualified Data.HashMap.Strict as HashMap
32import qualified Data.HashMap.Strict as HashMap
28import Data.Map (Map)
33import Data.Map (Map)
29import qualified Data.Map as Map
34import qualified Data.Map as Map
@@ -38,10 +43,29 @@ import Network (HostName, PortNumber)
38import Network.HTTP.Media (MediaType)
43import Network.HTTP.Media (MediaType)
39import Text.Read (readMaybe)
44import Text.Read (readMaybe)
46+import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
47+import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
49+import Generics.SOP.TH (deriveGeneric)
50+import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
51+ ,sopSwaggerGenericToJSONWithOpts
52+ ,sopSwaggerGenericParseJSON
53+ ,HasSwaggerAesonOptions(..)
54+ ,AesonDefaultValue(..)
55+ ,mkSwaggerAesonOptions
41import Data.Swagger.Internal.Utils
58import Data.Swagger.Internal.Utils
60+#if MIN_VERSION_aeson(0,10,0)
61+import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToEncoding)
62+#define DEFINE_TOENCODING toEncoding = sopSwaggerGenericToEncoding
64+#define DEFINE_TOENCODING
43-- | A list of definitions that can be used in references.
67-- | A list of definitions that can be used in references.
44−type Definitions = HashMap Text
68+type Definitions = InsOrdHashMap Text
46-- | This is the root document object for the API specification.
70-- | This is the root document object for the API specification.
47data Swagger = Swagger
71data Swagger = Swagger
@@ -73,7 +97,7 @@ data Swagger = Swagger
73 -- | The available paths and operations for the API.
97 -- | The available paths and operations for the API.
74 -- Holds the relative paths to the individual endpoints.
98 -- Holds the relative paths to the individual endpoints.
75 -- The path is appended to the @'basePath'@ in order to construct the full URL.
99 -- The path is appended to the @'basePath'@ in order to construct the full URL.
76− , _swaggerPaths :: HashMap FilePath PathItem
100+ , _swaggerPaths :: InsOrdHashMap FilePath PathItem
78 -- | An object to hold data types produced and consumed by operations.
102 -- | An object to hold data types produced and consumed by operations.
79 , _swaggerDefinitions :: Definitions Schema
103 , _swaggerDefinitions :: Definitions Schema
@@ -330,8 +354,8 @@ data ParamOtherSchema = ParamOtherSchema
330 -- Default value is @False@.
354 -- Default value is @False@.
331 , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
355 , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
333− , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema
357+ , _paramOtherSchemaParamSchema :: ParamSchema 'SwaggerKindParamOtherSchema
334− } deriving (Eq, Show, Generic, Data, Typeable)
358+ } deriving (Eq, Show, Generic, Typeable, Data)
336-- | Items for @'SwaggerArray'@ schemas.
360-- | Items for @'SwaggerArray'@ schemas.
@@ -344,9 +368,9 @@ data ParamOtherSchema = ParamOtherSchema
345-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
369-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
346data SwaggerItems t where
370data SwaggerItems t where
347− SwaggerItemsPrimitive :: Maybe (CollectionFormat t) -> ParamSchema t -> SwaggerItems t
371+ SwaggerItemsPrimitive :: Maybe (CollectionFormat k) -> ParamSchema k-> SwaggerItems k
348− SwaggerItemsObject :: Referenced Schema -> SwaggerItems Schema
372+ SwaggerItemsObject :: Referenced Schema -> SwaggerItems 'SwaggerKindSchema
349− SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems Schema
373+ SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems 'SwaggerKindSchema
350 deriving (Typeable)
374 deriving (Typeable)
352deriving instance Eq (SwaggerItems t)
376deriving instance Eq (SwaggerItems t)
@@ -356,17 +380,66 @@ deriving instance Show (SwaggerItems t)
356swaggerItemsPrimitiveConstr :: Constr
380swaggerItemsPrimitiveConstr :: Constr
357swaggerItemsPrimitiveConstr = mkConstr swaggerItemsDataType "SwaggerItemsPrimitive" [] Prefix
381swaggerItemsPrimitiveConstr = mkConstr swaggerItemsDataType "SwaggerItemsPrimitive" [] Prefix
383+swaggerItemsObjectConstr :: Constr
384+swaggerItemsObjectConstr = mkConstr swaggerItemsDataType "SwaggerItemsObject" [] Prefix
386+swaggerItemsArrayConstr :: Constr
387+swaggerItemsArrayConstr = mkConstr swaggerItemsDataType "SwaggerItemsArray" [] Prefix
359swaggerItemsDataType :: DataType
389swaggerItemsDataType :: DataType
360swaggerItemsDataType = mkDataType "Data.Swagger.SwaggerItems" [swaggerItemsPrimitiveConstr]
390swaggerItemsDataType = mkDataType "Data.Swagger.SwaggerItems" [swaggerItemsPrimitiveConstr]
392+-- Note: unfortunately we have to write these Data instances by hand,
393+-- to get better contexts / avoid duplicate name when using standalone deriving
362−instance OVERLAPPABLE_ Data t => Data (SwaggerItems t) where
395+instance Data t => Data (SwaggerItems ('SwaggerKindNormal t)) where
396+ -- TODO: define gfoldl
363 gunfold k z c = case constrIndex c of
397 gunfold k z c = case constrIndex c of
364 1 -> k (k (z SwaggerItemsPrimitive))
398 1 -> k (k (z SwaggerItemsPrimitive))
365 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems t)."
399 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems t)."
366 toConstr _ = swaggerItemsPrimitiveConstr
400 toConstr _ = swaggerItemsPrimitiveConstr
367 dataTypeOf _ = swaggerItemsDataType
401 dataTypeOf _ = swaggerItemsDataType
403+-- SwaggerItems SwaggerKindParamOtherSchema can be constructed using SwaggerItemsPrimitive only
369−deriving instance Data (SwaggerItems Schema)
404+instance Data (SwaggerItems 'SwaggerKindParamOtherSchema) where
405+ -- TODO: define gfoldl
406+ gunfold k z c = case constrIndex c of
407+ 1 -> k (k (z SwaggerItemsPrimitive))
408+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindParamOtherSchema)."
409+ toConstr _ = swaggerItemsPrimitiveConstr
410+ dataTypeOf _ = swaggerItemsDataType
412+instance Data (SwaggerItems 'SwaggerKindSchema) where
413+ gfoldl _ _ (SwaggerItemsPrimitive _ _) = error $ " Data.Data.gfoldl: Constructor SwaggerItemsPrimitive used to construct SwaggerItems SwaggerKindSchema"
414+ gfoldl k z (SwaggerItemsObject ref) = z SwaggerItemsObject `k` ref
415+ gfoldl k z (SwaggerItemsArray ref) = z SwaggerItemsArray `k` ref
417+ gunfold k z c = case constrIndex c of
418+ 2 -> k (z SwaggerItemsObject)
419+ 3 -> k (z SwaggerItemsArray)
420+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindSchema)."
422+ toConstr (SwaggerItemsPrimitive _ _) = error "Not supported"
423+ toConstr (SwaggerItemsObject _) = swaggerItemsObjectConstr
424+ toConstr (SwaggerItemsArray _) = swaggerItemsArrayConstr
426+ dataTypeOf _ = swaggerItemsDataType
428+-- | Type used as a kind to avoid overlapping instances.
430+ = SwaggerKindNormal t
431+ | SwaggerKindParamOtherSchema
435+deriving instance Typeable 'SwaggerKindNormal
436+deriving instance Typeable 'SwaggerKindParamOtherSchema
437+deriving instance Typeable 'SwaggerKindSchema
439+type family SwaggerKindType (k :: SwaggerKind *) :: *
440+type instance SwaggerKindType ('SwaggerKindNormal t) = t
441+type instance SwaggerKindType 'SwaggerKindSchema = Schema
442+type instance SwaggerKindType 'SwaggerKindParamOtherSchema = ParamOtherSchema
371data SwaggerType t where
444data SwaggerType t where
372 SwaggerString :: SwaggerType t
445 SwaggerString :: SwaggerType t
@@ -374,9 +447,9 @@ data SwaggerType t where
374 SwaggerInteger :: SwaggerType t
447 SwaggerInteger :: SwaggerType t
375 SwaggerBoolean :: SwaggerType t
448 SwaggerBoolean :: SwaggerType t
376 SwaggerArray :: SwaggerType t
449 SwaggerArray :: SwaggerType t
377− SwaggerFile :: SwaggerType ParamOtherSchema
450+ SwaggerFile :: SwaggerType 'SwaggerKindParamOtherSchema
378− SwaggerNull :: SwaggerType Schema
451+ SwaggerNull :: SwaggerType 'SwaggerKindSchema
379− SwaggerObject :: SwaggerType Schema
452+ SwaggerObject :: SwaggerType 'SwaggerKindSchema
380 deriving (Typeable)
453 deriving (Typeable)
382deriving instance Eq (SwaggerType t)
455deriving instance Eq (SwaggerType t)
@@ -388,30 +461,30 @@ swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
388swaggerTypeDataType :: Data (SwaggerType t) => SwaggerType t -> DataType
461swaggerTypeDataType :: Data (SwaggerType t) => SwaggerType t -> DataType
389swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
462swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
391−swaggerCommonTypes :: [SwaggerType t]
464+swaggerCommonTypes :: [SwaggerType k]
392swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
465swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
394−swaggerParamTypes :: [SwaggerType ParamOtherSchema]
467+swaggerParamTypes :: [SwaggerType 'SwaggerKindParamOtherSchema]
395swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
468swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
397−swaggerSchemaTypes :: [SwaggerType Schema]
470+swaggerSchemaTypes :: [SwaggerType 'SwaggerKindSchema]
398swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
471swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
400swaggerTypeConstrs :: [Constr]
473swaggerTypeConstrs :: [Constr]
401−swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType Schema])
474+swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType 'SwaggerKindSchema])
402 ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
475 ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
404−instance OVERLAPPABLE_ Typeable t => Data (SwaggerType t) where
477+instance Typeable t => Data (SwaggerType ('SwaggerKindNormal t)) where
405 gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
478 gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
406 toConstr = swaggerTypeConstr
479 toConstr = swaggerTypeConstr
407 dataTypeOf = swaggerTypeDataType
480 dataTypeOf = swaggerTypeDataType
409−instance OVERLAPPABLE_ Data (SwaggerType ParamOtherSchema) where
482+instance Data (SwaggerType 'SwaggerKindParamOtherSchema) where
410 gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
483 gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
411 toConstr = swaggerTypeConstr
484 toConstr = swaggerTypeConstr
412 dataTypeOf = swaggerTypeDataType
485 dataTypeOf = swaggerTypeDataType
414−instance OVERLAPPABLE_ Data (SwaggerType Schema) where
487+instance Data (SwaggerType 'SwaggerKindSchema) where
415 gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
488 gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
416 toConstr = swaggerTypeConstr
489 toConstr = swaggerTypeConstr
417 dataTypeOf = swaggerTypeDataType
490 dataTypeOf = swaggerTypeDataType
@@ -451,7 +524,7 @@ data CollectionFormat t where
451 -- Corresponds to multiple parameter instances
524 -- Corresponds to multiple parameter instances
452 -- instead of multiple values for a single instance @foo=bar&foo=baz@.
525 -- instead of multiple values for a single instance @foo=bar&foo=baz@.
453 -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
526 -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
454− CollectionMulti :: CollectionFormat ParamOtherSchema
527+ CollectionMulti :: CollectionFormat 'SwaggerKindParamOtherSchema
455 deriving (Typeable)
528 deriving (Typeable)
457deriving instance Eq (CollectionFormat t)
530deriving instance Eq (CollectionFormat t)
@@ -467,12 +540,12 @@ collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat" $
467collectionCommonFormats :: [CollectionFormat t]
540collectionCommonFormats :: [CollectionFormat t]
468collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
541collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
470−instance OVERLAPPABLE_ Data t => Data (CollectionFormat t) where
543+instance Data t => Data (CollectionFormat ('SwaggerKindNormal t)) where
471 gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
544 gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
472 toConstr = collectionFormatConstr
545 toConstr = collectionFormatConstr
473 dataTypeOf _ = collectionFormatDataType
546 dataTypeOf _ = collectionFormatDataType
475−deriving instance OVERLAPPABLE_ Data (CollectionFormat ParamOtherSchema)
548+deriving instance Data (CollectionFormat 'SwaggerKindParamOtherSchema)
477type ParamName = Text
550type ParamName = Text
@@ -482,7 +555,7 @@ data Schema = Schema
482 , _schemaRequired :: [ParamName]
555 , _schemaRequired :: [ParamName]
484 , _schemaAllOf :: Maybe [Schema]
557 , _schemaAllOf :: Maybe [Schema]
485− , _schemaProperties :: HashMap Text (Referenced Schema)
558+ , _schemaProperties :: InsOrdHashMap Text (Referenced Schema)
486 , _schemaAdditionalProperties :: Maybe (Referenced Schema)
559 , _schemaAdditionalProperties :: Maybe (Referenced Schema)
488 , _schemaDiscriminator :: Maybe Text
561 , _schemaDiscriminator :: Maybe Text
@@ -494,7 +567,7 @@ data Schema = Schema
494 , _schemaMaxProperties :: Maybe Integer
567 , _schemaMaxProperties :: Maybe Integer
495 , _schemaMinProperties :: Maybe Integer
568 , _schemaMinProperties :: Maybe Integer
497− , _schemaParamSchema :: ParamSchema Schema
570+ , _schemaParamSchema :: ParamSchema 'SwaggerKindSchema
498 } deriving (Eq, Show, Generic, Data, Typeable)
571 } deriving (Eq, Show, Generic, Data, Typeable)
500-- | A @'Schema'@ with an optional name.
573-- | A @'Schema'@ with an optional name.
@@ -507,7 +580,7 @@ data NamedSchema = NamedSchema
507-- | Regex pattern for @string@ type.
580-- | Regex pattern for @string@ type.
508type Pattern = Text
581type Pattern = Text
510−data ParamSchema t = ParamSchema
583+data ParamSchema (t :: SwaggerKind *) = ParamSchema
511 { -- | Declares the value of the parameter that the server will use if none is provided,
584 { -- | Declares the value of the parameter that the server will use if none is provided,
512 -- for example a @"count"@ to control the number of results per page might default to @100@
585 -- for example a @"count"@ to control the number of results per page might default to @100@
513 -- if not supplied by the client in the request.
586 -- if not supplied by the client in the request.
@@ -532,7 +605,7 @@ data ParamSchema t = ParamSchema
532 , _paramSchemaMultipleOf :: Maybe Scientific
605 , _paramSchemaMultipleOf :: Maybe Scientific
533 } deriving (Eq, Show, Generic, Typeable)
606 } deriving (Eq, Show, Generic, Typeable)
535−deriving instance (Data t, Data (SwaggerType t), Data (SwaggerItems t)) => Data (ParamSchema t)
608+deriving instance (Typeable k, Data (SwaggerKindType k), Data (SwaggerType k), Data (SwaggerItems k)) => Data (ParamSchema k)
537data Xml = Xml
610data Xml = Xml
538 { -- | Replaces the name of the element/attribute used for the described schema property.
611 { -- | Replaces the name of the element/attribute used for the described schema property.
@@ -574,7 +647,7 @@ data Responses = Responses
575 -- | Any HTTP status code can be used as the property name (one property per HTTP status code).
648 -- | Any HTTP status code can be used as the property name (one property per HTTP status code).
576 -- Describes the expected response for those HTTP status codes.
649 -- Describes the expected response for those HTTP status codes.
577− , _responsesResponses :: HashMap HttpStatusCode (Referenced Response)
650+ , _responsesResponses :: InsOrdHashMap HttpStatusCode (Referenced Response)
578 } deriving (Eq, Show, Generic, Data, Typeable)
651 } deriving (Eq, Show, Generic, Data, Typeable)
580type HttpStatusCode = Int
653type HttpStatusCode = Int
@@ -593,7 +666,7 @@ data Response = Response
593 , _responseSchema :: Maybe (Referenced Schema)
666 , _responseSchema :: Maybe (Referenced Schema)
595 -- | A list of headers that are sent with the response.
668 -- | A list of headers that are sent with the response.
596− , _responseHeaders :: HashMap HeaderName Header
669+ , _responseHeaders :: InsOrdHashMap HeaderName Header
598 -- | An example of the response message.
671 -- | An example of the response message.
599 , _responseExamples :: Maybe Example
672 , _responseExamples :: Maybe Example
@@ -608,7 +681,7 @@ data Header = Header
608 { -- | A short description of the header.
681 { -- | A short description of the header.
609 _headerDescription :: Maybe Text
682 _headerDescription :: Maybe Text
611− , _headerParamSchema :: ParamSchema Header
684+ , _headerParamSchema :: ParamSchema ('SwaggerKindNormal Header)
612 } deriving (Eq, Show, Generic, Data, Typeable)
685 } deriving (Eq, Show, Generic, Data, Typeable)
614data Example = Example { getExample :: Map MediaType Value }
687data Example = Example { getExample :: Map MediaType Value }
@@ -659,7 +732,7 @@ data OAuth2Params = OAuth2Params
659 _oauth2Flow :: OAuth2Flow
732 _oauth2Flow :: OAuth2Flow
661 -- | The available scopes for the OAuth2 security scheme.
734 -- | The available scopes for the OAuth2 security scheme.
662− , _oauth2Scopes :: HashMap Text Text
735+ , _oauth2Scopes :: InsOrdHashMap Text Text
663 } deriving (Eq, Show, Generic, Data, Typeable)
736 } deriving (Eq, Show, Generic, Data, Typeable)
665data SecuritySchemeType
738data SecuritySchemeType
@@ -680,7 +753,7 @@ data SecurityScheme = SecurityScheme
680-- The object can have multiple security schemes declared in it which are all required
753-- The object can have multiple security schemes declared in it which are all required
681-- (that is, there is a logical AND between the schemes).
754-- (that is, there is a logical AND between the schemes).
682newtype SecurityRequirement = SecurityRequirement
755newtype SecurityRequirement = SecurityRequirement
683− { getSecurityRequirement :: HashMap Text [Text]
756+ { getSecurityRequirement :: InsOrdHashMap Text [Text]
684 } deriving (Eq, Read, Show, Monoid, ToJSON, FromJSON, Data, Typeable)
757 } deriving (Eq, Read, Show, Monoid, ToJSON, FromJSON, Data, Typeable)
686-- | Tag name.
759-- | Tag name.
@@ -814,9 +887,9 @@ instance SwaggerMonoid ParamLocation where
814 swaggerMempty = ParamQuery
887 swaggerMempty = ParamQuery
815 swaggerMappend _ y = y
888 swaggerMappend _ y = y
817−instance OVERLAPPING_ SwaggerMonoid (HashMap FilePath PathItem) where
890+instance OVERLAPPING_ SwaggerMonoid (InsOrdHashMap FilePath PathItem) where
818− swaggerMempty = HashMap.empty
891+ swaggerMempty = InsOrdHashMap.empty
819− swaggerMappend = HashMap.unionWith mappend
892+ swaggerMappend = InsOrdHashMap.unionWith mappend
821instance Monoid a => SwaggerMonoid (Referenced a) where
894instance Monoid a => SwaggerMonoid (Referenced a) where
822 swaggerMempty = Inline mempty
895 swaggerMempty = Inline mempty
@@ -914,7 +987,8 @@ instance ToJSON OAuth2Flow where
914 , "tokenUrl" .= tokenUrl ]
987 , "tokenUrl" .= tokenUrl ]
916instance ToJSON OAuth2Params where
989instance ToJSON OAuth2Params where
917− toJSON = omitEmpties . genericToJSONWithSub "flow" (jsonPrefix "oauth2")
990+ toJSON = sopSwaggerGenericToJSON
919instance ToJSON SecuritySchemeType where
993instance ToJSON SecuritySchemeType where
920 toJSON SecuritySchemeBasic
994 toJSON SecuritySchemeBasic
@@ -927,24 +1001,22 @@ instance ToJSON SecuritySchemeType where
927 <+> object [ "type" .= ("oauth2" :: Text) ]
1001 <+> object [ "type" .= ("oauth2" :: Text) ]
929instance ToJSON Swagger where
1003instance ToJSON Swagger where
930− toJSON = omitEmpties . addVersion . genericToJSON (jsonPrefix "swagger")
1004+ toJSON = sopSwaggerGenericToJSON
931− where
1005+ DEFINE_TOENCODING
932− addVersion (Object o) = Object (HashMap.insert "swagger" "2.0" o)
933− addVersion _ = error "impossible"
935instance ToJSON SecurityScheme where
1007instance ToJSON SecurityScheme where
936− toJSON = genericToJSONWithSub "type" (jsonPrefix "securityScheme")
1008+ toJSON = sopSwaggerGenericToJSON
938instance ToJSON Schema where
1011instance ToJSON Schema where
939− toJSON = omitEmptiesExcept f . genericToJSONWithSub "paramSchema" (jsonPrefix "schema")
1012+ toJSON = sopSwaggerGenericToJSON
940− where
1013+ DEFINE_TOENCODING
941− f "items" (Array _) = True
944instance ToJSON Header where
1015instance ToJSON Header where
945− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "header")
1016+ toJSON = sopSwaggerGenericToJSON
947−instance ToJSON (SwaggerItems t) where
1019+instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
948 toJSON (SwaggerItemsPrimitive fmt schema) = object
1020 toJSON (SwaggerItemsPrimitive fmt schema) = object
949 [ "collectionFormat" .= fmt
1021 [ "collectionFormat" .= fmt
950 , "items" .= schema ]
1022 , "items" .= schema ]
@@ -961,27 +1033,32 @@ instance ToJSON MimeList where
961 toJSON (MimeList xs) = toJSON (map show xs)
1033 toJSON (MimeList xs) = toJSON (map show xs)
963instance ToJSON Param where
1035instance ToJSON Param where
964− toJSON = genericToJSONWithSub "schema" (jsonPrefix "param")
1036+ toJSON = sopSwaggerGenericToJSON
966instance ToJSON ParamAnySchema where
1039instance ToJSON ParamAnySchema where
967 toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
1040 toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
968 toJSON (ParamOther s) = toJSON s
1041 toJSON (ParamOther s) = toJSON s
970instance ToJSON ParamOtherSchema where
1043instance ToJSON ParamOtherSchema where
971− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
1044+ toJSON = sopSwaggerGenericToJSON
973instance ToJSON Responses where
1047instance ToJSON Responses where
974− toJSON (Responses def rs) = omitEmpties $
1048+ toJSON = sopSwaggerGenericToJSON
975− toJSON (hashMapMapKeys show rs) <+> object [ "default" .= def ]
1049+ DEFINE_TOENCODING
977instance ToJSON Response where
1051instance ToJSON Response where
978− toJSON = omitEmpties . genericToJSON (jsonPrefix "response")
1052+ toJSON = sopSwaggerGenericToJSON
980instance ToJSON Operation where
1055instance ToJSON Operation where
981− toJSON = omitEmpties . genericToJSON (jsonPrefix "operation")
1056+ toJSON = sopSwaggerGenericToJSON
983instance ToJSON PathItem where
1059instance ToJSON PathItem where
984− toJSON = omitEmpties . genericToJSON (jsonPrefix "pathItem")
1060+ toJSON = sopSwaggerGenericToJSON
986instance ToJSON Example where
1063instance ToJSON Example where
987 toJSON = toJSON . Map.mapKeys show . getExample
1064 toJSON = toJSON . Map.mapKeys show . getExample
@@ -1014,11 +1091,10 @@ instance ToJSON (CollectionFormat t) where
1014 toJSON CollectionPipes = "pipes"
1091 toJSON CollectionPipes = "pipes"
1015 toJSON CollectionMulti = "multi"
1092 toJSON CollectionMulti = "multi"
1017−instance ToJSON (ParamSchema t) where
1094+instance ToJSON (ParamSchema k) where
1018− toJSON = omitEmptiesExcept f . genericToJSONWithSub "items" (jsonPrefix "paramSchema")
1095+ -- TODO: this is a bit fishy, why we need sub object only in `ToJSON`?
1019− where
1096+ toJSON = sopSwaggerGenericToJSONWithOpts $
1020− f "items" (Array _) = True
1097+ mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1023-- =======================================================================
1099-- =======================================================================
1024-- Manual FromJSON instances
1100-- Manual FromJSON instances
@@ -1038,7 +1114,7 @@ instance FromJSON OAuth2Flow where
1038 parseJSON _ = empty
1114 parseJSON _ = empty
1040instance FromJSON OAuth2Params where
1116instance FromJSON OAuth2Params where
1041− parseJSON = genericParseJSONWithSub "flow" (jsonPrefix "oauth2")
1117+ parseJSON = sopSwaggerGenericParseJSON
1043instance FromJSON SecuritySchemeType where
1119instance FromJSON SecuritySchemeType where
1044 parseJSON js@(Object o) = do
1120 parseJSON js@(Object o) = do
@@ -1051,38 +1127,28 @@ instance FromJSON SecuritySchemeType where
1051 parseJSON _ = empty
1127 parseJSON _ = empty
1053instance FromJSON Swagger where
1129instance FromJSON Swagger where
1054− parseJSON js@(Object o) = do
1130+ parseJSON = sopSwaggerGenericParseJSON
1055− (version :: Text) <- o .: "swagger"
1056− when (version /= "2.0") empty
1057− (genericParseJSON (jsonPrefix "swagger")
1058− `withDefaults` [ "consumes" .= (mempty :: MimeList)
1059− , "produces" .= (mempty :: MimeList)
1060− , "security" .= ([] :: [SecurityRequirement])
1061− , "tags" .= ([] :: [Tag])
1062− , "definitions" .= (mempty :: Definitions Schema)
1063− , "parameters" .= (mempty :: Definitions Param)
1064− , "responses" .= (mempty :: Definitions Response)
1065− , "securityDefinitions" .= (mempty :: Definitions SecurityScheme)
1067− parseJSON _ = empty
1069instance FromJSON SecurityScheme where
1132instance FromJSON SecurityScheme where
1070− parseJSON = genericParseJSONWithSub "type" (jsonPrefix "securityScheme")
1133+ parseJSON = sopSwaggerGenericParseJSON
1072instance FromJSON Schema where
1135instance FromJSON Schema where
1073− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "schema")
1136+ parseJSON = sopSwaggerGenericParseJSON
1074− `withDefaults` [ "properties" .= (mempty :: HashMap Text Schema)
1075− , "required" .= ([] :: [ParamName]) ]
1077instance FromJSON Header where
1138instance FromJSON Header where
1078− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "header")
1139+ parseJSON = sopSwaggerGenericParseJSON
1080−instance OVERLAPPABLE_ (FromJSON (CollectionFormat t), FromJSON (ParamSchema t)) => FromJSON (SwaggerItems t) where
1141+instance (FromJSON (CollectionFormat ('SwaggerKindNormal t)), FromJSON (ParamSchema ('SwaggerKindNormal t))) => FromJSON (SwaggerItems ('SwaggerKindNormal t)) where
1081 parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1142 parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1082 <$> o .:? "collectionFormat"
1143 <$> o .:? "collectionFormat"
1083 <*> (o .: "items" >>= parseJSON)
1144 <*> (o .: "items" >>= parseJSON)
1146+instance FromJSON (SwaggerItems 'SwaggerKindParamOtherSchema) where
1147+ parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1148+ <$> o .:? "collectionFormat"
1149+ <*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))
1085−instance OVERLAPPABLE_ FromJSON (SwaggerItems Schema) where
1151+instance FromJSON (SwaggerItems 'SwaggerKindSchema) where
1086 parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1152 parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1087 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
1153 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
1088 parseJSON _ = empty
1154 parseJSON _ = empty
@@ -1100,7 +1166,7 @@ instance FromJSON MimeList where
1100 parseJSON js = (MimeList . map fromString) <$> parseJSON js
1166 parseJSON js = (MimeList . map fromString) <$> parseJSON js
1102instance FromJSON Param where
1168instance FromJSON Param where
1103− parseJSON = genericParseJSONWithSub "schema" (jsonPrefix "param")
1169+ parseJSON = sopSwaggerGenericParseJSON
1105instance FromJSON ParamAnySchema where
1171instance FromJSON ParamAnySchema where
1106 parseJSON js@(Object o) = do
1172 parseJSON js@(Object o) = do
@@ -1113,12 +1179,12 @@ instance FromJSON ParamAnySchema where
1113 parseJSON _ = empty
1179 parseJSON _ = empty
1115instance FromJSON ParamOtherSchema where
1181instance FromJSON ParamOtherSchema where
1116− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
1182+ parseJSON = sopSwaggerGenericParseJSON
1118instance FromJSON Responses where
1184instance FromJSON Responses where
1119 parseJSON (Object o) = Responses
1185 parseJSON (Object o) = Responses
1120 <$> o .:? "default"
1186 <$> o .:? "default"
1121− <*> (parseJSON (Object (HashMap.delete "default" o)) >>= hashMapReadKeys)
1187+ <*> (parseJSON (Object (HashMap.delete "default" o)))
1122 parseJSON _ = empty
1188 parseJSON _ = empty
1124instance FromJSON Example where
1190instance FromJSON Example where
@@ -1127,18 +1193,13 @@ instance FromJSON Example where
1127 pure $ Example (Map.mapKeys fromString m)
1193 pure $ Example (Map.mapKeys fromString m)
1129instance FromJSON Response where
1195instance FromJSON Response where
1130− parseJSON = genericParseJSON (jsonPrefix "response")
1196+ parseJSON = sopSwaggerGenericParseJSON
1131− `withDefaults` [ "headers" .= (mempty :: HashMap HeaderName Header) ]
1133instance FromJSON Operation where
1198instance FromJSON Operation where
1134− parseJSON = genericParseJSON (jsonPrefix "operation")
1199+ parseJSON = sopSwaggerGenericParseJSON
1135− `withDefaults` [ "security" .= ([] :: [SecurityRequirement])
1136− , "tags" .= ([] :: [Tag])
1137− , "parameters" .= ([] :: [Referenced Param]) ]
1139instance FromJSON PathItem where
1201instance FromJSON PathItem where
1140− parseJSON = genericParseJSON (jsonPrefix "pathItem")
1202+ parseJSON = sopSwaggerGenericParseJSON
1141− `withDefaults` [ "parameters" .= ([] :: [Param]) ]
1143instance FromJSON Reference where
1204instance FromJSON Reference where
1144 parseJSON (Object o) = Reference <$> o .: "$ref"
1205 parseJSON (Object o) = Reference <$> o .: "$ref"
@@ -1164,26 +1225,86 @@ instance FromJSON (Referenced Response) where parseJSON = referencedParseJSON "#
1164instance FromJSON Xml where
1225instance FromJSON Xml where
1165 parseJSON = genericParseJSON (jsonPrefix "xml")
1226 parseJSON = genericParseJSON (jsonPrefix "xml")
1167−instance FromJSON (SwaggerType Schema) where
1228+instance FromJSON (SwaggerType 'SwaggerKindSchema) where
1168 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
1229 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
1170−instance FromJSON (SwaggerType ParamOtherSchema) where
1231+instance FromJSON (SwaggerType 'SwaggerKindParamOtherSchema) where
1171 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerFile]
1232 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerFile]
1173−instance OVERLAPPABLE_ FromJSON (SwaggerType t) where
1234+instance FromJSON (SwaggerType ('SwaggerKindNormal t)) where
1174 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
1235 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
1176−instance OVERLAPPABLE_ FromJSON (CollectionFormat t) where
1237+instance FromJSON (CollectionFormat ('SwaggerKindNormal t)) where
1177 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1238 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1240+-- NOTE: There aren't collections of 'Schema'
1179−instance FromJSON (CollectionFormat ParamOtherSchema) where
1241+--instance FromJSON (CollectionFormat (SwaggerKindSchema)) where
1180− parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
1242+-- parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1182−-- NOTE: The constraints @FromJSON (SwaggerType t)@ and
1244+instance FromJSON (CollectionFormat 'SwaggerKindParamOtherSchema) where
1183−-- @FromJSON (SwaggerItems t)@ are necessary here!
1245+ parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
1184−-- Without the constraint the general instance will be used
1185−-- that only accepts common types (i.e. NOT object, null or file)
1186−-- and primitive array items.
1187−instance (FromJSON (SwaggerType t), FromJSON (SwaggerItems t)) => FromJSON (ParamSchema t) where
1188− parseJSON = genericParseJSONWithSub "items" (jsonPrefix "ParamSchema")
1247+instance (FromJSON (SwaggerType ('SwaggerKindNormal t)), FromJSON (SwaggerItems ('SwaggerKindNormal t))) => FromJSON (ParamSchema ('SwaggerKindNormal t)) where
1248+ parseJSON = sopSwaggerGenericParseJSON
1249+instance FromJSON (ParamSchema 'SwaggerKindParamOtherSchema) where
1250+ parseJSON = sopSwaggerGenericParseJSON
1251+instance FromJSON (ParamSchema 'SwaggerKindSchema) where
1252+ parseJSON = sopSwaggerGenericParseJSON
1254+-------------------------------------------------------------------------------
1256+-------------------------------------------------------------------------------
1258+deriveGeneric ''Header
1259+deriveGeneric ''OAuth2Params
1260+deriveGeneric ''Operation
1261+deriveGeneric ''Param
1262+deriveGeneric ''ParamOtherSchema
1263+deriveGeneric ''PathItem
1264+deriveGeneric ''Response
1265+deriveGeneric ''Responses
1266+deriveGeneric ''SecurityScheme
1267+deriveGeneric ''Schema
1268+deriveGeneric ''ParamSchema
1269+deriveGeneric ''Swagger
1271+instance HasSwaggerAesonOptions Header where
1272+ swaggerAesonOptions _ = mkSwaggerAesonOptions "header" & saoSubObject ?~ "paramSchema"
1273+instance HasSwaggerAesonOptions OAuth2Params where
1274+ swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2" & saoSubObject ?~ "flow"
1275+instance HasSwaggerAesonOptions Operation where
1276+ swaggerAesonOptions _ = mkSwaggerAesonOptions "operation"
1277+instance HasSwaggerAesonOptions Param where
1278+ swaggerAesonOptions _ = mkSwaggerAesonOptions "param" & saoSubObject ?~ "schema"
1279+instance HasSwaggerAesonOptions ParamOtherSchema where
1280+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramOtherSchema" & saoSubObject ?~ "paramSchema"
1281+instance HasSwaggerAesonOptions PathItem where
1282+ swaggerAesonOptions _ = mkSwaggerAesonOptions "pathItem"
1283+instance HasSwaggerAesonOptions Response where
1284+ swaggerAesonOptions _ = mkSwaggerAesonOptions "response"
1285+instance HasSwaggerAesonOptions Responses where
1286+ swaggerAesonOptions _ = mkSwaggerAesonOptions "responses" & saoSubObject ?~ "responses"
1287+instance HasSwaggerAesonOptions SecurityScheme where
1288+ swaggerAesonOptions _ = mkSwaggerAesonOptions "securityScheme" & saoSubObject ?~ "type"
1289+instance HasSwaggerAesonOptions Schema where
1290+ swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject ?~ "paramSchema"
1291+instance HasSwaggerAesonOptions Swagger where
1292+ swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("swagger", "2.0")]
1294+instance HasSwaggerAesonOptions (ParamSchema ('SwaggerKindNormal t)) where
1295+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1296+instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindParamOtherSchema) where
1297+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1298+-- NOTE: Schema doesn't have 'items' sub object!
1299+instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindSchema) where
1300+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema"
1302+instance AesonDefaultValue (ParamSchema s)
1303+instance AesonDefaultValue OAuth2Flow
1304+instance AesonDefaultValue Responses
1305+instance AesonDefaultValue ParamAnySchema
1306+instance AesonDefaultValue SecuritySchemeType
1307+instance AesonDefaultValue (SwaggerType a)
1308+instance AesonDefaultValue MimeList where defaultValue = Just mempty
1309+instance AesonDefaultValue Info
1310+instance AesonDefaultValue ParamLocation