@@ -10,6 +10,7 @@
10{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE ScopedTypeVariables #-}
11{-# LANGUAGE StandaloneDeriving #-}
11{-# LANGUAGE StandaloneDeriving #-}
12{-# LANGUAGE TemplateHaskell #-}
12{-# LANGUAGE TemplateHaskell #-}
13+{-# LANGUAGE UndecidableInstances #-}
13module Data.Swagger.Internal where
14module Data.Swagger.Internal where
15import Control.Applicative
16import Control.Applicative
@@ -324,9 +325,33 @@ data ParamOtherSchema = ParamOtherSchema
324 -- Default value is csv.
325 -- Default value is csv.
325 , _paramOtherSchemaCollectionFormat :: Maybe (CollectionFormat Param)
326 , _paramOtherSchemaCollectionFormat :: Maybe (CollectionFormat Param)
327− , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema Items
328+ , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema
328 } deriving (Eq, Show, Generic, Data, Typeable)
329 } deriving (Eq, Show, Generic, Data, Typeable)
331+data SwaggerItems t where
332+ SwaggerItemsPrimitive :: Items -> SwaggerItems t
333+ SwaggerItemsObject :: Referenced Schema -> SwaggerItems Schema
334+ SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems Schema
336+deriving instance Eq (SwaggerItems t)
337+deriving instance Show (SwaggerItems t)
338+deriving instance Typeable (SwaggerItems t)
340+swaggerItemsPrimitiveConstr :: Constr
341+swaggerItemsPrimitiveConstr = mkConstr swaggerItemsDataType "SwaggerItemsPrimitive" [] Prefix
343+swaggerItemsDataType :: DataType
344+swaggerItemsDataType = mkDataType "Data.Swagger.SwaggerItems" [swaggerItemsPrimitiveConstr]
346+instance {-# OVERLAPPABLE #-} Typeable t => Data (SwaggerItems t) where
347+ gunfold k z c = case constrIndex c of
348+ 1 -> k (z SwaggerItemsPrimitive)
349+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems t)."
350+ toConstr _ = swaggerItemsPrimitiveConstr
351+ dataTypeOf _ = swaggerItemsDataType
353+deriving instance Data (SwaggerItems Schema)
330data SwaggerType t where
355data SwaggerType t where
331 SwaggerString :: SwaggerType t
356 SwaggerString :: SwaggerType t
332 SwaggerNumber :: SwaggerType t
357 SwaggerNumber :: SwaggerType t
@@ -453,15 +478,10 @@ data Schema = Schema
453 , _schemaMaxProperties :: Maybe Integer
478 , _schemaMaxProperties :: Maybe Integer
454 , _schemaMinProperties :: Maybe Integer
479 , _schemaMinProperties :: Maybe Integer
456− , _schemaParamSchema :: ParamSchema Schema SchemaItems
481+ , _schemaParamSchema :: ParamSchema Schema
457 } deriving (Eq, Show, Generic, Data, Typeable)
482 } deriving (Eq, Show, Generic, Data, Typeable)
460− = SchemaItemsObject (Referenced Schema)
461− | SchemaItemsArray [Referenced Schema]
462− deriving (Eq, Show, Generic, Data, Typeable)
464−data ParamSchema t items = ParamSchema
484+data ParamSchema t = ParamSchema
465 { -- | Declares the value of the parameter that the server will use if none is provided,
485 { -- | Declares the value of the parameter that the server will use if none is provided,
466 -- for example a @"count"@ to control the number of results per page might default to @100@
486 -- for example a @"count"@ to control the number of results per page might default to @100@
467 -- if not supplied by the client in the request.
487 -- if not supplied by the client in the request.
@@ -471,7 +491,7 @@ data ParamSchema t items = ParamSchema
472 , _paramSchemaType :: SwaggerType t
492 , _paramSchemaType :: SwaggerType t
473 , _paramSchemaFormat :: Maybe Format
493 , _paramSchemaFormat :: Maybe Format
474− , _paramSchemaItems :: Maybe items
494+ , _paramSchemaItems :: Maybe (SwaggerItems t)
475 , _paramSchemaMaximum :: Maybe Scientific
495 , _paramSchemaMaximum :: Maybe Scientific
476 , _paramSchemaExclusiveMaximum :: Maybe Bool
496 , _paramSchemaExclusiveMaximum :: Maybe Bool
477 , _paramSchemaMinimum :: Maybe Scientific
497 , _paramSchemaMinimum :: Maybe Scientific
@@ -486,7 +506,7 @@ data ParamSchema t items = ParamSchema
486 , _paramSchemaMultipleOf :: Maybe Scientific
506 , _paramSchemaMultipleOf :: Maybe Scientific
487 } deriving (Eq, Show, Generic, Typeable)
507 } deriving (Eq, Show, Generic, Typeable)
489−deriving instance (Data t, Data (SwaggerType t), Data i) => Data (ParamSchema t i)
509+deriving instance (Data t, Data (SwaggerType t), Data (SwaggerItems t)) => Data (ParamSchema t)
491data Xml = Xml
511data Xml = Xml
492 { -- | Replaces the name of the element/attribute used for the described schema property.
512 { -- | Replaces the name of the element/attribute used for the described schema property.
@@ -521,7 +541,7 @@ data Items = Items
521 -- Default value is @'ItemsCollectionCSV'@.
541 -- Default value is @'ItemsCollectionCSV'@.
522 _itemsCollectionFormat :: Maybe (CollectionFormat Items)
542 _itemsCollectionFormat :: Maybe (CollectionFormat Items)
524− , _itemsParamSchema :: ParamSchema Items Items
544+ , _itemsParamSchema :: ParamSchema Items
525 } deriving (Eq, Show, Generic, Data, Typeable)
545 } deriving (Eq, Show, Generic, Data, Typeable)
527-- | A container for the expected responses of an operation.
547-- | A container for the expected responses of an operation.
@@ -571,7 +591,7 @@ data Header = Header
571 -- Default value is @'ItemsCollectionCSV'@.
591 -- Default value is @'ItemsCollectionCSV'@.
572 , _headerCollectionFormat :: Maybe (CollectionFormat Items)
592 , _headerCollectionFormat :: Maybe (CollectionFormat Items)
574− , _headerParamSchema :: ParamSchema Header Items
594+ , _headerParamSchema :: ParamSchema Header
575 } deriving (Eq, Show, Generic, Data, Typeable)
595 } deriving (Eq, Show, Generic, Data, Typeable)
577data Example = Example { getExample :: Map MediaType Value }
597data Example = Example { getExample :: Map MediaType Value }
@@ -706,7 +726,7 @@ instance Monoid Schema where
706 mempty = genericMempty
726 mempty = genericMempty
707 mappend = genericMappend
727 mappend = genericMappend
709−instance Monoid (ParamSchema t i) where
729+instance Monoid (ParamSchema t) where
710 mempty = genericMempty
730 mempty = genericMempty
711 mappend = genericMappend
731 mappend = genericMappend
@@ -742,7 +762,7 @@ instance SwaggerMonoid Info
742instance SwaggerMonoid Paths
762instance SwaggerMonoid Paths
743instance SwaggerMonoid PathItem
763instance SwaggerMonoid PathItem
744instance SwaggerMonoid Schema
764instance SwaggerMonoid Schema
745−instance SwaggerMonoid (ParamSchema t i)
765+instance SwaggerMonoid (ParamSchema t)
746instance SwaggerMonoid Param
766instance SwaggerMonoid Param
747instance SwaggerMonoid ParamOtherSchema
767instance SwaggerMonoid ParamOtherSchema
748instance SwaggerMonoid Responses
768instance SwaggerMonoid Responses
@@ -870,6 +890,11 @@ instance ToJSON Header where
870instance ToJSON Items where
890instance ToJSON Items where
871 toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "items")
891 toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "items")
893+instance ToJSON (SwaggerItems t) where
894+ toJSON (SwaggerItemsPrimitive x) = toJSON x
895+ toJSON (SwaggerItemsObject x) = toJSON x
896+ toJSON (SwaggerItemsArray x) = toJSON x
873instance ToJSON Host where
898instance ToJSON Host where
874 toJSON (Host host mport) = toJSON $
899 toJSON (Host host mport) = toJSON $
875 case mport of
900 case mport of
@@ -892,10 +917,6 @@ instance ToJSON ParamAnySchema where
892instance ToJSON ParamOtherSchema where
917instance ToJSON ParamOtherSchema where
893 toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
918 toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
895−instance ToJSON SchemaItems where
896− toJSON (SchemaItemsObject x) = toJSON x
897− toJSON (SchemaItemsArray xs) = toJSON xs
899instance ToJSON Responses where
920instance ToJSON Responses where
900 toJSON (Responses def rs) = omitEmpties $
921 toJSON (Responses def rs) = omitEmpties $
901 toJSON (hashMapMapKeys show rs) <+> object [ "default" .= def ]
922 toJSON (hashMapMapKeys show rs) <+> object [ "default" .= def ]
@@ -940,7 +961,7 @@ instance ToJSON (CollectionFormat t) where
940 toJSON CollectionPipes = "pipes"
961 toJSON CollectionPipes = "pipes"
941 toJSON CollectionMulti = "multi"
962 toJSON CollectionMulti = "multi"
943−instance ToJSON i => ToJSON (ParamSchema t i) where
964+instance ToJSON (ParamSchema t) where
944 toJSON = genericToJSON (jsonPrefix "paramSchema")
965 toJSON = genericToJSON (jsonPrefix "paramSchema")
946-- =======================================================================
967-- =======================================================================
@@ -1003,6 +1024,14 @@ instance FromJSON Header where
1003instance FromJSON Items where
1024instance FromJSON Items where
1004 parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "items")
1025 parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "items")
1027+instance {-# OVERLAPPABLE #-} FromJSON (SwaggerItems t) where
1028+ parseJSON js = SwaggerItemsPrimitive <$> parseJSON js
1030+instance {-# OVERLAPPING #-} FromJSON (SwaggerItems Schema) where
1031+ parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1032+ parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
1033+ parseJSON _ = empty
1006instance FromJSON Host where
1035instance FromJSON Host where
1007 parseJSON (String s) =
1036 parseJSON (String s) =
1008 case fromInteger <$> readMaybe portStr of
1037 case fromInteger <$> readMaybe portStr of
@@ -1035,11 +1064,6 @@ instance FromJSON ParamAnySchema where
1035instance FromJSON ParamOtherSchema where
1064instance FromJSON ParamOtherSchema where
1036 parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
1065 parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
1038−instance FromJSON SchemaItems where
1039− parseJSON js@(Object _) = SchemaItemsObject <$> parseJSON js
1040− parseJSON js@(Array _) = SchemaItemsArray <$> parseJSON js
1041− parseJSON _ = empty
1043instance FromJSON Responses where
1067instance FromJSON Responses where
1044 parseJSON (Object o) = Responses
1068 parseJSON (Object o) = Responses
1045 <$> o .:? "default"
1069 <$> o .:? "default"
@@ -1101,8 +1125,10 @@ instance FromJSON (CollectionFormat Param) where
1101instance FromJSON (CollectionFormat Items) where
1125instance FromJSON (CollectionFormat Items) where
1102 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1126 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1104−-- NOTE: The constraint @FromJSON (SwaggerType t)@ is necessary here!
1128+-- NOTE: The constraints @FromJSON (SwaggerType t)@ and
1129+-- @FromJSON (SwaggerItems t)@ are necessary here!
1105-- Without the constraint the general instance will be used
1130-- Without the constraint the general instance will be used
1106−-- that only accepts common types (i.e. NOT object, null or file).
1131+-- that only accepts common types (i.e. NOT object, null or file)
1132+-- and primitive array items.
1107−instance (FromJSON (SwaggerType t), FromJSON i) => FromJSON (ParamSchema t i) where
1133+instance (FromJSON (SwaggerType t), FromJSON (SwaggerItems t)) => FromJSON (ParamSchema t) where
1108 parseJSON = genericParseJSON (jsonPrefix "ParamSchema")
1134 parseJSON = genericParseJSON (jsonPrefix "ParamSchema")