git.iko.soy/typeable/openapi3
on master help sign in
Merge pull request #17 from GetShopTV/to-param-#16
e89e06fNickolay Kudasov
src/Data/Swagger.hs+9−10
@@ -6,6 +6,7 @@
6-- and Swagger-Codegen to generate clients in various languages.
6-- and Swagger-Codegen to generate clients in various languages.
7-- Additional utilities can also take advantage of the resulting files, such as testing tools.
7-- Additional utilities can also take advantage of the resulting files, such as testing tools.
8module Data.Swagger (
8module Data.Swagger (
9+ module Data.Swagger.ParamSchema,
9 module Data.Swagger.Schema,
10 module Data.Swagger.Schema,
10
11
11 -- * Swagger specification
12 -- * Swagger specification
@@ -27,27 +28,24 @@ module Data.Swagger (
27 Operation(..),
28 Operation(..),
28
29
29 -- * Types and formats
30 -- * Types and formats
30− ParameterType(..),
31+ SwaggerType(..),
31− ItemsType(..),
32− SchemaType(..),
33 Format,
32 Format,
34 CollectionFormat(..),
33 CollectionFormat(..),
35− ItemsCollectionFormat(..),
36
34
37 -- * Parameters
35 -- * Parameters
38− Parameter(..),
36+ Param(..),
39− ParameterSchema(..),
37+ ParamAnySchema(..),
40− ParameterOtherSchema(..),
38+ ParamOtherSchema(..),
41− ParameterLocation(..),
39+ ParamLocation(..),
42 ParamName,
40 ParamName,
43 Items(..),
41 Items(..),
44 Header(..),
42 Header(..),
45 Example(..),
43 Example(..),
46
44
47− -- * Schema
45+ -- * Schemas
46+ ParamSchema(..),
48 Schema(..),
47 Schema(..),
49 SchemaItems(..),
48 SchemaItems(..),
50− SchemaCommon(..),
51 Xml(..),
49 Xml(..),
52
50
53 -- * Responses
51 -- * Responses
@@ -81,6 +79,7 @@ module Data.Swagger (
81 URL(..),
79 URL(..),
82) where
80) where
83
81
82+import Data.Swagger.ParamSchema
84import Data.Swagger.Schema
83import Data.Swagger.Schema
85
84
86import Data.Swagger.Internal
85import Data.Swagger.Internal
src/Data/Swagger/Internal.hs+155−173
@@ -1,6 +1,8 @@
1{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE DeriveTraversable #-}
2{-# LANGUAGE DeriveTraversable #-}
3+{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE GADTs #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE OverloadedStrings #-}
7{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE RecordWildCards #-}
8{-# LANGUAGE RecordWildCards #-}
@@ -64,7 +66,7 @@ data Swagger = Swagger
64
66
65 -- | An object to hold parameters that can be used across operations.
67 -- | An object to hold parameters that can be used across operations.
66 -- This property does not define global parameters for all operations.
68 -- This property does not define global parameters for all operations.
67− , _parameters :: HashMap Text Parameter
69+ , _parameters :: HashMap Text Param
68
70
69 -- | An object to hold responses that can be used across operations.
71 -- | An object to hold responses that can be used across operations.
70 -- This property does not define global responses for all operations.
72 -- This property does not define global responses for all operations.
@@ -187,7 +189,7 @@ data PathItem = PathItem
187 -- These parameters can be overridden at the operation level, but cannot be removed there.
189 -- These parameters can be overridden at the operation level, but cannot be removed there.
188 -- The list MUST NOT include duplicated parameters.
190 -- The list MUST NOT include duplicated parameters.
189 -- A unique parameter is defined by a combination of a name and location.
191 -- A unique parameter is defined by a combination of a name and location.
190− , _pathItemParameters :: [Referenced Parameter]
192+ , _pathItemParameters :: [Referenced Param]
191 } deriving (Eq, Show, Generic)
193 } deriving (Eq, Show, Generic)
192
194
193-- | Describes a single API operation on a path.
195-- | Describes a single API operation on a path.
@@ -228,7 +230,7 @@ data Operation = Operation
228 -- the new definition will override it, but can never remove it.
230 -- the new definition will override it, but can never remove it.
229 -- The list MUST NOT include duplicated parameters.
231 -- The list MUST NOT include duplicated parameters.
230 -- A unique parameter is defined by a combination of a name and location.
232 -- A unique parameter is defined by a combination of a name and location.
231− , _operationParameters :: [Referenced Parameter]
233+ , _operationParameters :: [Referenced Param]
232
234
233 -- | The list of possible responses as they are returned from executing this operation.
235 -- | The list of possible responses as they are returned from executing this operation.
234 , _operationResponses :: Responses
236 , _operationResponses :: Responses
@@ -255,81 +257,70 @@ newtype MimeList = MimeList { getMimeList :: [MediaType] }
255
257
256-- | Describes a single operation parameter.
258-- | Describes a single operation parameter.
257-- A unique parameter is defined by a combination of a name and location.
259-- A unique parameter is defined by a combination of a name and location.
258−data Parameter = Parameter
260+data Param = Param
259 { -- | The name of the parameter.
261 { -- | The name of the parameter.
260 -- Parameter names are case sensitive.
262 -- Parameter names are case sensitive.
261− _parameterName :: Text
263+ _paramName :: Text
262
264
263 -- | A brief description of the parameter.
265 -- | A brief description of the parameter.
264 -- This could contain examples of use.
266 -- This could contain examples of use.
265 -- GFM syntax can be used for rich text representation.
267 -- GFM syntax can be used for rich text representation.
266− , _parameterDescription :: Maybe Text
268+ , _paramDescription :: Maybe Text
267
269
268 -- | Determines whether this parameter is mandatory.
270 -- | Determines whether this parameter is mandatory.
269 -- If the parameter is in "path", this property is required and its value MUST be true.
271 -- If the parameter is in "path", this property is required and its value MUST be true.
270 -- Otherwise, the property MAY be included and its default value is @False@.
272 -- Otherwise, the property MAY be included and its default value is @False@.
271− , _parameterRequired :: Maybe Bool
273+ , _paramRequired :: Maybe Bool
272
274
273 -- | Parameter schema.
275 -- | Parameter schema.
274− , _parameterSchema :: ParameterSchema
276+ , _paramSchema :: ParamAnySchema
275 } deriving (Eq, Show, Generic)
277 } deriving (Eq, Show, Generic)
276
278
277−data ParameterSchema
279+data ParamAnySchema
278− = ParameterBody (Referenced Schema)
280+ = ParamBody (Referenced Schema)
279− | ParameterOther ParameterOtherSchema
281+ | ParamOther ParamOtherSchema
280 deriving (Eq, Show)
282 deriving (Eq, Show)
281
283
282−data ParameterOtherSchema = ParameterOtherSchema
284+data ParamOtherSchema = ParamOtherSchema
283 { -- | The location of the parameter.
285 { -- | The location of the parameter.
284− _parameterOtherSchemaIn :: ParameterLocation
286+ _paramOtherSchemaIn :: ParamLocation
285−
286− -- | The type of the parameter.
287− -- Since the parameter is not located at the request body,
288− -- it is limited to simple types (that is, not an object).
289− -- If type is @'ParamFile'@, the @consumes@ MUST be either
290− -- "multipart/form-data" or " application/x-www-form-urlencoded"
291− -- and the parameter MUST be in @'ParameterFormData'@.
292− , _parameterOtherSchemaType :: ParameterType
293−
294− -- | The extending format for the previously mentioned type.
295− , _parameterOtherSchemaFormat :: Maybe Format
296
287
297 -- | Sets the ability to pass empty-valued parameters.
288 -- | Sets the ability to pass empty-valued parameters.
298− -- This is valid only for either @'ParameterQuery'@ or @'ParameterFormData'@
289+ -- This is valid only for either @'ParamQuery'@ or @'ParamFormData'@
299 -- and allows you to send a parameter with a name only or an empty value.
290 -- and allows you to send a parameter with a name only or an empty value.
300 -- Default value is @False@.
291 -- Default value is @False@.
301− , _parameterOtherSchemaAllowEmptyValue :: Maybe Bool
292+ , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
302−
303− -- | __Required if type is @'ParamArray'@__.
304− -- Describes the type of items in the array.
305− , _parameterOtherSchemaItems :: Maybe Items
306
293
307 -- | Determines the format of the array if @'ParamArray'@ is used.
294 -- | Determines the format of the array if @'ParamArray'@ is used.
308 -- Default value is csv.
295 -- Default value is csv.
309− , _parameterOtherSchemaCollectionFormat :: Maybe CollectionFormat
296+ , _paramOtherSchemaCollectionFormat :: Maybe (CollectionFormat Param)
310
297
311− , _parameterOtherSchemaCommon :: SchemaCommon
298+ , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema Items
312 } deriving (Eq, Show, Generic)
299 } deriving (Eq, Show, Generic)
313
300
314−data ParameterType
301+data SwaggerType t where
315− = ParamString
302+ SwaggerString :: SwaggerType t
316− | ParamNumber
303+ SwaggerNumber :: SwaggerType t
317− | ParamInteger
304+ SwaggerInteger :: SwaggerType t
318− | ParamBoolean
305+ SwaggerBoolean :: SwaggerType t
319− | ParamArray
306+ SwaggerArray :: SwaggerType t
320− | ParamFile
307+ SwaggerFile :: SwaggerType ParamOtherSchema
321− deriving (Eq, Show)
308+ SwaggerNull :: SwaggerType Schema
309+ SwaggerObject :: SwaggerType Schema
322
310
323−data ParameterLocation
311+deriving instance Eq (SwaggerType t)
312+deriving instance Show (SwaggerType t)
313+
314+data ParamLocation
324 = -- | Parameters that are appended to the URL.
315 = -- | Parameters that are appended to the URL.
325 -- For example, in @/items?id=###@, the query parameter is @id@.
316 -- For example, in @/items?id=###@, the query parameter is @id@.
326− ParameterQuery
317+ ParamQuery
327 -- | Custom headers that are expected as part of the request.
318 -- | Custom headers that are expected as part of the request.
328− | ParameterHeader
319+ | ParamHeader
329 -- | Used together with Path Templating, where the parameter value is actually part of the operation's URL.
320 -- | Used together with Path Templating, where the parameter value is actually part of the operation's URL.
330 -- This does not include the host or base path of the API.
321 -- This does not include the host or base path of the API.
331 -- For example, in @/items/{itemId}@, the path parameter is @itemId@.
322 -- For example, in @/items/{itemId}@, the path parameter is @itemId@.
332− | ParameterPath
323+ | ParamPath
333 -- | Used to describe the payload of an HTTP request when either @application/x-www-form-urlencoded@
324 -- | Used to describe the payload of an HTTP request when either @application/x-www-form-urlencoded@
334 -- or @multipart/form-data@ are used as the content type of the request
325 -- or @multipart/form-data@ are used as the content type of the request
335 -- (in Swagger's definition, the @consumes@ property of an operation).
326 -- (in Swagger's definition, the @consumes@ property of an operation).
@@ -337,58 +328,36 @@ data ParameterLocation
337 -- Since form parameters are sent in the payload, they cannot be declared together with a body parameter for the same operation.
328 -- Since form parameters are sent in the payload, they cannot be declared together with a body parameter for the same operation.
338 -- Form parameters have a different format based on the content-type used
329 -- Form parameters have a different format based on the content-type used
339 -- (for further details, consult <http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4>).
330 -- (for further details, consult <http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4>).
340− | ParameterFormData
331+ | ParamFormData
341 deriving (Eq, Show)
332 deriving (Eq, Show)
342
333
343type Format = Text
334type Format = Text
344
335
345-- | Determines the format of the array.
336-- | Determines the format of the array.
346−data CollectionFormat
337+data CollectionFormat t where
338+ -- Comma separated values: @foo,bar@.
339+ CollectionCSV :: CollectionFormat t
347− = CollectionCSV -- ^ Comma separated values: @foo,bar@.
340+ -- Space separated values: @foo bar@.
341+ CollectionSSV :: CollectionFormat t
348− | CollectionSSV -- ^ Space separated values: @foo bar@.
342+ -- Tab separated values: @foo\\tbar@.
343+ CollectionTSV :: CollectionFormat t
349− | CollectionTSV -- ^ Tab separated values: @foo\\tbar@.
344+ -- Pipe separated values: @foo|bar@.
350− | CollectionPipes -- ^ Pipe separated values: @foo|bar@.
345+ CollectionPipes :: CollectionFormat t
351− | CollectionMulti -- ^ Corresponds to multiple parameter instances
346+ -- Corresponds to multiple parameter instances
352− -- instead of multiple values for a single instance @foo=bar&foo=baz@.
347+ -- instead of multiple values for a single instance @foo=bar&foo=baz@.
353− -- This is valid only for parameters in @'ParameterQuery'@ or @'ParameterFormData'@.
348+ -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
354− deriving (Eq, Show)
349+ CollectionMulti :: CollectionFormat Param
355−
350+
356−data ItemsType
351+deriving instance Eq (CollectionFormat t)
357− = ItemsString
358− | ItemsNumber
359− | ItemsInteger
360− | ItemsBoolean
361− | ItemsArray
362− deriving (Eq, Show)
363−
364−data SchemaType
365− = SchemaArray
366− | SchemaBoolean
367− | SchemaInteger
368− | SchemaNumber
369− | SchemaNull
370− | SchemaObject
371− | SchemaString
372− deriving (Eq, Show)
352+deriving instance Show (CollectionFormat t)
373−
374−-- | Determines the format of the nested array.
375−data ItemsCollectionFormat
376− = ItemsCollectionCSV -- ^ Comma separated values: @foo,bar@.
377− | ItemsCollectionSSV -- ^ Space separated values: @foo bar@.
378− | ItemsCollectionTSV -- ^ Tab separated values: @foo\\tbar@.
379− | ItemsCollectionPipes -- ^ Pipe separated values: @foo|bar@.
380− deriving (Eq, Show)
381
353
382type ParamName = Text
354type ParamName = Text
383
355
384data Schema = Schema
356data Schema = Schema
385− { _schemaType :: SchemaType
386− , _schemaFormat :: Maybe Format
387− , _schemaTitle :: Maybe Text
357+ { _schemaTitle :: Maybe Text
388 , _schemaDescription :: Maybe Text
358 , _schemaDescription :: Maybe Text
389 , _schemaRequired :: [ParamName]
359 , _schemaRequired :: [ParamName]
390
360
391− , _schemaItems :: Maybe SchemaItems
392 , _schemaAllOf :: Maybe [Schema]
361 , _schemaAllOf :: Maybe [Schema]
393 , _schemaProperties :: HashMap Text (Referenced Schema)
362 , _schemaProperties :: HashMap Text (Referenced Schema)
394 , _schemaAdditionalProperties :: Maybe Schema
363 , _schemaAdditionalProperties :: Maybe Schema
@@ -402,7 +371,7 @@ data Schema = Schema
402 , _schemaMaxProperties :: Maybe Integer
371 , _schemaMaxProperties :: Maybe Integer
403 , _schemaMinProperties :: Maybe Integer
372 , _schemaMinProperties :: Maybe Integer
404
373
405− , _schemaSchemaCommon :: SchemaCommon
374+ , _schemaParamSchema :: ParamSchema Schema SchemaItems
406 } deriving (Eq, Show, Generic)
375 } deriving (Eq, Show, Generic)
407
376
408data SchemaItems
377data SchemaItems
@@ -410,26 +379,29 @@ data SchemaItems
410 | SchemaItemsArray [Referenced Schema]
379 | SchemaItemsArray [Referenced Schema]
411 deriving (Eq, Show)
380 deriving (Eq, Show)
412
381
413−data SchemaCommon = SchemaCommon
382+data ParamSchema t items = ParamSchema
414 { -- | Declares the value of the parameter that the server will use if none is provided,
383 { -- | Declares the value of the parameter that the server will use if none is provided,
415 -- for example a @"count"@ to control the number of results per page might default to @100@
384 -- for example a @"count"@ to control the number of results per page might default to @100@
416 -- if not supplied by the client in the request.
385 -- if not supplied by the client in the request.
417 -- (Note: "default" has no meaning for required parameters.)
386 -- (Note: "default" has no meaning for required parameters.)
418 -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
387 -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
419− _schemaCommonDefault :: Maybe Value
388+ _paramSchemaDefault :: Maybe Value
420−
389+
421− , _schemaCommonMaximum :: Maybe Scientific
390+ , _paramSchemaType :: SwaggerType t
422− , _schemaCommonExclusiveMaximum :: Maybe Bool
391+ , _paramSchemaFormat :: Maybe Format
392+ , _paramSchemaItems :: Maybe items
393+ , _paramSchemaMaximum :: Maybe Scientific
394+ , _paramSchemaExclusiveMaximum :: Maybe Bool
423− , _schemaCommonMinimum :: Maybe Scientific
395+ , _paramSchemaMinimum :: Maybe Scientific
424− , _schemaCommonExclusiveMinimum :: Maybe Bool
396+ , _paramSchemaExclusiveMinimum :: Maybe Bool
425− , _schemaCommonMaxLength :: Maybe Integer
397+ , _paramSchemaMaxLength :: Maybe Integer
426− , _schemaCommonMinLength :: Maybe Integer
398+ , _paramSchemaMinLength :: Maybe Integer
427− , _schemaCommonPattern :: Maybe Text
399+ , _paramSchemaPattern :: Maybe Text
428− , _schemaCommonMaxItems :: Maybe Integer
400+ , _paramSchemaMaxItems :: Maybe Integer
429− , _schemaCommonMinItems :: Maybe Integer
401+ , _paramSchemaMinItems :: Maybe Integer
430− , _schemaCommonUniqueItems :: Maybe Bool
402+ , _paramSchemaUniqueItems :: Maybe Bool
431− , _schemaCommonEnum :: Maybe [Value]
403+ , _paramSchemaEnum :: Maybe [Value]
432− , _schemaCommonMultipleOf :: Maybe Scientific
404+ , _paramSchemaMultipleOf :: Maybe Scientific
433 } deriving (Eq, Show, Generic)
405 } deriving (Eq, Show, Generic)
434
406
435data Xml = Xml
407data Xml = Xml
@@ -461,21 +433,11 @@ data Xml = Xml
461 } deriving (Eq, Show, Generic)
433 } deriving (Eq, Show, Generic)
462
434
463data Items = Items
435data Items = Items
464− { -- | The internal type of the array.
465− _itemsType :: ItemsType
466−
467− -- | The extending format for the previously mentioned type.
468− , _itemsFormat :: Maybe Format
469−
470− -- | __Required if type is @'ItemsArray'@.__
471− -- Describes the type of items in the array.
472− , _itemsItems :: Maybe Items
473−
474− -- | Determines the format of the array if type array is used.
436+ { -- | Determines the format of the array if type array is used.
475 -- Default value is @'ItemsCollectionCSV'@.
437 -- Default value is @'ItemsCollectionCSV'@.
476− , _itemsCollectionFormat :: Maybe ItemsCollectionFormat
438+ _itemsCollectionFormat :: Maybe (CollectionFormat Items)
477
439
478− , _itemsCommon :: SchemaCommon
440+ , _itemsParamSchema :: ParamSchema Items Items
479 } deriving (Eq, Show, Generic)
441 } deriving (Eq, Show, Generic)
480
442
481-- | A container for the expected responses of an operation.
443-- | A container for the expected responses of an operation.
@@ -521,21 +483,11 @@ data Header = Header
521 { -- | A short description of the header.
483 { -- | A short description of the header.
522 _headerDescription :: Maybe Text
484 _headerDescription :: Maybe Text
523
485
524− -- | The type of the object.
525− , _headerType :: ItemsType
526−
527− -- | The extending format for the previously mentioned type. See Data Type Formats for further details.
528− , _headerFormat :: Maybe Format
529−
530− -- | __Required if type is @'ItemsArray'@__.
531− -- Describes the type of items in the array.
532− , _headerItems :: Maybe Items
533−
534 -- | Determines the format of the array if type array is used.
486 -- | Determines the format of the array if type array is used.
535 -- Default value is @'ItemsCollectionCSV'@.
487 -- Default value is @'ItemsCollectionCSV'@.
536− , _headerCollectionFormat :: Maybe ItemsCollectionFormat
488+ , _headerCollectionFormat :: Maybe (CollectionFormat Items)
537
489
538− , _headerCommon :: SchemaCommon
490+ , _headerParamSchema :: ParamSchema Header Items
539 } deriving (Eq, Show, Generic)
491 } deriving (Eq, Show, Generic)
540
492
541data Example = Example { getExample :: Map MediaType Value }
493data Example = Example { getExample :: Map MediaType Value }
@@ -657,15 +609,15 @@ instance Monoid Schema where
657 mempty = genericMempty
609 mempty = genericMempty
658 mappend = genericMappend
610 mappend = genericMappend
659
611
660−instance Monoid SchemaCommon where
612+instance Monoid (ParamSchema t i) where
661 mempty = genericMempty
613 mempty = genericMempty
662 mappend = genericMappend
614 mappend = genericMappend
663
615
664−instance Monoid Parameter where
616+instance Monoid Param where
665 mempty = genericMempty
617 mempty = genericMempty
666 mappend = genericMappend
618 mappend = genericMappend
667
619
668−instance Monoid ParameterOtherSchema where
620+instance Monoid ParamOtherSchema where
669 mempty = genericMempty
621 mempty = genericMempty
670 mappend = genericMappend
622 mappend = genericMappend
671
623
@@ -693,9 +645,9 @@ instance SwaggerMonoid Info
693instance SwaggerMonoid Paths
645instance SwaggerMonoid Paths
694instance SwaggerMonoid PathItem
646instance SwaggerMonoid PathItem
695instance SwaggerMonoid Schema
647instance SwaggerMonoid Schema
696−instance SwaggerMonoid SchemaCommon
648+instance SwaggerMonoid (ParamSchema t i)
697−instance SwaggerMonoid Parameter
649+instance SwaggerMonoid Param
698−instance SwaggerMonoid ParameterOtherSchema
650+instance SwaggerMonoid ParamOtherSchema
699instance SwaggerMonoid Responses
651instance SwaggerMonoid Responses
700instance SwaggerMonoid Response
652instance SwaggerMonoid Response
701instance SwaggerMonoid ExternalDocs
653instance SwaggerMonoid ExternalDocs
@@ -704,16 +656,12 @@ instance SwaggerMonoid Operation
704instance SwaggerMonoid MimeList
656instance SwaggerMonoid MimeList
705deriving instance SwaggerMonoid URL
657deriving instance SwaggerMonoid URL
706
658
707−instance SwaggerMonoid SchemaType where
659+instance SwaggerMonoid (SwaggerType t) where
708− swaggerMempty = SchemaNull
709− swaggerMappend _ y = y
710−
711−instance SwaggerMonoid ParameterType where
712− swaggerMempty = ParamString
660+ swaggerMempty = SwaggerString
713 swaggerMappend _ y = y
661 swaggerMappend _ y = y
714
662
715−instance SwaggerMonoid ParameterLocation where
663+instance SwaggerMonoid ParamLocation where
716− swaggerMempty = ParameterQuery
664+ swaggerMempty = ParamQuery
717 swaggerMappend _ y = y
665 swaggerMappend _ y = y
718
666
719instance SwaggerMonoid (HashMap Text Schema) where
667instance SwaggerMonoid (HashMap Text Schema) where
@@ -729,7 +677,7 @@ instance Monoid a => SwaggerMonoid (Referenced a) where
729 swaggerMappend (Inline x) (Inline y) = Inline (x <> y)
677 swaggerMappend (Inline x) (Inline y) = Inline (x <> y)
730 swaggerMappend _ y = y
678 swaggerMappend _ y = y
731
679
732−instance SwaggerMonoid (HashMap Text Parameter) where
680+instance SwaggerMonoid (HashMap Text Param) where
733 swaggerMempty = HashMap.empty
681 swaggerMempty = HashMap.empty
734 swaggerMappend = HashMap.unionWith mappend
682 swaggerMappend = HashMap.unionWith mappend
735
683
@@ -753,28 +701,22 @@ instance SwaggerMonoid (HashMap HttpStatusCode (Referenced Response)) where
753 swaggerMempty = HashMap.empty
701 swaggerMempty = HashMap.empty
754 swaggerMappend = flip HashMap.union
702 swaggerMappend = flip HashMap.union
755
703
756−instance SwaggerMonoid ParameterSchema where
704+instance SwaggerMonoid ParamAnySchema where
757− swaggerMempty = ParameterOther swaggerMempty
705+ swaggerMempty = ParamOther swaggerMempty
758− swaggerMappend (ParameterBody x) (ParameterBody y) = ParameterBody (swaggerMappend x y)
706+ swaggerMappend (ParamBody x) (ParamBody y) = ParamBody (swaggerMappend x y)
759− swaggerMappend (ParameterOther x) (ParameterOther y) = ParameterOther (swaggerMappend x y)
707+ swaggerMappend (ParamOther x) (ParamOther y) = ParamOther (swaggerMappend x y)
760 swaggerMappend _ y = y
708 swaggerMappend _ y = y
761
709
762-- =======================================================================
710-- =======================================================================
763-- TH derived ToJSON and FromJSON instances
711-- TH derived ToJSON and FromJSON instances
764-- =======================================================================
712-- =======================================================================
765
713
766−deriveJSON (jsonPrefix "Parameter") ''ParameterLocation
714+deriveJSON (jsonPrefix "Param") ''ParamLocation
767−deriveJSON (jsonPrefix "Param") ''ParameterType
768deriveJSON' ''Info
715deriveJSON' ''Info
769deriveJSON' ''Contact
716deriveJSON' ''Contact
770deriveJSON' ''License
717deriveJSON' ''License
771−deriveJSON (jsonPrefix "Schema") ''SchemaType
772−deriveJSON (jsonPrefix "Items") ''ItemsType
773−deriveJSON (jsonPrefix "ItemsCollection") ''ItemsCollectionFormat
774−deriveJSON (jsonPrefix "Collection") ''CollectionFormat
775deriveJSON (jsonPrefix "ApiKey") ''ApiKeyLocation
718deriveJSON (jsonPrefix "ApiKey") ''ApiKeyLocation
776deriveJSON (jsonPrefix "apiKey") ''ApiKeyParams
719deriveJSON (jsonPrefix "apiKey") ''ApiKeyParams
777−deriveJSON' ''SchemaCommon
778deriveJSONDefault ''Scheme
720deriveJSONDefault ''Scheme
779deriveJSON' ''Tag
721deriveJSON' ''Tag
780deriveJSON' ''ExternalDocs
722deriveJSON' ''ExternalDocs
@@ -826,13 +768,13 @@ instance ToJSON SecurityScheme where
826 toJSON = genericToJSONWithSub "type" (jsonPrefix "securityScheme")
768 toJSON = genericToJSONWithSub "type" (jsonPrefix "securityScheme")
827
769
828instance ToJSON Schema where
770instance ToJSON Schema where
829− toJSON = genericToJSONWithSub "schemaCommon" (jsonPrefix "schema")
771+ toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "schema")
830
772
831instance ToJSON Header where
773instance ToJSON Header where
832− toJSON = genericToJSONWithSub "common" (jsonPrefix "header")
774+ toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "header")
833
775
834instance ToJSON Items where
776instance ToJSON Items where
835− toJSON = genericToJSONWithSub "common" (jsonPrefix "items")
777+ toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "items")
836
778
837instance ToJSON Host where
779instance ToJSON Host where
838 toJSON (Host host mport) = toJSON $
780 toJSON (Host host mport) = toJSON $
@@ -846,15 +788,15 @@ instance ToJSON Paths where
846instance ToJSON MimeList where
788instance ToJSON MimeList where
847 toJSON (MimeList xs) = toJSON (map show xs)
789 toJSON (MimeList xs) = toJSON (map show xs)
848
790
849−instance ToJSON Parameter where
791+instance ToJSON Param where
850− toJSON = genericToJSONWithSub "schema" (jsonPrefix "parameter")
792+ toJSON = genericToJSONWithSub "schema" (jsonPrefix "param")
851
793
852−instance ToJSON ParameterSchema where
794+instance ToJSON ParamAnySchema where
853− toJSON (ParameterBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
795+ toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
854− toJSON (ParameterOther s) = toJSON s
796+ toJSON (ParamOther s) = toJSON s
855
797
856−instance ToJSON ParameterOtherSchema where
798+instance ToJSON ParamOtherSchema where
857− toJSON = genericToJSONWithSub "common" (jsonPrefix "parameterOtherSchema")
799+ toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
858
800
859instance ToJSON SchemaItems where
801instance ToJSON SchemaItems where
860 toJSON (SchemaItemsObject x) = toJSON x
802 toJSON (SchemaItemsObject x) = toJSON x
@@ -873,6 +815,26 @@ instance ToJSON a => ToJSON (Referenced a) where
873 toJSON (Ref ref) = toJSON ref
815 toJSON (Ref ref) = toJSON ref
874 toJSON (Inline x) = toJSON x
816 toJSON (Inline x) = toJSON x
875
817
818+instance ToJSON (SwaggerType t) where
819+ toJSON SwaggerArray = "array"
820+ toJSON SwaggerString = "string"
821+ toJSON SwaggerInteger = "integer"
822+ toJSON SwaggerNumber = "number"
823+ toJSON SwaggerBoolean = "boolean"
824+ toJSON SwaggerFile = "file"
825+ toJSON SwaggerNull = "null"
826+ toJSON SwaggerObject = "object"
827+
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+
835+instance ToJSON i => ToJSON (ParamSchema t i) where
836+ toJSON = genericToJSON (jsonPrefix "paramSchema")
837+
876-- =======================================================================
838-- =======================================================================
877-- Manual FromJSON instances
839-- Manual FromJSON instances
878-- =======================================================================
840-- =======================================================================
@@ -913,7 +875,7 @@ instance FromJSON Swagger where
913 , "security" .= ([] :: [SecurityRequirement])
875 , "security" .= ([] :: [SecurityRequirement])
914 , "tags" .= ([] :: [Tag])
876 , "tags" .= ([] :: [Tag])
915 , "definitions" .= (mempty :: HashMap Text Schema)
877 , "definitions" .= (mempty :: HashMap Text Schema)
916− , "parameters" .= (mempty :: HashMap Text Parameter)
878+ , "parameters" .= (mempty :: HashMap Text Param)
917 , "responses" .= (mempty :: HashMap Text Response)
879 , "responses" .= (mempty :: HashMap Text Response)
918 , "securityDefinitions" .= (mempty :: HashMap Text SecurityScheme)
880 , "securityDefinitions" .= (mempty :: HashMap Text SecurityScheme)
919 ] ) js
881 ] ) js
@@ -923,15 +885,15 @@ instance FromJSON SecurityScheme where
923 parseJSON = genericParseJSONWithSub "type" (jsonPrefix "securityScheme")
885 parseJSON = genericParseJSONWithSub "type" (jsonPrefix "securityScheme")
924
886
925instance FromJSON Schema where
887instance FromJSON Schema where
926− parseJSON = genericParseJSONWithSub "schemaCommon" (jsonPrefix "schema")
888+ parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "schema")
927 `withDefaults` [ "properties" .= (mempty :: HashMap Text Schema)
889 `withDefaults` [ "properties" .= (mempty :: HashMap Text Schema)
928 , "required" .= ([] :: [ParamName]) ]
890 , "required" .= ([] :: [ParamName]) ]
929
891
930instance FromJSON Header where
892instance FromJSON Header where
931− parseJSON = genericParseJSONWithSub "common" (jsonPrefix "header")
893+ parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "header")
932
894
933instance FromJSON Items where
895instance FromJSON Items where
934− parseJSON = genericParseJSONWithSub "common" (jsonPrefix "items")
896+ parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "items")
935
897
936instance FromJSON Host where
898instance FromJSON Host where
937 parseJSON (String s) =
899 parseJSON (String s) =
@@ -949,21 +911,21 @@ instance FromJSON Paths where
949instance FromJSON MimeList where
911instance FromJSON MimeList where
950 parseJSON js = (MimeList . map fromString) <$> parseJSON js
912 parseJSON js = (MimeList . map fromString) <$> parseJSON js
951
913
952−instance FromJSON Parameter where
914+instance FromJSON Param where
953− parseJSON = genericParseJSONWithSub "schema" (jsonPrefix "parameter")
915+ parseJSON = genericParseJSONWithSub "schema" (jsonPrefix "param")
954
916
955−instance FromJSON ParameterSchema where
917+instance FromJSON ParamAnySchema where
956 parseJSON js@(Object o) = do
918 parseJSON js@(Object o) = do
957 (i :: Text) <- o .: "in"
919 (i :: Text) <- o .: "in"
958 case i of
920 case i of
959 "body" -> do
921 "body" -> do
960 schema <- o .: "schema"
922 schema <- o .: "schema"
961− ParameterBody <$> parseJSON schema
923+ ParamBody <$> parseJSON schema
962− _ -> ParameterOther <$> parseJSON js
924+ _ -> ParamOther <$> parseJSON js
963 parseJSON _ = empty
925 parseJSON _ = empty
964
926
965−instance FromJSON ParameterOtherSchema where
927+instance FromJSON ParamOtherSchema where
966− parseJSON = genericParseJSONWithSub "common" (jsonPrefix "parameterOtherSchema")
928+ parseJSON = genericParseJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
967
929
968instance FromJSON SchemaItems where
930instance FromJSON SchemaItems where
969 parseJSON js@(Object _) = SchemaItemsObject <$> parseJSON js
931 parseJSON js@(Object _) = SchemaItemsObject <$> parseJSON js
@@ -991,7 +953,7 @@ instance FromJSON Operation where
991
953
992instance FromJSON PathItem where
954instance FromJSON PathItem where
993 parseJSON = genericParseJSON (jsonPrefix "pathItem")
955 parseJSON = genericParseJSON (jsonPrefix "pathItem")
994− `withDefaults` [ "parameters" .= ([] :: [Parameter]) ]
956+ `withDefaults` [ "parameters" .= ([] :: [Param]) ]
995
957
996instance FromJSON Reference where
958instance FromJSON Reference where
997 parseJSON (Object o) = Reference <$> o .: "$ref"
959 parseJSON (Object o) = Reference <$> o .: "$ref"
@@ -1005,3 +967,23 @@ instance FromJSON a => FromJSON (Referenced a) where
1005instance FromJSON Xml where
967instance FromJSON Xml where
1006 parseJSON = genericParseJSON (jsonPrefix "xml")
968 parseJSON = genericParseJSON (jsonPrefix "xml")
1007
969
970+instance FromJSON (SwaggerType Schema) where
971+ parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
972+
973+instance FromJSON (SwaggerType ParamOtherSchema) where
974+ parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerFile]
975+
976+instance {-# OVERLAPPABLE #-} FromJSON (SwaggerType t) where
977+ parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
978+
979+instance FromJSON (CollectionFormat Param) where
980+ parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
981+
982+instance FromJSON (CollectionFormat Items) where
983+ parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
984+
985+-- NOTE: The constraint @FromJSON (SwaggerType t)@ is necessary here!
986+-- Without the constraint the general instance will be used
987+-- that only accepts common types (i.e. NOT object, null or file).
988+instance (FromJSON (SwaggerType t), FromJSON i) => FromJSON (ParamSchema t i) where
989+ parseJSON = genericParseJSON (jsonPrefix "ParamSchema")
src/Data/Swagger/Internal/ParamSchema.hsnew file+204
@@ -0,0 +1,204 @@
1+{-# LANGUAGE DefaultSignatures #-}
2+{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE KindSignatures #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE PolyKinds #-}
7+{-# LANGUAGE ScopedTypeVariables #-}
8+{-# LANGUAGE TypeOperators #-}
9+{-# LANGUAGE TypeSynonymInstances #-}
10+module Data.Swagger.Internal.ParamSchema where
11+
12+import Control.Lens
13+import Data.Aeson
14+import Data.Proxy
15+import GHC.Generics
16+
17+import Data.Int
18+import Data.Monoid
19+import Data.Scientific
20+import qualified Data.Text as T
21+import qualified Data.Text.Lazy as TL
22+import Data.Time
23+import Data.Word
24+
25+import Data.Swagger.Internal
26+import Data.Swagger.Lens
27+import Data.Swagger.SchemaOptions
28+
29+-- | Convert a type into a plain @'ParamSchema'@.
30+--
31+-- An example type and instance:
32+--
33+-- @
34+-- {-\# LANGUAGE OverloadedStrings \#-} -- allows to write 'T.Text' literals
35+--
36+-- import Control.Lens
37+--
38+-- data Direction = Up | Down
39+--
40+-- instance ToParamSchema Direction where
41+-- toParamSchema = mempty
42+-- & schemaType .~ SwaggerString
43+-- & schemaEnum .~ [ \"Up\", \"Down\" ]
44+-- @
45+--
46+-- Instead of manually writing your @'ToParamSchema'@ instance you can
47+-- use a default generic implementation of @'toParamSchema'@.
48+--
49+-- To do that, simply add @deriving 'Generic'@ clause to your datatype
50+-- and declare a @'ToParamSchema'@ instance for your datatype without
51+-- giving definition for @'toParamSchema'@.
52+--
53+-- For instance, the previous example can be simplified into this:
54+--
55+-- @
56+-- {-\# LANGUAGE DeriveGeneric \#-}
57+--
58+-- import GHC.Generics (Generic)
59+--
60+-- data Direction = Up | Down deriving Generic
61+--
62+-- instance ToParamSchema Direction
63+-- @
64+class ToParamSchema a where
65+ -- | Convert a type into a plain parameter schema.
66+ toParamSchema :: proxy a -> ParamSchema t i
67+ default toParamSchema :: (Generic a, GToParamSchema (Rep a)) => proxy a -> ParamSchema t i
68+ toParamSchema = genericToParamSchema defaultSchemaOptions
69+
70+instance {-# OVERLAPPING #-} ToParamSchema String where
71+ toParamSchema _ = mempty & schemaType .~ SwaggerString
72+
73+instance ToParamSchema Bool where
74+ toParamSchema _ = mempty & schemaType .~ SwaggerBoolean
75+
76+instance ToParamSchema Integer where
77+ toParamSchema _ = mempty & schemaType .~ SwaggerInteger
78+
79+instance ToParamSchema Int where toParamSchema = toParamSchemaBoundedIntegral
80+instance ToParamSchema Int8 where toParamSchema = toParamSchemaBoundedIntegral
81+instance ToParamSchema Int16 where toParamSchema = toParamSchemaBoundedIntegral
82+instance ToParamSchema Int32 where toParamSchema = toParamSchemaBoundedIntegral
83+instance ToParamSchema Int64 where toParamSchema = toParamSchemaBoundedIntegral
84+
85+instance ToParamSchema Word where toParamSchema = toParamSchemaBoundedIntegral
86+instance ToParamSchema Word8 where toParamSchema = toParamSchemaBoundedIntegral
87+instance ToParamSchema Word16 where toParamSchema = toParamSchemaBoundedIntegral
88+instance ToParamSchema Word32 where toParamSchema = toParamSchemaBoundedIntegral
89+instance ToParamSchema Word64 where toParamSchema = toParamSchemaBoundedIntegral
90+
91+-- | Default plain schema for @'Bounded'@, @'Integral'@ types.
92+toParamSchemaBoundedIntegral :: forall proxy a t i. (Bounded a, Integral a) => proxy a -> ParamSchema t i
93+toParamSchemaBoundedIntegral _ = mempty
94+ & schemaType .~ SwaggerInteger
95+ & schemaMinimum ?~ fromInteger (toInteger (minBound :: a))
96+ & schemaMaximum ?~ fromInteger (toInteger (maxBound :: a))
97+
98+instance ToParamSchema Char where
99+ toParamSchema _ = mempty
100+ & schemaType .~ SwaggerString
101+ & schemaMaxLength ?~ 1
102+ & schemaMinLength ?~ 1
103+
104+instance ToParamSchema Scientific where
105+ toParamSchema _ = mempty & schemaType .~ SwaggerNumber
106+
107+instance ToParamSchema Double where
108+ toParamSchema _ = mempty & schemaType .~ SwaggerNumber
109+
110+instance ToParamSchema Float where
111+ toParamSchema _ = mempty & schemaType .~ SwaggerNumber
112+
113+timeParamSchema :: String -> ParamSchema t i
114+timeParamSchema format = mempty
115+ & schemaType .~ SwaggerString
116+ & schemaFormat ?~ T.pack format
117+ & schemaMinLength ?~ toInteger (length format)
118+
119+-- |
120+-- >>> toParamSchema (Proxy :: Proxy Day) ^. schemaFormat
121+-- Just "yyyy-mm-dd"
122+instance ToParamSchema Day where
123+ toParamSchema _ = timeParamSchema "yyyy-mm-dd"
124+
125+-- |
126+-- >>> toParamSchema (Proxy :: Proxy LocalTime) ^. schemaFormat
127+-- Just "yyyy-mm-ddThh:MM:ss"
128+instance ToParamSchema LocalTime where
129+ toParamSchema _ = timeParamSchema "yyyy-mm-ddThh:MM:ss"
130+
131+-- |
132+-- >>> toParamSchema (Proxy :: Proxy ZonedTime) ^. schemaFormat
133+-- Just "yyyy-mm-ddThh:MM:ss+hhMM"
134+instance ToParamSchema ZonedTime where
135+ toParamSchema _ = timeParamSchema "yyyy-mm-ddThh:MM:ss+hhMM"
136+
137+-- |
138+-- >>> toParamSchema (Proxy :: Proxy UTCTime) ^. schemaFormat
139+-- Just "yyyy-mm-ddThh:MM:ssZ"
140+instance ToParamSchema UTCTime where
141+ toParamSchema _ = timeParamSchema "yyyy-mm-ddThh:MM:ssZ"
142+
143+instance ToParamSchema NominalDiffTime where
144+ toParamSchema _ = toParamSchema (Proxy :: Proxy Integer)
145+
146+instance ToParamSchema T.Text where
147+ toParamSchema _ = toParamSchema (Proxy :: Proxy String)
148+
149+instance ToParamSchema TL.Text where
150+ toParamSchema _ = toParamSchema (Proxy :: Proxy String)
151+
152+instance ToParamSchema All where toParamSchema _ = toParamSchema (Proxy :: Proxy Bool)
153+instance ToParamSchema Any where toParamSchema _ = toParamSchema (Proxy :: Proxy Bool)
154+instance ToParamSchema a => ToParamSchema (Sum a) where toParamSchema _ = toParamSchema (Proxy :: Proxy a)
155+instance ToParamSchema a => ToParamSchema (Product a) where toParamSchema _ = toParamSchema (Proxy :: Proxy a)
156+instance ToParamSchema a => ToParamSchema (First a) where toParamSchema _ = toParamSchema (Proxy :: Proxy a)
157+instance ToParamSchema a => ToParamSchema (Last a) where toParamSchema _ = toParamSchema (Proxy :: Proxy a)
158+instance ToParamSchema a => ToParamSchema (Dual a) where toParamSchema _ = toParamSchema (Proxy :: Proxy a)
159+
160+-- |
161+-- >>> encode (toParamSchema (Proxy :: Proxy ()) :: ParamSchema t Items)
162+-- "{\"type\":\"string\",\"enum\":[\"_\"]}"
163+instance ToParamSchema () where
164+ toParamSchema _ = mempty
165+ & schemaType .~ SwaggerString
166+ & schemaEnum ?~ ["_"]
167+
168+-- | A configurable generic @'ParamSchema'@ creator.
169+genericToParamSchema :: forall proxy a t i. (Generic a, GToParamSchema (Rep a)) => SchemaOptions -> proxy a -> ParamSchema t i
170+genericToParamSchema opts _ = gtoParamSchema opts (Proxy :: Proxy (Rep a)) mempty
171+
172+class GToParamSchema (f :: * -> *) where
173+ gtoParamSchema :: SchemaOptions -> proxy f -> ParamSchema t i -> ParamSchema t i
174+
175+instance GToParamSchema f => GToParamSchema (D1 d f) where
176+ gtoParamSchema opts _ = gtoParamSchema opts (Proxy :: Proxy f)
177+
178+instance GToParamSchema f => GToParamSchema (C1 c (S1 s f)) where
179+ gtoParamSchema opts _ = gtoParamSchema opts (Proxy :: Proxy f)
180+
181+instance ToParamSchema c => GToParamSchema (K1 i c) where
182+ gtoParamSchema _ _ _ = toParamSchema (Proxy :: Proxy c)
183+
184+instance (GEnumParamSchema f, GEnumParamSchema g) => GToParamSchema (f :+: g) where
185+ gtoParamSchema opts _ = genumParamSchema opts (Proxy :: Proxy (f :+: g))
186+
187+class GEnumParamSchema (f :: * -> *) where
188+ genumParamSchema :: SchemaOptions -> proxy f -> ParamSchema t i -> ParamSchema t i
189+
190+instance (GEnumParamSchema f, GEnumParamSchema g) => GEnumParamSchema (f :+: g) where
191+ genumParamSchema opts _ = genumParamSchema opts (Proxy :: Proxy f) . genumParamSchema opts (Proxy :: Proxy g)
192+
193+instance Constructor c => GEnumParamSchema (C1 c U1) where
194+ genumParamSchema opts _ s = s
195+ & schemaType .~ SwaggerString
196+ & schemaEnum %~ addEnumValue tag
197+ where
198+ tag = toJSON (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
199+
200+ addEnumValue x Nothing = Just [x]
201+ addEnumValue x (Just xs) = Just (x:xs)
202+
203+data Proxy3 a b c = Proxy3
204+
src/Data/Swagger/Schema/Internal.hs → src/Data/Swagger/Internal/Schema.hsrenamed+47−107
@@ -10,7 +10,7 @@
10{-# LANGUAGE TypeFamilies #-}
10{-# LANGUAGE TypeFamilies #-}
11{-# LANGUAGE TypeOperators #-}
11{-# LANGUAGE TypeOperators #-}
12{-# LANGUAGE TypeSynonymInstances #-}
12{-# LANGUAGE TypeSynonymInstances #-}
13−module Data.Swagger.Schema.Internal where
13+module Data.Swagger.Internal.Schema where
14
14
15import Control.Lens
15import Control.Lens
16import Data.Aeson
16import Data.Aeson
@@ -32,7 +32,9 @@ import Data.Word
32import GHC.Generics
32import GHC.Generics
33
33
34import Data.Swagger.Internal
34import Data.Swagger.Internal
35+import Data.Swagger.Internal.ParamSchema (ToParamSchema(..))
35import Data.Swagger.Lens
36import Data.Swagger.Lens
37+import Data.Swagger.SchemaOptions
36
38
37-- | A @'Schema'@ with an optional name.
39-- | A @'Schema'@ with an optional name.
38-- This name can be used in references.
40-- This name can be used in references.
@@ -58,7 +60,7 @@ named name schema = (Just name, schema)
58--
60--
59-- instance ToSchema Coord where
61-- instance ToSchema Coord where
60-- toNamedSchema = (Just \"Coord\", mempty
62-- toNamedSchema = (Just \"Coord\", mempty
61−-- & schemaType .~ SchemaObject
63+-- & schemaType .~ SwaggerObject
62-- & schemaProperties .~
64-- & schemaProperties .~
63-- [ ("x", toSchemaRef (Proxy :: Proxy Double))
65-- [ ("x", toSchemaRef (Proxy :: Proxy Double))
64-- , ("y", toSchemaRef (Proxy :: Proxy Double))
66-- , ("y", toSchemaRef (Proxy :: Proxy Double))
@@ -113,44 +115,27 @@ gtoSchema opts proxy = snd . gtoNamedSchema opts proxy
113
115
114instance {-# OVERLAPPABLE #-} ToSchema a => ToSchema [a] where
116instance {-# OVERLAPPABLE #-} ToSchema a => ToSchema [a] where
115 toNamedSchema _ = unnamed $ mempty
117 toNamedSchema _ = unnamed $ mempty
116− & schemaType .~ SchemaArray
118+ & schemaType .~ SwaggerArray
117 & schemaItems ?~ SchemaItemsObject (toSchemaRef (Proxy :: Proxy a))
119 & schemaItems ?~ SchemaItemsObject (toSchemaRef (Proxy :: Proxy a))
118
120
119−instance {-# OVERLAPPING #-} ToSchema String where
121+instance {-# OVERLAPPING #-} ToSchema String where toNamedSchema = unnamed . paramSchemaToSchema
120− toNamedSchema _ = unnamed $ mempty & schemaType .~ SchemaString
121−
122−instance ToSchema Bool where
122+instance ToSchema Bool where toNamedSchema = unnamed . paramSchemaToSchema
123− toNamedSchema _ = unnamed $ mempty & schemaType .~ SchemaBoolean
124−
125−instance ToSchema Integer where
123+instance ToSchema Integer where toNamedSchema = unnamed . paramSchemaToSchema
126− toNamedSchema _ = unnamed $ mempty & schemaType .~ SchemaInteger
127−
128−instance ToSchema Int where toNamedSchema = unnamed . toSchemaBoundedIntegral
124+instance ToSchema Int where toNamedSchema = unnamed . paramSchemaToSchema
129−instance ToSchema Int8 where toNamedSchema = unnamed . toSchemaBoundedIntegral
125+instance ToSchema Int8 where toNamedSchema = unnamed . paramSchemaToSchema
130−instance ToSchema Int16 where toNamedSchema = unnamed . toSchemaBoundedIntegral
126+instance ToSchema Int16 where toNamedSchema = unnamed . paramSchemaToSchema
131−instance ToSchema Int32 where toNamedSchema = unnamed . toSchemaBoundedIntegral
127+instance ToSchema Int32 where toNamedSchema = unnamed . paramSchemaToSchema
132−instance ToSchema Int64 where toNamedSchema = unnamed . toSchemaBoundedIntegral
128+instance ToSchema Int64 where toNamedSchema = unnamed . paramSchemaToSchema
133−
134−instance ToSchema Word where toNamedSchema = unnamed . toSchemaBoundedIntegral
129+instance ToSchema Word where toNamedSchema = unnamed . paramSchemaToSchema
135−instance ToSchema Word8 where toNamedSchema = unnamed . toSchemaBoundedIntegral
130+instance ToSchema Word8 where toNamedSchema = unnamed . paramSchemaToSchema
136−instance ToSchema Word16 where toNamedSchema = unnamed . toSchemaBoundedIntegral
131+instance ToSchema Word16 where toNamedSchema = unnamed . paramSchemaToSchema
137−instance ToSchema Word32 where toNamedSchema = unnamed . toSchemaBoundedIntegral
132+instance ToSchema Word32 where toNamedSchema = unnamed . paramSchemaToSchema
138−instance ToSchema Word64 where toNamedSchema = unnamed . toSchemaBoundedIntegral
133+instance ToSchema Word64 where toNamedSchema = unnamed . paramSchemaToSchema
139−
134+
140−instance ToSchema Char where
135+instance ToSchema Char where toNamedSchema = unnamed . paramSchemaToSchema
141− toNamedSchema _ = unnamed $ mempty
142− & schemaType .~ SchemaString
143− & schemaMaxLength ?~ 1
144− & schemaMinLength ?~ 1
145−
146−instance ToSchema Scientific where
136+instance ToSchema Scientific where toNamedSchema = unnamed . paramSchemaToSchema
147− toNamedSchema _ = unnamed $ mempty & schemaType .~ SchemaNumber
148−
149−instance ToSchema Double where
137+instance ToSchema Double where toNamedSchema = unnamed . paramSchemaToSchema
150− toNamedSchema _ = unnamed $ mempty & schemaType .~ SchemaNumber
151−
152−instance ToSchema Float where
138+instance ToSchema Float where toNamedSchema = unnamed . paramSchemaToSchema
153− toNamedSchema _ = unnamed $ mempty & schemaType .~ SchemaNumber
154
139
155instance ToSchema a => ToSchema (Maybe a) where
140instance ToSchema a => ToSchema (Maybe a) where
156 toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
141 toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
@@ -167,7 +152,7 @@ instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f
167
152
168timeNamedSchema :: String -> String -> NamedSchema
153timeNamedSchema :: String -> String -> NamedSchema
169timeNamedSchema name format = (Just name, mempty
154timeNamedSchema name format = (Just name, mempty
170− & schemaType .~ SchemaString
155+ & schemaType .~ SwaggerString
171 & schemaFormat ?~ T.pack format
156 & schemaFormat ?~ T.pack format
172 & schemaMinLength ?~ toInteger (length format))
157 & schemaMinLength ?~ toInteger (length format))
173
158
@@ -188,7 +173,7 @@ instance ToSchema LocalTime where
188-- Just "yyyy-mm-ddThh:MM:ss(Z|+hh:MM)"
173-- Just "yyyy-mm-ddThh:MM:ss(Z|+hh:MM)"
189instance ToSchema ZonedTime where
174instance ToSchema ZonedTime where
190 toNamedSchema _ = (Just "ZonedTime", mempty
175 toNamedSchema _ = (Just "ZonedTime", mempty
191− & schemaType .~ SchemaString
176+ & schemaType .~ SwaggerString
192 & schemaFormat ?~ "yyyy-mm-ddThh:MM:ss(Z|+hh:MM)"
177 & schemaFormat ?~ "yyyy-mm-ddThh:MM:ss(Z|+hh:MM)"
193 & schemaMinLength ?~ toInteger (length ("yyyy-mm-ddThh:MM:ssZ" :: String)))
178 & schemaMinLength ?~ toInteger (length ("yyyy-mm-ddThh:MM:ssZ" :: String)))
194
179
@@ -201,11 +186,8 @@ instance ToSchema NominalDiffTime where
201instance ToSchema UTCTime where
186instance ToSchema UTCTime where
202 toNamedSchema _ = timeNamedSchema "UTCTime" "yyyy-mm-ddThh:MM:ssZ"
187 toNamedSchema _ = timeNamedSchema "UTCTime" "yyyy-mm-ddThh:MM:ssZ"
203
188
204−instance ToSchema T.Text where
189+instance ToSchema T.Text where toNamedSchema = unnamed . paramSchemaToSchema
205− toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy String)
206−
207−instance ToSchema TL.Text where
190+instance ToSchema TL.Text where toNamedSchema = unnamed . paramSchemaToSchema
208− toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy String)
209
191
210instance ToSchema IntSet where toNamedSchema _ = toNamedSchema (Proxy :: Proxy (Set Int))
192instance ToSchema IntSet where toNamedSchema _ = toNamedSchema (Proxy :: Proxy (Set Int))
211
193
@@ -215,7 +197,7 @@ instance ToSchema a => ToSchema (IntMap a) where
215
197
216instance ToSchema a => ToSchema (Map String a) where
198instance ToSchema a => ToSchema (Map String a) where
217 toNamedSchema _ = unnamed $ mempty
199 toNamedSchema _ = unnamed $ mempty
218− & schemaType .~ SchemaObject
200+ & schemaType .~ SwaggerObject
219 & schemaAdditionalProperties ?~ toSchema (Proxy :: Proxy a)
201 & schemaAdditionalProperties ?~ toSchema (Proxy :: Proxy a)
220
202
221instance ToSchema a => ToSchema (Map T.Text a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy (Map String a))
203instance ToSchema a => ToSchema (Map T.Text a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy (Map String a))
@@ -231,71 +213,19 @@ instance ToSchema a => ToSchema (Set a) where
231
213
232instance ToSchema a => ToSchema (HashSet a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy (Set a))
214instance ToSchema a => ToSchema (HashSet a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy (Set a))
233
215
234−instance ToSchema All where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy Bool)
216+instance ToSchema All where toNamedSchema = unnamed . paramSchemaToSchema
235−instance ToSchema Any where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy Bool)
217+instance ToSchema Any where toNamedSchema = unnamed . paramSchemaToSchema
218+
236instance ToSchema a => ToSchema (Sum a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
219instance ToSchema a => ToSchema (Sum a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
237instance ToSchema a => ToSchema (Product a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
220instance ToSchema a => ToSchema (Product a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
238instance ToSchema a => ToSchema (First a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
221instance ToSchema a => ToSchema (First a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
239instance ToSchema a => ToSchema (Last a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
222instance ToSchema a => ToSchema (Last a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
240instance ToSchema a => ToSchema (Dual a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
223instance ToSchema a => ToSchema (Dual a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
241
224
242−-- | Options that specify how to encode your type to Swagger schema.
243−data SchemaOptions = SchemaOptions
244− { -- | Function applied to field labels. Handy for removing common record prefixes for example.
245− fieldLabelModifier :: String -> String
246− -- | Function applied to constructor tags which could be handy for lower-casing them for example.
247− , constructorTagModifier :: String -> String
248− -- | Function applied to datatype name.
249− , datatypeNameModifier :: String -> String
250− -- | If @'True'@ the constructors of a datatype, with all nullary constructors,
251− -- will be encoded to a string enumeration schema with the constructor tags as possible values.
252− , allNullaryToStringTag :: Bool
253− -- | If @'True'@ direct subschemas will be referenced if possible (rather than inlined).
254− -- Note that this option does not influence nested schemas, e.g. for these types
255− --
256− -- @
257− -- data Object = Object String deriving Generic
258− -- instance ToSchema Object
259− --
260− -- newtype Objects = Objects [Object] deriving Generic
261− -- instance ToSchema Objects where
262− -- toNamedSchema = genericToNamedSchema defaultSchemaOptions
263− -- { useReferences = False }
264− -- @
265− --
266− -- Schema for @Objects@ __will not__ inline @Object@ schema because
267− -- it is nested in a @[]@ schema.
268− , useReferences :: Bool
269− -- | Hide the field name when a record constructor has only one field, like a newtype.
270− , unwrapUnaryRecords :: Bool
271− }
272−
273−-- | Default encoding @'SchemaOptions'@.
274−--
275−-- @
276−-- 'SchemaOptions'
277−-- { 'fieldLabelModifier' = id
278−-- , 'constructorTagModifier' = id
279−-- , 'datatypeNameModifier' = id
280−-- , 'allNullaryToStringTag' = True
281−-- , 'useReferences' = True
282−-- , 'unwrapUnaryRecords' = False
283−-- }
284−-- @
285−defaultSchemaOptions :: SchemaOptions
286−defaultSchemaOptions = SchemaOptions
287− { fieldLabelModifier = id
288− , constructorTagModifier = id
289− , datatypeNameModifier = id
290− , allNullaryToStringTag = True
291− , useReferences = True
292− , unwrapUnaryRecords = False
293− }
294−
295-- | Default schema for @'Bounded'@, @'Integral'@ types.
225-- | Default schema for @'Bounded'@, @'Integral'@ types.
296toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
226toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
297toSchemaBoundedIntegral _ = mempty
227toSchemaBoundedIntegral _ = mempty
298− & schemaType .~ SchemaInteger
228+ & schemaType .~ SwaggerInteger
299 & schemaMinimum ?~ fromInteger (toInteger (minBound :: a))
229 & schemaMinimum ?~ fromInteger (toInteger (minBound :: a))
300 & schemaMaximum ?~ fromInteger (toInteger (maxBound :: a))
230 & schemaMaximum ?~ fromInteger (toInteger (maxBound :: a))
301
231
@@ -325,9 +255,19 @@ gdatatypeSchemaName opts _ = case name of
325 where
255 where
326 name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
256 name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
327
257
258+-- | Lift a plain @'ParamSchema'@ into a model @'NamedSchema'@.
259+paramSchemaToNamedSchema :: forall a d f proxy.
260+ (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
261+ => SchemaOptions -> proxy a -> NamedSchema
262+paramSchemaToNamedSchema opts proxy = (gdatatypeSchemaName opts (Proxy :: Proxy d), paramSchemaToSchema proxy)
263+
264+-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
265+paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
266+paramSchemaToSchema _ = mempty & schemaParamSchema .~ toParamSchema (Proxy :: Proxy a)
267+
328nullarySchema :: Schema
268nullarySchema :: Schema
329nullarySchema = mempty
269nullarySchema = mempty
330− & schemaType .~ SchemaArray
270+ & schemaType .~ SwaggerArray
331 & schemaEnum ?~ [ toJSON () ]
271 & schemaEnum ?~ [ toJSON () ]
332
272
333instance GToSchema U1 where
273instance GToSchema U1 where
@@ -370,10 +310,10 @@ appendItem _ _ = error "GToSchema.appendItem: cannot append to SchemaItemsObject
370withFieldSchema :: forall proxy s f. (Selector s, GToSchema f) => SchemaOptions -> proxy s f -> Bool -> Schema -> Schema
310withFieldSchema :: forall proxy s f. (Selector s, GToSchema f) => SchemaOptions -> proxy s f -> Bool -> Schema -> Schema
371withFieldSchema opts _ isRequiredField schema
311withFieldSchema opts _ isRequiredField schema
372 | T.null fieldName = schema
312 | T.null fieldName = schema
373− & schemaType .~ SchemaArray
313+ & schemaType .~ SwaggerArray
374 & schemaItems %~ appendItem fieldSchemaRef
314 & schemaItems %~ appendItem fieldSchemaRef
375 | otherwise = schema
315 | otherwise = schema
376− & schemaType .~ SchemaObject
316+ & schemaType .~ SwaggerObject
377 & schemaProperties . at fieldName ?~ fieldSchemaRef
317 & schemaProperties . at fieldName ?~ fieldSchemaRef
378 & if isRequiredField
318 & if isRequiredField
379 then schemaRequired %~ (fieldName :)
319 then schemaRequired %~ (fieldName :)
@@ -401,7 +341,7 @@ instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
401 (All allNullary, sumSchema) = gsumToSchema opts (Proxy :: Proxy (f :+: g)) s
341 (All allNullary, sumSchema) = gsumToSchema opts (Proxy :: Proxy (f :+: g)) s
402
342
403 toStringTag schema = mempty
343 toStringTag schema = mempty
404− & schemaType .~ SchemaString
344+ & schemaType .~ SwaggerString
405 & schemaEnum ?~ map toJSON (schema ^.. schemaProperties.ifolded.asIndex)
345 & schemaEnum ?~ map toJSON (schema ^.. schemaProperties.ifolded.asIndex)
406
346
407type AllNullary = All
347type AllNullary = All
@@ -420,7 +360,7 @@ instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
420gsumConToSchema :: forall c f proxy. Constructor c =>
360gsumConToSchema :: forall c f proxy. Constructor c =>
421 Bool -> Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> (AllNullary, Schema)
361 Bool -> Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> (AllNullary, Schema)
422gsumConToSchema isNullary tagSchemaRef opts _ schema = (All isNullary, schema
362gsumConToSchema isNullary tagSchemaRef opts _ schema = (All isNullary, schema
423− & schemaType .~ SchemaObject
363+ & schemaType .~ SwaggerObject
424 & schemaProperties . at tag ?~ tagSchemaRef
364 & schemaProperties . at tag ?~ tagSchemaRef
425 & schemaMaxProperties ?~ 1
365 & schemaMaxProperties ?~ 1
426 & schemaMinProperties ?~ 1)
366 & schemaMinProperties ?~ 1)
src/Data/Swagger/Internal/Utils.hs+8
@@ -52,6 +52,14 @@ deriveJSONDefault = deriveJSON (jsonPrefix "")
52deriveJSON' :: Name -> Q [Dec]
52deriveJSON' :: Name -> Q [Dec]
53deriveJSON' name = deriveJSON (jsonPrefix (nameBase name)) name
53deriveJSON' name = deriveJSON (jsonPrefix (nameBase name)) name
54
54
55+parseOneOf :: ToJSON a => [a] -> Value -> Parser a
56+parseOneOf xs js =
57+ case lookup js ys of
58+ Nothing -> fail $ "invalid json: " ++ show js ++ " (expected one of " ++ show (map fst ys) ++ ")"
59+ Just x -> pure x
60+ where
61+ ys = zip (map toJSON xs) xs
62+
55genericToJSONWithSub :: (Generic a, GToJSON (Rep a)) => Text -> Options -> a -> Value
63genericToJSONWithSub :: (Generic a, GToJSON (Rep a)) => Text -> Options -> a -> Value
56genericToJSONWithSub sub opts x =
64genericToJSONWithSub sub opts x =
57 case genericToJSON opts x of
65 case genericToJSON opts x of
src/Data/Swagger/Lens.hs+47−38
@@ -23,14 +23,14 @@ makeLenses ''Paths
23makeLenses ''PathItem
23makeLenses ''PathItem
24makeLenses ''Tag
24makeLenses ''Tag
25makeLenses ''Operation
25makeLenses ''Operation
26−makeLenses ''Parameter
26+makeLenses ''Param
27−makePrisms ''ParameterSchema
27+makePrisms ''ParamAnySchema
28−makeLenses ''ParameterOtherSchema
28+makeLenses ''ParamOtherSchema
29makeLenses ''Items
29makeLenses ''Items
30makeLenses ''Header
30makeLenses ''Header
31makeLenses ''Schema
31makeLenses ''Schema
32makePrisms ''SchemaItems
32makePrisms ''SchemaItems
33−makeLenses ''SchemaCommon
33+makeLenses ''ParamSchema
34makeLenses ''Xml
34makeLenses ''Xml
35makeLenses ''Responses
35makeLenses ''Responses
36makeLenses ''Response
36makeLenses ''Response
@@ -51,57 +51,66 @@ instance HasDescription Response Text where description = responseDescript
51instance HasDescription Info (Maybe Text) where description = infoDescription
51instance HasDescription Info (Maybe Text) where description = infoDescription
52instance HasDescription Tag (Maybe Text) where description = tagDescription
52instance HasDescription Tag (Maybe Text) where description = tagDescription
53instance HasDescription Operation (Maybe Text) where description = operationDescription
53instance HasDescription Operation (Maybe Text) where description = operationDescription
54−instance HasDescription Parameter (Maybe Text) where description = parameterDescription
54+instance HasDescription Param (Maybe Text) where description = paramDescription
55instance HasDescription Header (Maybe Text) where description = headerDescription
55instance HasDescription Header (Maybe Text) where description = headerDescription
56instance HasDescription Schema (Maybe Text) where description = schemaDescription
56instance HasDescription Schema (Maybe Text) where description = schemaDescription
57instance HasDescription SecurityScheme (Maybe Text) where description = securitySchemeDescription
57instance HasDescription SecurityScheme (Maybe Text) where description = securitySchemeDescription
58instance HasDescription ExternalDocs (Maybe Text) where description = externalDocsDescription
58instance HasDescription ExternalDocs (Maybe Text) where description = externalDocsDescription
59
59
60−class HasSchemaCommon s where
60+class HasParamSchema s t i | s -> t i where
61− schemaCommon :: Lens' s SchemaCommon
61+ parameterSchema :: Lens' s (ParamSchema t i)
62
62
63−instance HasSchemaCommon Schema where schemaCommon = schemaSchemaCommon
63+instance HasParamSchema Schema Schema SchemaItems where parameterSchema = schemaParamSchema
64−instance HasSchemaCommon ParameterOtherSchema where schemaCommon = parameterOtherSchemaCommon
64+instance HasParamSchema ParamOtherSchema ParamOtherSchema Items where parameterSchema = paramOtherSchemaParamSchema
65−instance HasSchemaCommon Items where schemaCommon = itemsCommon
65+instance HasParamSchema Items Items Items where parameterSchema = itemsParamSchema
66−instance HasSchemaCommon Header where schemaCommon = headerCommon
66+instance HasParamSchema Header Header Items where parameterSchema = headerParamSchema
67−instance HasSchemaCommon SchemaCommon where schemaCommon = id
67+instance HasParamSchema (ParamSchema t i) t i where parameterSchema = id
68
68
69−schemaDefault :: HasSchemaCommon s => Lens' s (Maybe Value)
69+schemaType :: HasParamSchema s t i => Lens' s (SwaggerType t)
70−schemaDefault = schemaCommon.schemaCommonDefault
70+schemaType = parameterSchema.paramSchemaType
71
71
72−schemaMaximum :: HasSchemaCommon s => Lens' s (Maybe Scientific)
72+schemaFormat :: HasParamSchema s t i => Lens' s (Maybe Format)
73−schemaMaximum = schemaCommon.schemaCommonMaximum
73+schemaFormat = parameterSchema.paramSchemaFormat
74
74
75−schemaExclusiveMaximum :: HasSchemaCommon s => Lens' s (Maybe Bool)
75+schemaItems :: HasParamSchema s t i => Lens' s (Maybe i)
76−schemaExclusiveMaximum = schemaCommon.schemaCommonExclusiveMaximum
76+schemaItems = parameterSchema.paramSchemaItems
77
77
78−schemaMinimum :: HasSchemaCommon s => Lens' s (Maybe Scientific)
78+schemaDefault :: HasParamSchema s t i => Lens' s (Maybe Value)
79−schemaMinimum = schemaCommon.schemaCommonMinimum
79+schemaDefault = parameterSchema.paramSchemaDefault
80
80
81−schemaExclusiveMinimum :: HasSchemaCommon s => Lens' s (Maybe Bool)
81+schemaMaximum :: HasParamSchema s t i => Lens' s (Maybe Scientific)
82−schemaExclusiveMinimum = schemaCommon.schemaCommonExclusiveMinimum
82+schemaMaximum = parameterSchema.paramSchemaMaximum
83
83
84−schemaMaxLength :: HasSchemaCommon s => Lens' s (Maybe Integer)
84+schemaExclusiveMaximum :: HasParamSchema s t i => Lens' s (Maybe Bool)
85−schemaMaxLength = schemaCommon.schemaCommonMaxLength
85+schemaExclusiveMaximum = parameterSchema.paramSchemaExclusiveMaximum
86
86
87−schemaMinLength :: HasSchemaCommon s => Lens' s (Maybe Integer)
87+schemaMinimum :: HasParamSchema s t i => Lens' s (Maybe Scientific)
88−schemaMinLength = schemaCommon.schemaCommonMinLength
88+schemaMinimum = parameterSchema.paramSchemaMinimum
89
89
90−schemaPattern :: HasSchemaCommon s => Lens' s (Maybe Text)
90+schemaExclusiveMinimum :: HasParamSchema s t i => Lens' s (Maybe Bool)
91−schemaPattern = schemaCommon.schemaCommonPattern
91+schemaExclusiveMinimum = parameterSchema.paramSchemaExclusiveMinimum
92
92
93−schemaMaxItems :: HasSchemaCommon s => Lens' s (Maybe Integer)
93+schemaMaxLength :: HasParamSchema s t i => Lens' s (Maybe Integer)
94−schemaMaxItems = schemaCommon.schemaCommonMaxItems
94+schemaMaxLength = parameterSchema.paramSchemaMaxLength
95
95
96−schemaMinItems :: HasSchemaCommon s => Lens' s (Maybe Integer)
96+schemaMinLength :: HasParamSchema s t i => Lens' s (Maybe Integer)
97−schemaMinItems = schemaCommon.schemaCommonMinItems
97+schemaMinLength = parameterSchema.paramSchemaMinLength
98
98
99−schemaUniqueItems :: HasSchemaCommon s => Lens' s (Maybe Bool)
99+schemaPattern :: HasParamSchema s t i => Lens' s (Maybe Text)
100−schemaUniqueItems = schemaCommon.schemaCommonUniqueItems
100+schemaPattern = parameterSchema.paramSchemaPattern
101
101
102−schemaEnum :: HasSchemaCommon s => Lens' s (Maybe [Value])
102+schemaMaxItems :: HasParamSchema s t i => Lens' s (Maybe Integer)
103−schemaEnum = schemaCommon.schemaCommonEnum
103+schemaMaxItems = parameterSchema.paramSchemaMaxItems
104
104
105+schemaMinItems :: HasParamSchema s t i => Lens' s (Maybe Integer)
106+schemaMinItems = parameterSchema.paramSchemaMinItems
107+
108+schemaUniqueItems :: HasParamSchema s t i => Lens' s (Maybe Bool)
109+schemaUniqueItems = parameterSchema.paramSchemaUniqueItems
110+
111+schemaEnum :: HasParamSchema s t i => Lens' s (Maybe [Value])
112+schemaEnum = parameterSchema.paramSchemaEnum
113+
105−schemaMultipleOf :: HasSchemaCommon s => Lens' s (Maybe Scientific)
114+schemaMultipleOf :: HasParamSchema s t i => Lens' s (Maybe Scientific)
106−schemaMultipleOf = schemaCommon.schemaCommonMultipleOf
115+schemaMultipleOf = parameterSchema.paramSchemaMultipleOf
107
116
src/Data/Swagger/ParamSchema.hsnew file+23
@@ -0,0 +1,23 @@
1+-- |
2+-- Module: Data.Swagger.ParamSchema
3+-- Copyright: (c) 2015 GetShopTV
4+-- License: BSD3
5+-- Maintainer: Nickolay Kudasov <nickolay@getshoptv.com>
6+-- Stability: experimental
7+--
8+-- Types and functions for working with Swagger parameter schema.
9+module Data.Swagger.ParamSchema (
10+ -- * Encoding
11+ ToParamSchema(..),
12+
13+ -- * Generic schema encoding
14+ genericToParamSchema,
15+ toParamSchemaBoundedIntegral,
16+
17+ -- * Generic encoding configuration
18+ SchemaOptions(..),
19+ defaultSchemaOptions,
20+) where
21+
22+import Data.Swagger.Internal.ParamSchema
23+import Data.Swagger.SchemaOptions
src/Data/Swagger/Schema.hs+4−1
@@ -19,10 +19,13 @@ module Data.Swagger.Schema (
19 genericToNamedSchema,
19 genericToNamedSchema,
20 genericToNamedSchemaBoundedIntegral,
20 genericToNamedSchemaBoundedIntegral,
21 toSchemaBoundedIntegral,
21 toSchemaBoundedIntegral,
22+ paramSchemaToNamedSchema,
23+ paramSchemaToSchema,
22
24
23 -- * Generic encoding configuration
25 -- * Generic encoding configuration
24 SchemaOptions(..),
26 SchemaOptions(..),
25 defaultSchemaOptions,
27 defaultSchemaOptions,
26) where
28) where
27
29
28−import Data.Swagger.Schema.Internal
30+import Data.Swagger.Internal.Schema
31+import Data.Swagger.SchemaOptions
src/Data/Swagger/SchemaOptions.hsnew file+55
@@ -0,0 +1,55 @@
1+module Data.Swagger.SchemaOptions where
2+
3+-- | Options that specify how to encode your type to Swagger schema.
4+data SchemaOptions = SchemaOptions
5+ { -- | Function applied to field labels. Handy for removing common record prefixes for example.
6+ fieldLabelModifier :: String -> String
7+ -- | Function applied to constructor tags which could be handy for lower-casing them for example.
8+ , constructorTagModifier :: String -> String
9+ -- | Function applied to datatype name.
10+ , datatypeNameModifier :: String -> String
11+ -- | If @'True'@ the constructors of a datatype, with all nullary constructors,
12+ -- will be encoded to a string enumeration schema with the constructor tags as possible values.
13+ , allNullaryToStringTag :: Bool
14+ -- | If @'True'@ direct subschemas will be referenced if possible (rather than inlined).
15+ -- Note that this option does not influence nested schemas, e.g. for these types
16+ --
17+ -- @
18+ -- data Object = Object String deriving Generic
19+ -- instance ToSchema Object
20+ --
21+ -- newtype Objects = Objects [Object] deriving Generic
22+ -- instance ToSchema Objects where
23+ -- toNamedSchema = genericToNamedSchema defaultSchemaOptions
24+ -- { useReferences = False }
25+ -- @
26+ --
27+ -- Schema for @Objects@ __will not__ inline @Object@ schema because
28+ -- it is nested in a @[]@ schema.
29+ , useReferences :: Bool
30+ -- | Hide the field name when a record constructor has only one field, like a newtype.
31+ , unwrapUnaryRecords :: Bool
32+ }
33+
34+-- | Default encoding @'SchemaOptions'@.
35+--
36+-- @
37+-- 'SchemaOptions'
38+-- { 'fieldLabelModifier' = id
39+-- , 'constructorTagModifier' = id
40+-- , 'datatypeNameModifier' = id
41+-- , 'allNullaryToStringTag' = True
42+-- , 'useReferences' = True
43+-- , 'unwrapUnaryRecords' = False
44+-- }
45+-- @
46+defaultSchemaOptions :: SchemaOptions
47+defaultSchemaOptions = SchemaOptions
48+ { fieldLabelModifier = id
49+ , constructorTagModifier = id
50+ , datatypeNameModifier = id
51+ , allNullaryToStringTag = True
52+ , useReferences = True
53+ , unwrapUnaryRecords = False
54+ }
55+
swagger2.cabal+5−1
@@ -21,12 +21,15 @@ library
21 exposed-modules:
21 exposed-modules:
22 Data.Swagger
22 Data.Swagger
23 Data.Swagger.Lens
23 Data.Swagger.Lens
24+ Data.Swagger.ParamSchema
24 Data.Swagger.Schema
25 Data.Swagger.Schema
26+ Data.Swagger.SchemaOptions
25
27
26 -- internal modules
28 -- internal modules
27 Data.Swagger.Internal
29 Data.Swagger.Internal
30+ Data.Swagger.Internal.Schema
31+ Data.Swagger.Internal.ParamSchema
28 Data.Swagger.Internal.Utils
32 Data.Swagger.Internal.Utils
29− Data.Swagger.Schema.Internal
30 build-depends: base == 4.*
33 build-depends: base == 4.*
31 , aeson < 0.10
34 , aeson < 0.10
32 , containers
35 , containers
@@ -56,6 +59,7 @@ test-suite spec
56 , containers
59 , containers
57 , unordered-containers
60 , unordered-containers
58 , vector
61 , vector
62+ , lens
59 other-modules:
63 other-modules:
60 SpecCommon
64 SpecCommon
61 Data.SwaggerSpec
65 Data.SwaggerSpec
test/Data/Swagger/ParamSchemaSpec.hsnew file+97
@@ -0,0 +1,97 @@
1+{-# LANGUAGE DeriveGeneric #-}
2+{-# LANGUAGE DeriveAnyClass #-}
3+{-# LANGUAGE ScopedTypeVariables #-}
4+{-# LANGUAGE TemplateHaskell #-}
5+{-# LANGUAGE QuasiQuotes #-}
6+module Data.Swagger.ParamSchemaSpec where
7+
8+import Data.Aeson
9+import Data.Aeson.QQ
10+import Data.Char
11+import Data.Proxy
12+import Data.Set (Set)
13+import GHC.Generics
14+
15+import Data.Swagger
16+
17+import SpecCommon
18+import Test.Hspec
19+
20+checkToParamSchema :: ToParamSchema a => Proxy a -> Value -> Spec
21+checkToParamSchema proxy js = (toParamSchema proxy :: ParamSchema Param Items) <=> js
22+
23+spec :: Spec
24+spec = do
25+ describe "Generic ToParamSchema" $ do
26+ context "Color (bounded enum)" $ checkToParamSchema (Proxy :: Proxy Color) colorSchemaJSON
27+ context "Status (constructorTagModifier)" $ checkToParamSchema (Proxy :: Proxy Status) statusSchemaJSON
28+ context "Unary records" $ do
29+ context "Email (unary record)" $ checkToParamSchema (Proxy :: Proxy Email) emailSchemaJSON
30+ context "UserId (non-record newtype)" $ checkToParamSchema (Proxy :: Proxy UserId) userIdSchemaJSON
31+
32+main :: IO ()
33+main = hspec spec
34+
35+-- ========================================================================
36+-- Color (enum)
37+-- ========================================================================
38+data Color
39+ = Red
40+ | Green
41+ | Blue
42+ deriving (Generic, ToParamSchema)
43+
44+colorSchemaJSON :: Value
45+colorSchemaJSON = [aesonQQ|
46+{
47+ "type": "string",
48+ "enum": ["Red", "Green", "Blue"]
49+}
50+|]
51+
52+-- ========================================================================
53+-- Status (constructorTagModifier)
54+-- ========================================================================
55+
56+data Status = StatusOk | StatusError deriving (Generic)
57+
58+instance ToParamSchema Status where
59+ toParamSchema = genericToParamSchema defaultSchemaOptions
60+ { constructorTagModifier = map toLower . drop (length "Status") }
61+
62+statusSchemaJSON :: Value
63+statusSchemaJSON = [aesonQQ|
64+{
65+ "type": "string",
66+ "enum": ["ok", "error"]
67+}
68+|]
69+
70+-- ========================================================================
71+-- Email (newtype with unwrapUnaryRecords set to True)
72+-- ========================================================================
73+
74+newtype Email = Email { getEmail :: String }
75+ deriving (Generic, ToParamSchema)
76+
77+emailSchemaJSON :: Value
78+emailSchemaJSON = [aesonQQ|
79+{
80+ "type": "string"
81+}
82+|]
83+
84+-- ========================================================================
85+-- UserId (non-record newtype)
86+-- ========================================================================
87+
88+newtype UserId = UserId Integer
89+ deriving (Generic, ToParamSchema)
90+
91+userIdSchemaJSON :: Value
92+userIdSchemaJSON = [aesonQQ|
93+{
94+ "type": "integer"
95+}
96+|]
97+
test/Data/Swagger/SchemaSpec.hs+18
@@ -33,6 +33,7 @@ spec = do
33 context "ISPair" $ checkToSchema (Proxy :: Proxy ISPair) ispairSchemaJSON
33 context "ISPair" $ checkToSchema (Proxy :: Proxy ISPair) ispairSchemaJSON
34 context "Point (fieldLabelModifier)" $ checkToSchema (Proxy :: Proxy Point) pointSchemaJSON
34 context "Point (fieldLabelModifier)" $ checkToSchema (Proxy :: Proxy Point) pointSchemaJSON
35 context "Color (bounded enum)" $ checkToSchema (Proxy :: Proxy Color) colorSchemaJSON
35 context "Color (bounded enum)" $ checkToSchema (Proxy :: Proxy Color) colorSchemaJSON
36+ context "Shade (paramSchemaToNamedSchema)" $ checkToSchema (Proxy :: Proxy Shade) shadeSchemaJSON
36 context "Paint (record with bounded enum field)" $ checkToSchema (Proxy :: Proxy Paint) paintSchemaJSON
37 context "Paint (record with bounded enum field)" $ checkToSchema (Proxy :: Proxy Paint) paintSchemaJSON
37 context "UserGroup (set newtype)" $ checkToSchema (Proxy :: Proxy UserGroup) userGroupSchemaJSON
38 context "UserGroup (set newtype)" $ checkToSchema (Proxy :: Proxy UserGroup) userGroupSchemaJSON
38 context "Unary records" $ do
39 context "Unary records" $ do
@@ -49,6 +50,7 @@ spec = do
49 context "String" $ checkSchemaName Nothing (Proxy :: Proxy String)
50 context "String" $ checkSchemaName Nothing (Proxy :: Proxy String)
50 context "(Int, Float)" $ checkSchemaName Nothing (Proxy :: Proxy (Int, Float))
51 context "(Int, Float)" $ checkSchemaName Nothing (Proxy :: Proxy (Int, Float))
51 context "Person" $ checkSchemaName (Just "Person") (Proxy :: Proxy Person)
52 context "Person" $ checkSchemaName (Just "Person") (Proxy :: Proxy Person)
53+ context "Shade" $ checkSchemaName (Just "Shade") (Proxy :: Proxy Shade)
52
54
53main :: IO ()
55main :: IO ()
54main = hspec spec
56main = hspec spec
@@ -137,6 +139,22 @@ colorSchemaJSON = [aesonQQ|
137}
139}
138|]
140|]
139
141
142+-- ========================================================================
143+-- Shade (paramSchemaToNamedSchema)
144+-- ========================================================================
145+
146+data Shade = Dim | Bright deriving (Generic, ToParamSchema)
147+
148+instance ToSchema Shade where toNamedSchema = paramSchemaToNamedSchema defaultSchemaOptions
149+
150+shadeSchemaJSON :: Value
151+shadeSchemaJSON = [aesonQQ|
152+{
153+ "type": "string",
154+ "enum": ["Dim", "Bright"]
155+}
156+|]
157+
140-- ========================================================================
158-- ========================================================================
141-- Paint (record with bounded enum property)
159-- Paint (record with bounded enum property)
142-- ========================================================================
160-- ========================================================================
test/Data/SwaggerSpec.hs+81−88
@@ -4,12 +4,15 @@
4{-# LANGUAGE QuasiQuotes #-}
4{-# LANGUAGE QuasiQuotes #-}
5module Data.SwaggerSpec where
5module Data.SwaggerSpec where
6
6
7+import Control.Lens
8+
7import Data.Aeson
9import Data.Aeson
8import Data.Aeson.QQ
10import Data.Aeson.QQ
9import Data.HashMap.Strict (HashMap)
11import Data.HashMap.Strict (HashMap)
10import Data.Text (Text)
12import Data.Text (Text)
11
13
12import Data.Swagger
14import Data.Swagger
15+import Data.Swagger.Lens
13import SpecCommon
16import SpecCommon
14import Test.Hspec
17import Test.Hspec
15
18
@@ -25,7 +28,7 @@ spec = do
25 context "Model with Map/Dictionary Properties" $ schemaModelDictExample <~> schemaModelDictExampleJSON
28 context "Model with Map/Dictionary Properties" $ schemaModelDictExample <~> schemaModelDictExampleJSON
26 context "Model with Example" $ schemaWithExampleExample <~> schemaWithExampleExampleJSON
29 context "Model with Example" $ schemaWithExampleExample <~> schemaWithExampleExampleJSON
27 describe "Definitions Object" $ definitionsExample <~> definitionsExampleJSON
30 describe "Definitions Object" $ definitionsExample <~> definitionsExampleJSON
28− describe "Parameters Definition Object" $ parametersDefinitionExample <~> parametersDefinitionExampleJSON
31+ describe "Parameters Definition Object" $ paramsDefinitionExample <~> paramsDefinitionExampleJSON
29 describe "Responses Definition Object" $ responsesDefinitionExample <~> responsesDefinitionExampleJSON
32 describe "Responses Definition Object" $ responsesDefinitionExample <~> responsesDefinitionExampleJSON
30 describe "Security Definitions Object" $ securityDefinitionsExample <~> securityDefinitionsExampleJSON
33 describe "Security Definitions Object" $ securityDefinitionsExample <~> securityDefinitionsExampleJSON
31 describe "Swagger Object" $ do
34 describe "Swagger Object" $ do
@@ -131,27 +134,27 @@ operationExample = mempty
131 , (405, Inline mempty { _responseDescription = "Invalid input" }) ] }
134 , (405, Inline mempty { _responseDescription = "Invalid input" }) ] }
132
135
133 params = map Inline
136 params = map Inline
134− [ Parameter
137+ [ Param
135− { _parameterName = "petId"
138+ { _paramName = "petId"
136− , _parameterDescription = Just "ID of pet that needs to be updated"
139+ , _paramDescription = Just "ID of pet that needs to be updated"
137− , _parameterRequired = Just True
140+ , _paramRequired = Just True
138− , _parameterSchema = ParameterOther (stringSchema ParameterPath) }
141+ , _paramSchema = ParamOther (stringSchema ParamPath) }
139− , Parameter
142+ , Param
140− { _parameterName = "name"
143+ { _paramName = "name"
141− , _parameterDescription = Just "Updated name of the pet"
144+ , _paramDescription = Just "Updated name of the pet"
142− , _parameterRequired = Just False
145+ , _paramRequired = Just False
143− , _parameterSchema = ParameterOther (stringSchema ParameterFormData) }
146+ , _paramSchema = ParamOther (stringSchema ParamFormData) }
144− , Parameter
147+ , Param
145− { _parameterName = "status"
148+ { _paramName = "status"
146− , _parameterDescription = Just "Updated status of the pet"
149+ , _paramDescription = Just "Updated status of the pet"
147− , _parameterRequired = Just False
150+ , _paramRequired = Just False
148− , _parameterSchema = ParameterOther (stringSchema ParameterFormData) }
151+ , _paramSchema = ParamOther (stringSchema ParamFormData) }
149 ]
152 ]
150
153
154+ stringSchema :: ParamLocation -> ParamOtherSchema
151 stringSchema i = mempty
155 stringSchema i = mempty
152− { _parameterOtherSchemaIn = i
156+ & paramOtherSchemaIn .~ i
153− , _parameterOtherSchemaType = ParamString
157+ & schemaType .~ SwaggerString
154− }
155
158
156operationExampleJSON :: Value
159operationExampleJSON :: Value
157operationExampleJSON = [aesonQQ|
160operationExampleJSON = [aesonQQ|
@@ -217,9 +220,8 @@ operationExampleJSON = [aesonQQ|
217
220
218schemaPrimitiveExample :: Schema
221schemaPrimitiveExample :: Schema
219schemaPrimitiveExample = mempty
222schemaPrimitiveExample = mempty
220− { _schemaType = SchemaString
223+ & schemaType .~ SwaggerString
221− , _schemaFormat = Just "email"
224+ & schemaFormat ?~ "email"
222− }
223
225
224schemaPrimitiveExampleJSON :: Value
226schemaPrimitiveExampleJSON :: Value
225schemaPrimitiveExampleJSON = [aesonQQ|
227schemaPrimitiveExampleJSON = [aesonQQ|
@@ -231,17 +233,15 @@ schemaPrimitiveExampleJSON = [aesonQQ|
231
233
232schemaSimpleModelExample :: Schema
234schemaSimpleModelExample :: Schema
233schemaSimpleModelExample = mempty
235schemaSimpleModelExample = mempty
234− { _schemaType = SchemaObject
236+ & schemaType .~ SwaggerObject
235− , _schemaRequired = [ "name" ]
237+ & schemaRequired .~ [ "name" ]
236− , _schemaProperties =
238+ & schemaProperties .~
237− [ ("name", Inline mempty
239+ [ ("name", Inline (mempty & schemaType .~ SwaggerString))
238− { _schemaType = SchemaString } )
239 , ("address", Ref (Reference "#/definitions/Address"))
240 , ("address", Ref (Reference "#/definitions/Address"))
240− , ("age", Inline mempty
241+ , ("age", Inline $ mempty
241− { _schemaType = SchemaInteger
242+ & schemaMinimum ?~ 0
242− , _schemaFormat = Just "int32"
243+ & schemaType .~ SwaggerInteger
243− , _schemaSchemaCommon = mempty
244+ & schemaFormat ?~ "int32" ) ]
244− { _schemaCommonMinimum = Just 0 } } ) ] }
245
245
246schemaSimpleModelExampleJSON :: Value
246schemaSimpleModelExampleJSON :: Value
247schemaSimpleModelExampleJSON = [aesonQQ|
247schemaSimpleModelExampleJSON = [aesonQQ|
@@ -268,9 +268,8 @@ schemaSimpleModelExampleJSON = [aesonQQ|
268
268
269schemaModelDictExample :: Schema
269schemaModelDictExample :: Schema
270schemaModelDictExample = mempty
270schemaModelDictExample = mempty
271− { _schemaType = SchemaObject
271+ & schemaType .~ SwaggerObject
272− , _schemaAdditionalProperties = Just mempty
272+ & schemaAdditionalProperties ?~ (mempty & schemaType .~ SwaggerString)
273− { _schemaType = SchemaString } }
274
273
275schemaModelDictExampleJSON :: Value
274schemaModelDictExampleJSON :: Value
276schemaModelDictExampleJSON = [aesonQQ|
275schemaModelDictExampleJSON = [aesonQQ|
@@ -283,14 +282,12 @@ schemaModelDictExampleJSON = [aesonQQ|
283|]
282|]
284
283
285schemaWithExampleExample :: Schema
284schemaWithExampleExample :: Schema
286−schemaWithExampleExample = mempty
285+schemaWithExampleExample = (mempty & schemaType .~ SwaggerObject)
287− { _schemaType = SchemaObject
288− , _schemaProperties =
286+ { _schemaProperties =
289− [ ("id", Inline mempty
287+ [ ("id", Inline $ mempty
290− { _schemaType = SchemaInteger
288+ & schemaType .~ SwaggerInteger
291− , _schemaFormat = Just "int64" })
289+ & schemaFormat ?~ "int64" )
292− , ("name", Inline mempty
290+ , ("name", Inline (mempty & schemaType .~ SwaggerString)) ]
293− { _schemaType = SchemaString }) ]
294 , _schemaRequired = [ "name" ]
291 , _schemaRequired = [ "name" ]
295 , _schemaExample = Just [aesonQQ|
292 , _schemaExample = Just [aesonQQ|
296 {
293 {
@@ -329,21 +326,19 @@ schemaWithExampleExampleJSON = [aesonQQ|
329definitionsExample :: HashMap Text Schema
326definitionsExample :: HashMap Text Schema
330definitionsExample =
327definitionsExample =
331 [ ("Category", mempty
328 [ ("Category", mempty
332− { _schemaType = SchemaObject
329+ & schemaType .~ SwaggerObject
333− , _schemaProperties =
330+ & schemaProperties .~
334− [ ("id", Inline mempty
331+ [ ("id", Inline $ mempty
335− { _schemaType = SchemaInteger
332+ & schemaType .~ SwaggerInteger
336− , _schemaFormat = Just "int64" })
333+ & schemaFormat ?~ "int64")
337− , ("name", Inline mempty
334+ , ("name", Inline (mempty & schemaType .~ SwaggerString)) ] )
338− { _schemaType = SchemaString }) ] })
339 , ("Tag", mempty
335 , ("Tag", mempty
340− { _schemaType = SchemaObject
336+ & schemaType .~ SwaggerObject
341− , _schemaProperties =
337+ & schemaProperties .~
342− [ ("id", Inline mempty
338+ [ ("id", Inline $ mempty
343− { _schemaType = SchemaInteger
339+ & schemaType .~ SwaggerInteger
344− , _schemaFormat = Just "int64" })
340+ & schemaFormat ?~ "int64")
345− , ("name", Inline mempty
341+ , ("name", Inline (mempty & schemaType .~ SwaggerString)) ] ) ]
346− { _schemaType = SchemaString }) ] }) ]
347
342
348definitionsExampleJSON :: Value
343definitionsExampleJSON :: Value
349definitionsExampleJSON = [aesonQQ|
344definitionsExampleJSON = [aesonQQ|
@@ -379,27 +374,27 @@ definitionsExampleJSON = [aesonQQ|
379-- Parameters Definition object
374-- Parameters Definition object
380-- =======================================================================
375-- =======================================================================
381
376
382−parametersDefinitionExample :: HashMap Text Parameter
377+paramsDefinitionExample :: HashMap Text Param
383−parametersDefinitionExample =
378+paramsDefinitionExample =
384 [ ("skipParam", mempty
379 [ ("skipParam", mempty
385− { _parameterName = "skip"
380+ { _paramName = "skip"
386− , _parameterDescription = Just "number of items to skip"
381+ , _paramDescription = Just "number of items to skip"
387− , _parameterRequired = Just True
382+ , _paramRequired = Just True
388− , _parameterSchema = ParameterOther mempty
383+ , _paramSchema = ParamOther $ mempty
389− { _parameterOtherSchemaIn = ParameterQuery
384+ & paramOtherSchemaIn .~ ParamQuery
390− , _parameterOtherSchemaType = ParamInteger
385+ & schemaType .~ SwaggerInteger
391− , _parameterOtherSchemaFormat = Just "int32" } })
386+ & schemaFormat ?~ "int32" })
392 , ("limitParam", mempty
387 , ("limitParam", mempty
393− { _parameterName = "limit"
388+ { _paramName = "limit"
394− , _parameterDescription = Just "max records to return"
389+ , _paramDescription = Just "max records to return"
395− , _parameterRequired = Just True
390+ , _paramRequired = Just True
396− , _parameterSchema = ParameterOther mempty
391+ , _paramSchema = ParamOther $ mempty
397− { _parameterOtherSchemaIn = ParameterQuery
392+ & paramOtherSchemaIn .~ ParamQuery
398− , _parameterOtherSchemaType = ParamInteger
393+ & schemaType .~ SwaggerInteger
399− , _parameterOtherSchemaFormat = Just "int32" } }) ]
394+ & schemaFormat ?~ "int32" }) ]
400
395
401−parametersDefinitionExampleJSON :: Value
396+paramsDefinitionExampleJSON :: Value
402−parametersDefinitionExampleJSON = [aesonQQ|
397+paramsDefinitionExampleJSON = [aesonQQ|
403{
398{
404 "skipParam": {
399 "skipParam": {
405 "name": "skip",
400 "name": "skip",
@@ -500,30 +495,28 @@ swaggerExample = mempty
500 { _operationResponses = mempty
495 { _operationResponses = mempty
501 { _responsesResponses =
496 { _responsesResponses =
502 [ (200, Inline mempty
497 [ (200, Inline mempty
503− { _responseSchema = Just $ Inline mempty
498+ { _responseSchema = Just $ Inline (mempty & schemaType .~ SwaggerObject)
504 { _schemaExample = Just [aesonQQ|
499 { _schemaExample = Just [aesonQQ|
505 {
500 {
506 "created": 100,
501 "created": 100,
507 "description": "get milk"
502 "description": "get milk"
508 } |]
503 } |]
509− , _schemaType = SchemaObject
510 , _schemaDescription = Just "This is some real Todo right here"
504 , _schemaDescription = Just "This is some real Todo right here"
511 , _schemaProperties =
505 , _schemaProperties =
512− [ ("created", Inline mempty
506+ [ ("created", Inline $ mempty
513− { _schemaType = SchemaInteger
507+ & schemaType .~ SwaggerInteger
514− , _schemaFormat = Just "int32" })
508+ & schemaFormat ?~ "int32")
515− , ("description", Inline mempty
509+ , ("description", Inline (mempty & schemaType .~ SwaggerString)) ] }
516− { _schemaType = SchemaString }) ] }
517 , _responseDescription = "OK" }) ] }
510 , _responseDescription = "OK" }) ] }
518 , _operationProduces = Just (MimeList [ "application/json" ])
511 , _operationProduces = Just (MimeList [ "application/json" ])
519 , _operationParameters =
512 , _operationParameters =
520 [ Inline mempty
513 [ Inline mempty
521− { _parameterRequired = Just True
514+ { _paramRequired = Just True
522− , _parameterName = "id"
515+ , _paramName = "id"
523− , _parameterDescription = Just "TodoId param"
516+ , _paramDescription = Just "TodoId param"
524− , _parameterSchema = ParameterOther mempty
517+ , _paramSchema = ParamOther $ mempty
525− { _parameterOtherSchemaIn = ParameterPath
518+ & paramOtherSchemaIn .~ ParamPath
526− , _parameterOtherSchemaType = ParamString } } ]
519+ & schemaType .~ SwaggerString } ]
527 , _operationTags = [ "todo" ] } }) ] } }
520 , _operationTags = [ "todo" ] } }) ] } }
528
521
529swaggerExampleJSON :: Value
522swaggerExampleJSON :: Value