1{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE QuasiQuotes #-}
3{-# LANGUAGE ScopedTypeVariables #-}
5module Data.Swagger.CommonTestTypes where
10import Data.Aeson (ToJSON (..), ToJSONKey (..), Value)
11import Data.Aeson.QQ.Simple
12import Data.Aeson.Types (toJSONKeyText)
17import qualified Data.Text as Text
21import Data.Swagger.Declare
22import Data.Swagger.Internal (SwaggerKind (..))
24-- ========================================================================
26-- ========================================================================
28data Unit = Unit deriving (Generic)
29instance ToParamSchema Unit
32unitSchemaJSON :: Value
33unitSchemaJSON = [aesonQQ|
40-- ========================================================================
42-- ========================================================================
48instance ToParamSchema Color
49instance ToSchema Color
51colorSchemaJSON :: Value
52colorSchemaJSON = [aesonQQ|
55 "enum": ["Red", "Green", "Blue"]
59-- ========================================================================
60-- Shade (paramSchemaToNamedSchema)
61-- ========================================================================
63data Shade = Dim | Bright deriving (Generic)
64instance ToParamSchema Shade
66instance ToSchema Shade where declareNamedSchema = pure . paramSchemaToNamedSchema defaultSchemaOptions
68shadeSchemaJSON :: Value
69shadeSchemaJSON = [aesonQQ|
72 "enum": ["Dim", "Bright"]
76-- ========================================================================
77-- Paint (record with bounded enum property)
78-- ========================================================================
80newtype Paint = Paint { color :: Color }
82instance ToSchema Paint
84paintSchemaJSON :: Value
85paintSchemaJSON = [aesonQQ|
92 "$ref": "#/definitions/Color"
99paintInlinedSchemaJSON :: Value
100paintInlinedSchemaJSON = [aesonQQ|
108 "enum": ["Red", "Green", "Blue"]
111 "required": ["color"]
115-- ========================================================================
116-- Status (constructorTagModifier)
117-- ========================================================================
124instance ToParamSchema Status where
125 toParamSchema = genericToParamSchema defaultSchemaOptions
126 { constructorTagModifier = map toLower . drop (length "Status") }
127instance ToSchema Status where
128 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
129 { constructorTagModifier = map toLower . drop (length "Status") }
131statusSchemaJSON :: Value
132statusSchemaJSON = [aesonQQ|
135 "enum": ["ok", "error"]
139-- ========================================================================
140-- Email (newtype with unwrapUnaryRecords set to True)
141-- ========================================================================
143newtype Email = Email { getEmail :: String }
145instance ToParamSchema Email
146instance ToSchema Email where
147 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
148 { unwrapUnaryRecords = True }
150emailSchemaJSON :: Value
151emailSchemaJSON = [aesonQQ|
157-- ========================================================================
158-- UserId (non-record newtype)
159-- ========================================================================
161newtype UserId = UserId Integer
162 deriving (Eq, Ord, Generic)
163instance ToParamSchema UserId
164instance ToSchema UserId
166userIdSchemaJSON :: Value
167userIdSchemaJSON = [aesonQQ|
173-- ========================================================================
174-- UserGroup (set newtype)
175-- ========================================================================
177newtype UserGroup = UserGroup (Set UserId)
179instance ToSchema UserGroup
181userGroupSchemaJSON :: Value
182userGroupSchemaJSON = [aesonQQ|
185 "items": { "$ref": "#/definitions/UserId" },
190-- ========================================================================
191-- Person (simple record with optional fields)
192-- ========================================================================
196 , email :: Maybe String
199instance ToSchema Person
201personSchemaJSON :: Value
202personSchemaJSON = [aesonQQ|
207 "name": { "type": "string" },
208 "phone": { "type": "integer" },
209 "email": { "type": "string" }
211 "required": ["name", "phone"]
215-- ========================================================================
216-- Player (record newtype)
217-- ========================================================================
219newtype Player = Player
222instance ToSchema Player
224playerSchemaJSON :: Value
225playerSchemaJSON = [aesonQQ|
232 "$ref": "#/definitions/Point"
235 "required": ["position"]
239newtype Players = Players [Inlined Player]
241instance ToSchema Players
243playersSchemaJSON :: Value
244playersSchemaJSON = [aesonQQ|
254 "$ref": "#/definitions/Point"
257 "required": ["position"]
262-- ========================================================================
263-- Character (sum type with ref and record in alternative)
264-- ========================================================================
268 | NPC { npcName :: String, npcPosition :: Point }
270instance ToSchema Character
272characterSchemaJSON :: Value
273characterSchemaJSON = [aesonQQ|
278 "PC": { "$ref": "#/definitions/Player" },
284 "npcName": { "type": "string" },
285 "npcPosition": { "$ref": "#/definitions/Point" }
287 "required": ["npcName", "npcPosition"]
295characterInlinedSchemaJSON :: Value
296characterInlinedSchemaJSON = [aesonQQ|
311 "x": { "type": "number", "format": "double" },
312 "y": { "type": "number", "format": "double" }
314 "required": ["x", "y"]
317 "required": ["position"]
324 "npcName": { "type": "string" },
330 "x": { "type": "number", "format": "double" },
331 "y": { "type": "number", "format": "double" }
333 "required": ["x", "y"]
336 "required": ["npcName", "npcPosition"]
344characterInlinedPlayerSchemaJSON :: Value
345characterInlinedPlayerSchemaJSON = [aesonQQ|
357 "$ref": "#/definitions/Point"
360 "required": ["position"]
367 "npcName": { "type": "string" },
368 "npcPosition": { "$ref": "#/definitions/Point" }
370 "required": ["npcName", "npcPosition"]
378-- ========================================================================
379-- ISPair (non-record product data type)
380-- ========================================================================
381data ISPair = ISPair Integer String
384instance ToSchema ISPair
386ispairSchemaJSON :: Value
387ispairSchemaJSON = [aesonQQ|
392 { "type": "integer" },
400-- ========================================================================
401-- Point (record data type with custom fieldLabelModifier)
402-- ========================================================================
409instance ToSchema Point where
410 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
411 { fieldLabelModifier = map toLower . drop (length "point") }
413pointSchemaJSON :: Value
414pointSchemaJSON = [aesonQQ|
419 "x": { "type": "number", "format": "double" },
420 "y": { "type": "number", "format": "double" }
422 "required": ["x", "y"]
426-- ========================================================================
427-- Point (record data type with multiple fields)
428-- ========================================================================
435 , point5V :: Double -- 5 dimensional!
438instance ToSchema Point5 where
439 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
440 { fieldLabelModifier = map toLower . drop (length "point5") }
442point5SchemaJSON :: Value
443point5SchemaJSON = [aesonQQ|
448 "x": { "type": "number", "format": "double" },
449 "y": { "type": "number", "format": "double" },
450 "z": { "type": "number", "format": "double" },
451 "u": { "type": "number", "format": "double" },
452 "v": { "type": "number", "format": "double" }
454 "required": ["x", "y", "z", "u", "v"]
458point5Properties :: [String]
459point5Properties = ["x", "y", "z", "u", "v"]
461-- ========================================================================
462-- MyRoseTree (custom datatypeNameModifier)
463-- ========================================================================
465data MyRoseTree = MyRoseTree
467 , trees :: [MyRoseTree]
470instance ToSchema MyRoseTree where
471 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
472 { datatypeNameModifier = drop (length "My") }
474myRoseTreeSchemaJSON :: Value
475myRoseTreeSchemaJSON = [aesonQQ|
480 "root": { "type": "string" },
486 "$ref": "#/definitions/RoseTree"
490 "required": ["root", "trees"]
494data MyRoseTree' = MyRoseTree'
496 , trees' :: [MyRoseTree']
499instance ToSchema MyRoseTree' where
500 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
501 { datatypeNameModifier = map toLower }
503myRoseTreeSchemaJSON' :: Value
504myRoseTreeSchemaJSON' = [aesonQQ|
509 "root'": { "type": "string" },
515 "$ref": "#/definitions/myrosetree'"
519 "required": ["root'", "trees'"]
523-- ========================================================================
524-- Inlined (newtype for inlining schemas)
525-- ========================================================================
527newtype Inlined a = Inlined { getInlined :: a }
529instance ToSchema a => ToSchema (Inlined a) where
530 declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
532 unname (NamedSchema _ s) = NamedSchema Nothing s
534-- ========================================================================
535-- Light (sum type with unwrapUnaryRecords)
536-- ========================================================================
542 | LightWaveLength { waveLength :: Double }
545instance ToSchema Light where
546 declareNamedSchema = genericDeclareNamedSchemaUnrestricted defaultSchemaOptions
547 { unwrapUnaryRecords = True }
549lightSchemaJSON :: Value
550lightSchemaJSON = [aesonQQ|
555 "NoLight": { "type": "array", "items": {}, "maxItems": 0, "example": [] },
556 "LightFreq": { "type": "number", "format": "double" },
557 "LightColor": { "$ref": "#/definitions/Color" },
558 "LightWaveLength": { "type": "number", "format": "double" }
565lightInlinedSchemaJSON :: Value
566lightInlinedSchemaJSON = [aesonQQ|
571 "NoLight": { "type": "array", "items": {}, "maxItems": 0, "example": [] },
572 "LightFreq": { "type": "number", "format": "double" },
576 "enum": ["Red", "Green", "Blue"]
578 "LightWaveLength": { "type": "number", "format": "double" }
585-- ========================================================================
586-- ResourceId (series of newtypes)
587-- ========================================================================
589newtype Id = Id String deriving (Generic)
592newtype ResourceId = ResourceId Id deriving (Generic)
593instance ToSchema ResourceId
595-- ========================================================================
596-- ButtonImages (bounded enum key mapping)
597-- ========================================================================
599data ButtonState = Neutral | Focus | Active | Hover | Disabled
600 deriving (Show, Bounded, Enum, Generic)
602instance ToJSON ButtonState
603instance ToSchema ButtonState
604instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (Text.pack . show)
606type ImageUrl = Text.Text
608newtype ButtonImages = ButtonImages { getButtonImages :: Map ButtonState ImageUrl }
611instance ToJSON ButtonImages where
612 toJSON = toJSON . getButtonImages
614instance ToSchema ButtonImages where
615 declareNamedSchema = genericDeclareNamedSchemaNewtype defaultSchemaOptions
616 declareSchemaBoundedEnumKeyMapping
618buttonImagesSchemaJSON :: Value
619buttonImagesSchemaJSON = [aesonQQ|
624 "Neutral": { "type": "string" },
625 "Focus": { "type": "string" },
626 "Active": { "type": "string" },
627 "Hover": { "type": "string" },
628 "Disabled": { "type": "string" }
633-- ========================================================================
634-- SingleMaybeField (single field data with optional field)
635-- ========================================================================
637data SingleMaybeField = SingleMaybeField { singleMaybeField :: Maybe String }
638 deriving (Show, Generic)
640instance ToJSON SingleMaybeField
641instance ToSchema SingleMaybeField
643singleMaybeFieldSchemaJSON :: Value
644singleMaybeFieldSchemaJSON = [aesonQQ|
649 "singleMaybeField": { "type": "string" }
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list