git.iko.soy/typeable/openapi3
on master help sign in
Replace collection formats with GADT
commita9cdd0f9e1d55795eab99cd83c7671bf770db732 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateTue 8 Dec 2015, 19:33 (11 years ago) parentf9c8629
2 files changed+32−23
src/Data/Swagger.hs−1 src/Data/Swagger/Internal.hs+32−22
src/Data/Swagger.hs−1
@@ -30,7 +30,6 @@ module Data.Swagger (
3030 SwaggerType(..),
3131 Format,
3232 CollectionFormat(..),
33− ItemsCollectionFormat(..),
3433
3534 -- * Parameters
3635 Param(..),
src/Data/Swagger/Internal.hs+32−22
@@ -292,7 +292,7 @@ data ParamOtherSchema = ParamOtherSchema
292292
293293 -- | Determines the format of the array if @'ParamArray'@ is used.
294294 -- Default value is csv.
295− , _paramOtherSchemaCollectionFormat :: Maybe CollectionFormat
295+ , _paramOtherSchemaCollectionFormat :: Maybe (CollectionFormat Param)
296296
297297 , _paramOtherSchemaCommon :: SchemaCommon (SwaggerType Param) Items
298298 } deriving (Eq, Show, Generic)
@@ -333,23 +333,22 @@ data ParamLocation
333333type Format = Text
334334
335335-- | Determines the format of the array.
336−data CollectionFormat
337− = CollectionCSV -- ^ Comma separated values: @foo,bar@.
338− | CollectionSSV -- ^ Space separated values: @foo bar@.
339− | CollectionTSV -- ^ Tab separated values: @foo\\tbar@.
340− | CollectionPipes -- ^ Pipe separated values: @foo|bar@.
341− | CollectionMulti -- ^ Corresponds to multiple parameter instances
342− -- instead of multiple values for a single instance @foo=bar&foo=baz@.
343− -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
344− deriving (Eq, Show)
345−
346−-- | Determines the format of the nested array.
347−data ItemsCollectionFormat
348− = ItemsCollectionCSV -- ^ Comma separated values: @foo,bar@.
349− | ItemsCollectionSSV -- ^ Space separated values: @foo bar@.
350− | ItemsCollectionTSV -- ^ Tab separated values: @foo\\tbar@.
351− | ItemsCollectionPipes -- ^ Pipe separated values: @foo|bar@.
352− deriving (Eq, Show)
336+data CollectionFormat t where
337+ -- Comma separated values: @foo,bar@.
338+ CollectionCSV :: CollectionFormat t
339+ -- Space separated values: @foo bar@.
340+ CollectionSSV :: CollectionFormat t
341+ -- Tab separated values: @foo\\tbar@.
342+ CollectionTSV :: CollectionFormat t
343+ -- Pipe separated values: @foo|bar@.
344+ CollectionPipes :: CollectionFormat t
345+ -- Corresponds to multiple parameter instances
346+ -- instead of multiple values for a single instance @foo=bar&foo=baz@.
347+ -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
348+ CollectionMulti :: CollectionFormat Param
349+
350+deriving instance Eq (CollectionFormat t)
351+deriving instance Show (CollectionFormat t)
353352
354353type ParamName = Text
355354
@@ -435,7 +434,7 @@ data Xml = Xml
435434data Items = Items
436435 { -- | Determines the format of the array if type array is used.
437436 -- Default value is @'ItemsCollectionCSV'@.
438− _itemsCollectionFormat :: Maybe ItemsCollectionFormat
437+ _itemsCollectionFormat :: Maybe (CollectionFormat Items)
439438
440439 , _itemsCommon :: SchemaCommon (SwaggerType Items) Items
441440 } deriving (Eq, Show, Generic)
@@ -485,7 +484,7 @@ data Header = Header
485484
486485 -- | Determines the format of the array if type array is used.
487486 -- Default value is @'ItemsCollectionCSV'@.
488− , _headerCollectionFormat :: Maybe ItemsCollectionFormat
487+ , _headerCollectionFormat :: Maybe (CollectionFormat Items)
489488
490489 , _headerCommon :: SchemaCommon (SwaggerType Items) Items
491490 } deriving (Eq, Show, Generic)
@@ -715,8 +714,6 @@ deriveJSON (jsonPrefix "Param") ''ParamLocation
715714deriveJSON' ''Info
716715deriveJSON' ''Contact
717716deriveJSON' ''License
718−deriveJSON (jsonPrefix "ItemsCollection") ''ItemsCollectionFormat
719−deriveJSON (jsonPrefix "Collection") ''CollectionFormat
720717deriveJSON (jsonPrefix "ApiKey") ''ApiKeyLocation
721718deriveJSON (jsonPrefix "apiKey") ''ApiKeyParams
722719deriveJSON' ''SchemaCommon
@@ -828,6 +825,13 @@ instance ToJSON (SwaggerType t) where
828825 toJSON SwaggerNull = "null"
829826 toJSON SwaggerObject = "object"
830827
828+instance ToJSON (CollectionFormat t) where
829+ toJSON CollectionCSV = "csv"
830+ toJSON CollectionSSV = "ssv"
831+ toJSON CollectionTSV = "tsv"
832+ toJSON CollectionPipes = "pipes"
833+ toJSON CollectionMulti = "multi"
834+
831835-- =======================================================================
832836-- Manual FromJSON instances
833837-- =======================================================================
@@ -969,3 +973,9 @@ instance FromJSON (SwaggerType Param) where
969973instance FromJSON (SwaggerType Items) where
970974 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
971975
976+instance FromJSON (CollectionFormat Param) where
977+ parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
978+
979+instance FromJSON (CollectionFormat Items) where
980+ parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
981+