@@ -8,6 +8,7 @@
8{-# LANGUAGE GADTs #-}
8{-# LANGUAGE GADTs #-}
9{-# LANGUAGE GeneralizedNewtypeDeriving #-}
9{-# LANGUAGE GeneralizedNewtypeDeriving #-}
10{-# LANGUAGE OverloadedStrings #-}
10{-# LANGUAGE OverloadedStrings #-}
11+{-# LANGUAGE PolyKinds #-}
11{-# LANGUAGE RecordWildCards #-}
12{-# LANGUAGE RecordWildCards #-}
12{-# LANGUAGE ScopedTypeVariables #-}
13{-# LANGUAGE ScopedTypeVariables #-}
13{-# LANGUAGE StandaloneDeriving #-}
14{-# LANGUAGE StandaloneDeriving #-}
@@ -48,6 +49,10 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
49import Generics.SOP.TH (deriveGeneric)
50import Generics.SOP.TH (deriveGeneric)
50import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
51import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
52+ ,sopSwaggerGenericToJSONWithOpts
53+ ,sopSwaggerGenericParseJSON
54+ ,HasSwaggerAesonOptions(..)
55+ ,AesonDefaultValue(..)
51 ,mkSwaggerAesonOptions
56 ,mkSwaggerAesonOptions
52 ,saoAdditionalPairs
57 ,saoAdditionalPairs
53 ,saoSubObject)
58 ,saoSubObject)
@@ -343,8 +348,8 @@ data ParamOtherSchema = ParamOtherSchema
343 -- Default value is @False@.
348 -- Default value is @False@.
344 , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
349 , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
346− , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema
351+ , _paramOtherSchemaParamSchema :: ParamSchema SwaggerKindParamOtherSchema
347− } deriving (Eq, Show, Generic, Data, Typeable)
352+ } deriving (Eq, Show, Generic, Typeable, Data)
349-- | Items for @'SwaggerArray'@ schemas.
354-- | Items for @'SwaggerArray'@ schemas.
@@ -357,9 +362,9 @@ data ParamOtherSchema = ParamOtherSchema
358-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
363-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
359data SwaggerItems t where
364data SwaggerItems t where
360− SwaggerItemsPrimitive :: Maybe (CollectionFormat t) -> ParamSchema t -> SwaggerItems t
365+ SwaggerItemsPrimitive :: Maybe (CollectionFormat k) -> ParamSchema k-> SwaggerItems k
361− SwaggerItemsObject :: Referenced Schema -> SwaggerItems Schema
366+ SwaggerItemsObject :: Referenced Schema -> SwaggerItems SwaggerKindSchema
362− SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems Schema
367+ SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems SwaggerKindSchema
363 deriving (Typeable)
368 deriving (Typeable)
365deriving instance Eq (SwaggerItems t)
370deriving instance Eq (SwaggerItems t)
@@ -369,17 +374,66 @@ deriving instance Show (SwaggerItems t)
369swaggerItemsPrimitiveConstr :: Constr
374swaggerItemsPrimitiveConstr :: Constr
370swaggerItemsPrimitiveConstr = mkConstr swaggerItemsDataType "SwaggerItemsPrimitive" [] Prefix
375swaggerItemsPrimitiveConstr = mkConstr swaggerItemsDataType "SwaggerItemsPrimitive" [] Prefix
377+swaggerItemsObjectConstr :: Constr
378+swaggerItemsObjectConstr = mkConstr swaggerItemsDataType "SwaggerItemsObject" [] Prefix
380+swaggerItemsArrayConstr :: Constr
381+swaggerItemsArrayConstr = mkConstr swaggerItemsDataType "SwaggerItemsArray" [] Prefix
372swaggerItemsDataType :: DataType
383swaggerItemsDataType :: DataType
373swaggerItemsDataType = mkDataType "Data.Swagger.SwaggerItems" [swaggerItemsPrimitiveConstr]
384swaggerItemsDataType = mkDataType "Data.Swagger.SwaggerItems" [swaggerItemsPrimitiveConstr]
386+-- Note: unfortunately we have to write these Data instances by hand,
387+-- to get better contexts / avoid duplicate name when using standalone deriving
375−instance OVERLAPPABLE_ Data t => Data (SwaggerItems t) where
389+instance Data t => Data (SwaggerItems (SwaggerKindNormal t)) where
390+ -- TODO: define gfoldl
376 gunfold k z c = case constrIndex c of
391 gunfold k z c = case constrIndex c of
377 1 -> k (k (z SwaggerItemsPrimitive))
392 1 -> k (k (z SwaggerItemsPrimitive))
378 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems t)."
393 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems t)."
379 toConstr _ = swaggerItemsPrimitiveConstr
394 toConstr _ = swaggerItemsPrimitiveConstr
380 dataTypeOf _ = swaggerItemsDataType
395 dataTypeOf _ = swaggerItemsDataType
397+-- SwaggerItems SwaggerKindParamOtherSchema can be constructed using SwaggerItemsPrimitive only
382−deriving instance Data (SwaggerItems Schema)
398+instance Data (SwaggerItems SwaggerKindParamOtherSchema) where
399+ -- TODO: define gfoldl
400+ gunfold k z c = case constrIndex c of
401+ 1 -> k (k (z SwaggerItemsPrimitive))
402+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindParamOtherSchema)."
403+ toConstr _ = swaggerItemsPrimitiveConstr
404+ dataTypeOf _ = swaggerItemsDataType
406+instance Data (SwaggerItems SwaggerKindSchema) where
407+ gfoldl _ _ (SwaggerItemsPrimitive _ _) = error $ " Data.Data.gfoldl: Constructor SwaggerItemsPrimitive used to construct SwaggerItems SwaggerKindSchema"
408+ gfoldl k z (SwaggerItemsObject ref) = z SwaggerItemsObject `k` ref
409+ gfoldl k z (SwaggerItemsArray ref) = z SwaggerItemsArray `k` ref
411+ gunfold k z c = case constrIndex c of
412+ 1 -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindSchema)"
413+ 2 -> k (z SwaggerItemsObject)
414+ 3 -> k (z SwaggerItemsArray)
416+ toConstr (SwaggerItemsObject _) = swaggerItemsObjectConstr
417+ toConstr (SwaggerItemsArray _) = swaggerItemsArrayConstr
419+ dataTypeOf _ = swaggerItemsDataType
421+-- | Type used as a kind to avoid overlapping instances.
423+ = SwaggerKindNormal t
424+ | SwaggerKindParamOtherSchema
427+#if __GLASGLOW_HASKELL__ < 710
428+deriving instance Typeable 'SwaggerKindNormal
429+deriving instance Typeable 'SwaggerKindParamOtherSchema
430+deriving instance Typeable 'SwaggerKindSchema
433+type family SwaggerKindType (k :: SwaggerKind *) :: *
434+type instance SwaggerKindType (SwaggerKindNormal t) = t
435+type instance SwaggerKindType SwaggerKindSchema = Schema
436+type instance SwaggerKindType SwaggerKindParamOtherSchema = ParamOtherSchema
384data SwaggerType t where
438data SwaggerType t where
385 SwaggerString :: SwaggerType t
439 SwaggerString :: SwaggerType t
@@ -387,9 +441,9 @@ data SwaggerType t where
387 SwaggerInteger :: SwaggerType t
441 SwaggerInteger :: SwaggerType t
388 SwaggerBoolean :: SwaggerType t
442 SwaggerBoolean :: SwaggerType t
389 SwaggerArray :: SwaggerType t
443 SwaggerArray :: SwaggerType t
390− SwaggerFile :: SwaggerType ParamOtherSchema
444+ SwaggerFile :: SwaggerType SwaggerKindParamOtherSchema
391− SwaggerNull :: SwaggerType Schema
445+ SwaggerNull :: SwaggerType SwaggerKindSchema
392− SwaggerObject :: SwaggerType Schema
446+ SwaggerObject :: SwaggerType SwaggerKindSchema
393 deriving (Typeable)
447 deriving (Typeable)
395deriving instance Eq (SwaggerType t)
449deriving instance Eq (SwaggerType t)
@@ -401,30 +455,30 @@ swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
401swaggerTypeDataType :: Data (SwaggerType t) => SwaggerType t -> DataType
455swaggerTypeDataType :: Data (SwaggerType t) => SwaggerType t -> DataType
402swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
456swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
404−swaggerCommonTypes :: [SwaggerType t]
458+swaggerCommonTypes :: [SwaggerType k]
405swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
459swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
407−swaggerParamTypes :: [SwaggerType ParamOtherSchema]
461+swaggerParamTypes :: [SwaggerType SwaggerKindParamOtherSchema]
408swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
462swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
410−swaggerSchemaTypes :: [SwaggerType Schema]
464+swaggerSchemaTypes :: [SwaggerType SwaggerKindSchema]
411swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
465swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
413swaggerTypeConstrs :: [Constr]
467swaggerTypeConstrs :: [Constr]
414−swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType Schema])
468+swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType SwaggerKindSchema])
415 ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
469 ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
417−instance OVERLAPPABLE_ Typeable t => Data (SwaggerType t) where
471+instance Typeable t => Data (SwaggerType (SwaggerKindNormal t)) where
418 gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
472 gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
419 toConstr = swaggerTypeConstr
473 toConstr = swaggerTypeConstr
420 dataTypeOf = swaggerTypeDataType
474 dataTypeOf = swaggerTypeDataType
422−instance OVERLAPPABLE_ Data (SwaggerType ParamOtherSchema) where
476+instance Data (SwaggerType SwaggerKindParamOtherSchema) where
423 gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
477 gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
424 toConstr = swaggerTypeConstr
478 toConstr = swaggerTypeConstr
425 dataTypeOf = swaggerTypeDataType
479 dataTypeOf = swaggerTypeDataType
427−instance OVERLAPPABLE_ Data (SwaggerType Schema) where
481+instance Data (SwaggerType SwaggerKindSchema) where
428 gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
482 gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
429 toConstr = swaggerTypeConstr
483 toConstr = swaggerTypeConstr
430 dataTypeOf = swaggerTypeDataType
484 dataTypeOf = swaggerTypeDataType
@@ -464,7 +518,7 @@ data CollectionFormat t where
464 -- Corresponds to multiple parameter instances
518 -- Corresponds to multiple parameter instances
465 -- instead of multiple values for a single instance @foo=bar&foo=baz@.
519 -- instead of multiple values for a single instance @foo=bar&foo=baz@.
466 -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
520 -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
467− CollectionMulti :: CollectionFormat ParamOtherSchema
521+ CollectionMulti :: CollectionFormat SwaggerKindParamOtherSchema
468 deriving (Typeable)
522 deriving (Typeable)
470deriving instance Eq (CollectionFormat t)
524deriving instance Eq (CollectionFormat t)
@@ -480,12 +534,12 @@ collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat" $
480collectionCommonFormats :: [CollectionFormat t]
534collectionCommonFormats :: [CollectionFormat t]
481collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
535collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
483−instance OVERLAPPABLE_ Data t => Data (CollectionFormat t) where
537+instance Data t => Data (CollectionFormat (SwaggerKindNormal t)) where
484 gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
538 gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
485 toConstr = collectionFormatConstr
539 toConstr = collectionFormatConstr
486 dataTypeOf _ = collectionFormatDataType
540 dataTypeOf _ = collectionFormatDataType
488−deriving instance OVERLAPPABLE_ Data (CollectionFormat ParamOtherSchema)
542+deriving instance Data (CollectionFormat SwaggerKindParamOtherSchema)
490type ParamName = Text
544type ParamName = Text
@@ -507,7 +561,7 @@ data Schema = Schema
507 , _schemaMaxProperties :: Maybe Integer
561 , _schemaMaxProperties :: Maybe Integer
508 , _schemaMinProperties :: Maybe Integer
562 , _schemaMinProperties :: Maybe Integer
510− , _schemaParamSchema :: ParamSchema Schema
564+ , _schemaParamSchema :: ParamSchema SwaggerKindSchema
511 } deriving (Eq, Show, Generic, Data, Typeable)
565 } deriving (Eq, Show, Generic, Data, Typeable)
513-- | A @'Schema'@ with an optional name.
567-- | A @'Schema'@ with an optional name.
@@ -520,7 +574,7 @@ data NamedSchema = NamedSchema
520-- | Regex pattern for @string@ type.
574-- | Regex pattern for @string@ type.
521type Pattern = Text
575type Pattern = Text
523−data ParamSchema t = ParamSchema
577+data ParamSchema (t :: SwaggerKind *) = ParamSchema
524 { -- | Declares the value of the parameter that the server will use if none is provided,
578 { -- | Declares the value of the parameter that the server will use if none is provided,
525 -- for example a @"count"@ to control the number of results per page might default to @100@
579 -- for example a @"count"@ to control the number of results per page might default to @100@
526 -- if not supplied by the client in the request.
580 -- if not supplied by the client in the request.
@@ -545,7 +599,7 @@ data ParamSchema t = ParamSchema
545 , _paramSchemaMultipleOf :: Maybe Scientific
599 , _paramSchemaMultipleOf :: Maybe Scientific
546 } deriving (Eq, Show, Generic, Typeable)
600 } deriving (Eq, Show, Generic, Typeable)
548−deriving instance (Data t, Data (SwaggerType t), Data (SwaggerItems t)) => Data (ParamSchema t)
602+deriving instance (Typeable k, Data (SwaggerKindType k), Data (SwaggerType k), Data (SwaggerItems k)) => Data (ParamSchema k)
550data Xml = Xml
604data Xml = Xml
551 { -- | Replaces the name of the element/attribute used for the described schema property.
605 { -- | Replaces the name of the element/attribute used for the described schema property.
@@ -621,7 +675,7 @@ data Header = Header
621 { -- | A short description of the header.
675 { -- | A short description of the header.
622 _headerDescription :: Maybe Text
676 _headerDescription :: Maybe Text
624− , _headerParamSchema :: ParamSchema Header
678+ , _headerParamSchema :: ParamSchema (SwaggerKindNormal Header)
625 } deriving (Eq, Show, Generic, Data, Typeable)
679 } deriving (Eq, Show, Generic, Data, Typeable)
627data Example = Example { getExample :: Map MediaType Value }
681data Example = Example { getExample :: Map MediaType Value }
@@ -827,10 +881,6 @@ instance SwaggerMonoid ParamLocation where
827 swaggerMempty = ParamQuery
881 swaggerMempty = ParamQuery
828 swaggerMappend _ y = y
882 swaggerMappend _ y = y
830−instance OVERLAPPING_ SwaggerMonoid (HashMap FilePath PathItem) where
831− swaggerMempty = HashMap.empty
832− swaggerMappend = HashMap.unionWith mappend
834instance OVERLAPPING_ SwaggerMonoid (InsOrdHashMap FilePath PathItem) where
884instance OVERLAPPING_ SwaggerMonoid (InsOrdHashMap FilePath PathItem) where
835 swaggerMempty = InsOrdHashMap.empty
885 swaggerMempty = InsOrdHashMap.empty
836 swaggerMappend = InsOrdHashMap.unionWith mappend
886 swaggerMappend = InsOrdHashMap.unionWith mappend
@@ -931,8 +981,7 @@ instance ToJSON OAuth2Flow where
931 , "tokenUrl" .= tokenUrl ]
981 , "tokenUrl" .= tokenUrl ]
933instance ToJSON OAuth2Params where
983instance ToJSON OAuth2Params where
934− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "oauth2" &
984+ toJSON = sopSwaggerGenericToJSON
935− saoSubObject ?~ "flow"
937instance ToJSON SecuritySchemeType where
986instance ToJSON SecuritySchemeType where
938 toJSON SecuritySchemeBasic
987 toJSON SecuritySchemeBasic
@@ -945,22 +994,18 @@ instance ToJSON SecuritySchemeType where
945 <+> object [ "type" .= ("oauth2" :: Text) ]
994 <+> object [ "type" .= ("oauth2" :: Text) ]
947instance ToJSON Swagger where
996instance ToJSON Swagger where
948− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "swagger" &
997+ toJSON = sopSwaggerGenericToJSON
949− saoAdditionalPairs .~ [("swagger", "2.0")]
951instance ToJSON SecurityScheme where
999instance ToJSON SecurityScheme where
952− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "securityScheme" &
1000+ toJSON = sopSwaggerGenericToJSON
953− saoSubObject ?~ "type"
955instance ToJSON Schema where
1002instance ToJSON Schema where
956− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "schema" &
1003+ toJSON = sopSwaggerGenericToJSON
957− saoSubObject ?~ "paramSchema"
959instance ToJSON Header where
1005instance ToJSON Header where
960− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "header" &
1006+ toJSON = sopSwaggerGenericToJSON
961− saoSubObject ?~ "paramSchema"
963−instance ToJSON (SwaggerItems t) where
1008+instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
964 toJSON (SwaggerItemsPrimitive fmt schema) = object
1009 toJSON (SwaggerItemsPrimitive fmt schema) = object
965 [ "collectionFormat" .= fmt
1010 [ "collectionFormat" .= fmt
966 , "items" .= schema ]
1011 , "items" .= schema ]
@@ -977,29 +1022,26 @@ instance ToJSON MimeList where
977 toJSON (MimeList xs) = toJSON (map show xs)
1022 toJSON (MimeList xs) = toJSON (map show xs)
979instance ToJSON Param where
1024instance ToJSON Param where
980− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "param" &
1025+ toJSON = sopSwaggerGenericToJSON
981− saoSubObject ?~ "schema"
983instance ToJSON ParamAnySchema where
1027instance ToJSON ParamAnySchema where
984 toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
1028 toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
985 toJSON (ParamOther s) = toJSON s
1029 toJSON (ParamOther s) = toJSON s
987instance ToJSON ParamOtherSchema where
1031instance ToJSON ParamOtherSchema where
988− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "paramOtherSchema" &
1032+ toJSON = sopSwaggerGenericToJSON
989− saoSubObject ?~ "paramSchema"
991instance ToJSON Responses where
1034instance ToJSON Responses where
992− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "responses" &
1035+ toJSON = sopSwaggerGenericToJSON
993− saoSubObject ?~ "responses"
995instance ToJSON Response where
1037instance ToJSON Response where
996− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "response"
1038+ toJSON = sopSwaggerGenericToJSON
998instance ToJSON Operation where
1040instance ToJSON Operation where
999− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "operation"
1041+ toJSON = sopSwaggerGenericToJSON
1001instance ToJSON PathItem where
1043instance ToJSON PathItem where
1002− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "pathItem"
1044+ toJSON = sopSwaggerGenericToJSON
1004instance ToJSON Example where
1046instance ToJSON Example where
1005 toJSON = toJSON . Map.mapKeys show . getExample
1047 toJSON = toJSON . Map.mapKeys show . getExample
@@ -1032,9 +1074,10 @@ instance ToJSON (CollectionFormat t) where
1032 toJSON CollectionPipes = "pipes"
1074 toJSON CollectionPipes = "pipes"
1033 toJSON CollectionMulti = "multi"
1075 toJSON CollectionMulti = "multi"
1035−instance ToJSON (ParamSchema t) where
1077+instance ToJSON (ParamSchema k) where
1078+ -- TODO: this is a bit fishy, why we need sub object only in `ToJSON`?
1079+ toJSON = sopSwaggerGenericToJSONWithOpts $
1036− toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "paramSchema" &
1080+ mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1037− saoSubObject ?~ "items"
1039-- =======================================================================
1082-- =======================================================================
1040-- Manual FromJSON instances
1083-- Manual FromJSON instances
@@ -1054,7 +1097,7 @@ instance FromJSON OAuth2Flow where
1054 parseJSON _ = empty
1097 parseJSON _ = empty
1056instance FromJSON OAuth2Params where
1099instance FromJSON OAuth2Params where
1057− parseJSON = genericParseJSONWithSub "flow" (jsonPrefix "oauth2")
1100+ parseJSON = sopSwaggerGenericParseJSON
1059instance FromJSON SecuritySchemeType where
1102instance FromJSON SecuritySchemeType where
1060 parseJSON js@(Object o) = do
1103 parseJSON js@(Object o) = do
@@ -1067,38 +1110,28 @@ instance FromJSON SecuritySchemeType where
1067 parseJSON _ = empty
1110 parseJSON _ = empty
1069instance FromJSON Swagger where
1112instance FromJSON Swagger where
1070− parseJSON js@(Object o) = do
1113+ parseJSON = sopSwaggerGenericParseJSON
1071− (version :: Text) <- o .: "swagger"
1072− when (version /= "2.0") empty
1073− (genericParseJSON (jsonPrefix "swagger")
1074− `withDefaults` [ "consumes" .= (mempty :: MimeList)
1075− , "produces" .= (mempty :: MimeList)
1076− , "security" .= ([] :: [SecurityRequirement])
1077− , "tags" .= ([] :: [Tag])
1078− , "definitions" .= (mempty :: Definitions Schema)
1079− , "parameters" .= (mempty :: Definitions Param)
1080− , "responses" .= (mempty :: Definitions Response)
1081− , "securityDefinitions" .= (mempty :: Definitions SecurityScheme)
1083− parseJSON _ = empty
1085instance FromJSON SecurityScheme where
1115instance FromJSON SecurityScheme where
1086− parseJSON = genericParseJSONWithSub "type" (jsonPrefix "securityScheme")
1116+ parseJSON = sopSwaggerGenericParseJSON
1088instance FromJSON Schema where
1118instance FromJSON Schema where
1089− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "schema")
1119+ parseJSON = sopSwaggerGenericParseJSON
1090− `withDefaults` [ "properties" .= (mempty :: InsOrdHashMap Text Schema)
1091− , "required" .= ([] :: [ParamName]) ]
1093instance FromJSON Header where
1121instance FromJSON Header where
1094− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "header")
1122+ parseJSON = sopSwaggerGenericParseJSON
1096−instance OVERLAPPABLE_ (FromJSON (CollectionFormat t), FromJSON (ParamSchema t)) => FromJSON (SwaggerItems t) where
1124+instance (FromJSON (CollectionFormat (SwaggerKindNormal t)), FromJSON (ParamSchema (SwaggerKindNormal t))) => FromJSON (SwaggerItems (SwaggerKindNormal t)) where
1097 parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1125 parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1098 <$> o .:? "collectionFormat"
1126 <$> o .:? "collectionFormat"
1099 <*> (o .: "items" >>= parseJSON)
1127 <*> (o .: "items" >>= parseJSON)
1129+instance FromJSON (SwaggerItems SwaggerKindParamOtherSchema) where
1130+ parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1131+ <$> o .:? "collectionFormat"
1132+ <*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))
1101−instance OVERLAPPABLE_ FromJSON (SwaggerItems Schema) where
1134+instance FromJSON (SwaggerItems SwaggerKindSchema) where
1102 parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1135 parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1103 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
1136 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
1104 parseJSON _ = empty
1137 parseJSON _ = empty
@@ -1116,7 +1149,7 @@ instance FromJSON MimeList where
1116 parseJSON js = (MimeList . map fromString) <$> parseJSON js
1149 parseJSON js = (MimeList . map fromString) <$> parseJSON js
1118instance FromJSON Param where
1151instance FromJSON Param where
1119− parseJSON = genericParseJSONWithSub "schema" (jsonPrefix "param")
1152+ parseJSON = sopSwaggerGenericParseJSON
1121instance FromJSON ParamAnySchema where
1154instance FromJSON ParamAnySchema where
1122 parseJSON js@(Object o) = do
1155 parseJSON js@(Object o) = do
@@ -1129,7 +1162,7 @@ instance FromJSON ParamAnySchema where
1129 parseJSON _ = empty
1162 parseJSON _ = empty
1131instance FromJSON ParamOtherSchema where
1164instance FromJSON ParamOtherSchema where
1132− parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
1165+ parseJSON = sopSwaggerGenericParseJSON
1134instance FromJSON Responses where
1167instance FromJSON Responses where
1135 parseJSON (Object o) = Responses
1168 parseJSON (Object o) = Responses
@@ -1143,18 +1176,13 @@ instance FromJSON Example where
1143 pure $ Example (Map.mapKeys fromString m)
1176 pure $ Example (Map.mapKeys fromString m)
1145instance FromJSON Response where
1178instance FromJSON Response where
1146− parseJSON = genericParseJSON (jsonPrefix "response")
1179+ parseJSON = sopSwaggerGenericParseJSON
1147− `withDefaults` [ "headers" .= (mempty :: InsOrdHashMap HeaderName Header) ]
1149instance FromJSON Operation where
1181instance FromJSON Operation where
1150− parseJSON = genericParseJSON (jsonPrefix "operation")
1182+ parseJSON = sopSwaggerGenericParseJSON
1151− `withDefaults` [ "security" .= ([] :: [SecurityRequirement])
1152− , "tags" .= ([] :: [Tag])
1153− , "parameters" .= ([] :: [Referenced Param]) ]
1155instance FromJSON PathItem where
1184instance FromJSON PathItem where
1156− parseJSON = genericParseJSON (jsonPrefix "pathItem")
1185+ parseJSON = sopSwaggerGenericParseJSON
1157− `withDefaults` [ "parameters" .= ([] :: [Param]) ]
1159instance FromJSON Reference where
1187instance FromJSON Reference where
1160 parseJSON (Object o) = Reference <$> o .: "$ref"
1188 parseJSON (Object o) = Reference <$> o .: "$ref"
@@ -1180,28 +1208,31 @@ instance FromJSON (Referenced Response) where parseJSON = referencedParseJSON "#
1180instance FromJSON Xml where
1208instance FromJSON Xml where
1181 parseJSON = genericParseJSON (jsonPrefix "xml")
1209 parseJSON = genericParseJSON (jsonPrefix "xml")
1183−instance FromJSON (SwaggerType Schema) where
1211+instance FromJSON (SwaggerType SwaggerKindSchema) where
1184 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
1212 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
1186−instance FromJSON (SwaggerType ParamOtherSchema) where
1214+instance FromJSON (SwaggerType SwaggerKindParamOtherSchema) where
1187 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerFile]
1215 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerFile]
1189−instance OVERLAPPABLE_ FromJSON (SwaggerType t) where
1217+instance FromJSON (SwaggerType (SwaggerKindNormal t)) where
1190 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
1218 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
1192−instance OVERLAPPABLE_ FromJSON (CollectionFormat t) where
1220+instance FromJSON (CollectionFormat (SwaggerKindNormal t)) where
1193 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1221 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1223+-- NOTE: There aren't collections of 'Schema'
1224+--instance FromJSON (CollectionFormat (SwaggerKindSchema)) where
1225+-- parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1195−instance FromJSON (CollectionFormat ParamOtherSchema) where
1227+instance FromJSON (CollectionFormat SwaggerKindParamOtherSchema) where
1196 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
1228 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
1198−-- NOTE: The constraints @FromJSON (SwaggerType t)@ and
1230+instance (FromJSON (SwaggerType (SwaggerKindNormal t)), FromJSON (SwaggerItems (SwaggerKindNormal t))) => FromJSON (ParamSchema (SwaggerKindNormal t)) where
1199−-- @FromJSON (SwaggerItems t)@ are necessary here!
1231+ parseJSON = sopSwaggerGenericParseJSON
1200−-- Without the constraint the general instance will be used
1232+instance FromJSON (ParamSchema SwaggerKindParamOtherSchema) where
1201−-- that only accepts common types (i.e. NOT object, null or file)
1233+ parseJSON = sopSwaggerGenericParseJSON
1202−-- and primitive array items.
1234+instance FromJSON (ParamSchema SwaggerKindSchema) where
1203−instance (FromJSON (SwaggerType t), FromJSON (SwaggerItems t)) => FromJSON (ParamSchema t) where
1235+ parseJSON = sopSwaggerGenericParseJSON
1204− parseJSON = genericParseJSONWithSub "items" (jsonPrefix "ParamSchema")
1206-------------------------------------------------------------------------------
1237-------------------------------------------------------------------------------
1207-- TH splices
1238-- TH splices
@@ -1219,3 +1250,44 @@ deriveGeneric ''SecurityScheme
1219deriveGeneric ''Schema
1250deriveGeneric ''Schema
1220deriveGeneric ''ParamSchema
1251deriveGeneric ''ParamSchema
1221deriveGeneric ''Swagger
1252deriveGeneric ''Swagger
1254+instance HasSwaggerAesonOptions Header where
1255+ swaggerAesonOptions _ = mkSwaggerAesonOptions "header" & saoSubObject ?~ "paramSchema"
1256+instance HasSwaggerAesonOptions OAuth2Params where
1257+ swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2" & saoSubObject ?~ "flow"
1258+instance HasSwaggerAesonOptions Operation where
1259+ swaggerAesonOptions _ = mkSwaggerAesonOptions "operation"
1260+instance HasSwaggerAesonOptions Param where
1261+ swaggerAesonOptions _ = mkSwaggerAesonOptions "param" & saoSubObject ?~ "schema"
1262+instance HasSwaggerAesonOptions ParamOtherSchema where
1263+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramOtherSchema" & saoSubObject ?~ "paramSchema"
1264+instance HasSwaggerAesonOptions PathItem where
1265+ swaggerAesonOptions _ = mkSwaggerAesonOptions "pathItem"
1266+instance HasSwaggerAesonOptions Response where
1267+ swaggerAesonOptions _ = mkSwaggerAesonOptions "response"
1268+instance HasSwaggerAesonOptions Responses where
1269+ swaggerAesonOptions _ = mkSwaggerAesonOptions "responses" & saoSubObject ?~ "responses"
1270+instance HasSwaggerAesonOptions SecurityScheme where
1271+ swaggerAesonOptions _ = mkSwaggerAesonOptions "securityScheme" & saoSubObject ?~ "type"
1272+instance HasSwaggerAesonOptions Schema where
1273+ swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject ?~ "paramSchema"
1274+instance HasSwaggerAesonOptions Swagger where
1275+ swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("swagger", "2.0")]
1277+instance HasSwaggerAesonOptions (ParamSchema (SwaggerKindNormal t)) where
1278+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1279+instance HasSwaggerAesonOptions (ParamSchema SwaggerKindParamOtherSchema) where
1280+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1281+-- NOTE: Schema doesn't have 'items' sub object!
1282+instance HasSwaggerAesonOptions (ParamSchema SwaggerKindSchema) where
1283+ swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema"
1285+instance AesonDefaultValue (ParamSchema s)
1286+instance AesonDefaultValue OAuth2Flow
1287+instance AesonDefaultValue Responses
1288+instance AesonDefaultValue ParamAnySchema
1289+instance AesonDefaultValue SecuritySchemeType
1290+instance AesonDefaultValue (SwaggerType a)
1291+instance AesonDefaultValue MimeList where defaultValue = Just mempty
1292+instance AesonDefaultValue Info
1293+instance AesonDefaultValue ParamLocation