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
23-- ========================================================================
25-- ========================================================================
27data Unit = Unit deriving (Generic)
28instance ToParamSchema Unit
31unitSchemaJSON :: Value
32unitSchemaJSON = [aesonQQ|
39-- ========================================================================
41-- ========================================================================
47instance ToParamSchema Color
48instance ToSchema Color
50colorSchemaJSON :: Value
51colorSchemaJSON = [aesonQQ|
54 "enum": ["Red", "Green", "Blue"]
58-- ========================================================================
59-- Shade (paramSchemaToNamedSchema)
60-- ========================================================================
62data Shade = Dim | Bright deriving (Generic)
63instance ToParamSchema Shade
65instance ToSchema Shade where declareNamedSchema = pure . paramSchemaToNamedSchema defaultSchemaOptions
67shadeSchemaJSON :: Value
68shadeSchemaJSON = [aesonQQ|
71 "enum": ["Dim", "Bright"]
75-- ========================================================================
76-- Paint (record with bounded enum property)
77-- ========================================================================
79newtype Paint = Paint { color :: Color }
81instance ToSchema Paint
83paintSchemaJSON :: Value
84paintSchemaJSON = [aesonQQ|
91 "$ref": "#/components/schemas/Color"
98paintInlinedSchemaJSON :: Value
99paintInlinedSchemaJSON = [aesonQQ|
107 "enum": ["Red", "Green", "Blue"]
110 "required": ["color"]
114-- ========================================================================
115-- Status (constructorTagModifier)
116-- ========================================================================
123instance ToParamSchema Status where
124 toParamSchema = genericToParamSchema defaultSchemaOptions
125 { constructorTagModifier = map toLower . drop (length "Status") }
126instance ToSchema Status where
127 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
128 { constructorTagModifier = map toLower . drop (length "Status") }
130statusSchemaJSON :: Value
131statusSchemaJSON = [aesonQQ|
134 "enum": ["ok", "error"]
138-- ========================================================================
139-- Email (newtype with unwrapUnaryRecords set to True)
140-- ========================================================================
142newtype Email = Email { getEmail :: String }
144instance ToParamSchema Email
145instance ToSchema Email where
146 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
147 { unwrapUnaryRecords = True }
149emailSchemaJSON :: Value
150emailSchemaJSON = [aesonQQ|
156-- ========================================================================
157-- UserId (non-record newtype)
158-- ========================================================================
160newtype UserId = UserId Integer
161 deriving (Eq, Ord, Generic)
162instance ToParamSchema UserId
163instance ToSchema UserId
165userIdSchemaJSON :: Value
166userIdSchemaJSON = [aesonQQ|
172-- ========================================================================
173-- UserGroup (set newtype)
174-- ========================================================================
176newtype UserGroup = UserGroup (Set UserId)
178instance ToSchema UserGroup
180userGroupSchemaJSON :: Value
181userGroupSchemaJSON = [aesonQQ|
184 "items": { "$ref": "#/components/schemas/UserId" },
189-- ========================================================================
190-- Person (simple record with optional fields)
191-- ========================================================================
195 , email :: Maybe String
198instance ToSchema Person
200personSchemaJSON :: Value
201personSchemaJSON = [aesonQQ|
206 "name": { "type": "string" },
207 "phone": { "type": "integer" },
208 "email": { "type": "string" }
210 "required": ["name", "phone"]
214-- ========================================================================
215-- Player (record newtype)
216-- ========================================================================
218newtype Player = Player
221instance ToSchema Player
223playerSchemaJSON :: Value
224playerSchemaJSON = [aesonQQ|
231 "$ref": "#/components/schemas/Point"
234 "required": ["position"]
238newtype Players = Players [Inlined Player]
240instance ToSchema Players
242playersSchemaJSON :: Value
243playersSchemaJSON = [aesonQQ|
253 "$ref": "#/components/schemas/Point"
256 "required": ["position"]
261-- ========================================================================
262-- Character (sum type with ref and record in alternative)
263-- ========================================================================
267 | NPC { npcName :: String, npcPosition :: Point }
269instance ToSchema Character
271characterSchemaJSON :: Value
272characterSchemaJSON = [aesonQQ|
289 "$ref": "#/components/schemas/Player"
308 "$ref": "#/components/schemas/Point"
321characterInlinedSchemaJSON :: Value
322characterInlinedSchemaJSON = [aesonQQ|
406characterInlinedPlayerSchemaJSON :: Value
407characterInlinedPlayerSchemaJSON = [aesonQQ|
430 "$ref": "#/components/schemas/Point"
451 "$ref": "#/components/schemas/Point"
463-- ========================================================================
464-- ISPair (non-record product data type)
465-- ========================================================================
466data ISPair = ISPair Integer String
469instance ToSchema ISPair
471ispairSchemaJSON :: Value
472ispairSchemaJSON = [aesonQQ|
477 { "type": "integer" },
485-- ========================================================================
486-- Point (record data type with custom fieldLabelModifier)
487-- ========================================================================
494instance ToSchema Point where
495 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
496 { fieldLabelModifier = map toLower . drop (length "point") }
498pointSchemaJSON :: Value
499pointSchemaJSON = [aesonQQ|
504 "x": { "type": "number", "format": "double" },
505 "y": { "type": "number", "format": "double" }
507 "required": ["x", "y"]
511-- ========================================================================
512-- Point (record data type with multiple fields)
513-- ========================================================================
520 , point5V :: Double -- 5 dimensional!
523instance ToSchema Point5 where
524 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
525 { fieldLabelModifier = map toLower . drop (length "point5") }
527point5SchemaJSON :: Value
528point5SchemaJSON = [aesonQQ|
533 "x": { "type": "number", "format": "double" },
534 "y": { "type": "number", "format": "double" },
535 "z": { "type": "number", "format": "double" },
536 "u": { "type": "number", "format": "double" },
537 "v": { "type": "number", "format": "double" }
539 "required": ["x", "y", "z", "u", "v"]
543point5Properties :: [String]
544point5Properties = ["x", "y", "z", "u", "v"]
546-- ========================================================================
547-- MyRoseTree (custom datatypeNameModifier)
548-- ========================================================================
550data MyRoseTree = MyRoseTree
552 , trees :: [MyRoseTree]
555instance ToSchema MyRoseTree where
556 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
557 { datatypeNameModifier = drop (length "My") }
559myRoseTreeSchemaJSON :: Value
560myRoseTreeSchemaJSON = [aesonQQ|
565 "root": { "type": "string" },
571 "$ref": "#/components/schemas/RoseTree"
575 "required": ["root", "trees"]
579data MyRoseTree' = MyRoseTree'
581 , trees' :: [MyRoseTree']
584instance ToSchema MyRoseTree' where
585 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
586 { datatypeNameModifier = map toLower }
588myRoseTreeSchemaJSON' :: Value
589myRoseTreeSchemaJSON' = [aesonQQ|
594 "root'": { "type": "string" },
600 "$ref": "#/components/schemas/myrosetree'"
604 "required": ["root'", "trees'"]
608-- ========================================================================
609-- Inlined (newtype for inlining schemas)
610-- ========================================================================
612newtype Inlined a = Inlined { getInlined :: a }
614instance ToSchema a => ToSchema (Inlined a) where
615 declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
617 unname (NamedSchema _ s) = NamedSchema Nothing s
619-- ========================================================================
620-- Light (sum type with unwrapUnaryRecords)
621-- ========================================================================
627 | LightWaveLength { waveLength :: Double }
630instance ToSchema Light where
631 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
632 { unwrapUnaryRecords = True }
634lightSchemaJSON :: Value
635lightSchemaJSON = [aesonQQ|
685 "$ref": "#/components/schemas/Color"
713lightInlinedSchemaJSON :: Value
714lightInlinedSchemaJSON = [aesonQQ|
797-- ========================================================================
798-- ResourceId (series of newtypes)
799-- ========================================================================
801newtype Id = Id String deriving (Generic)
804newtype ResourceId = ResourceId Id deriving (Generic)
805instance ToSchema ResourceId
807-- ========================================================================
808-- ButtonImages (bounded enum key mapping)
809-- ========================================================================
811data ButtonState = Neutral | Focus | Active | Hover | Disabled
812 deriving (Show, Bounded, Enum, Generic)
814instance ToJSON ButtonState
815instance ToSchema ButtonState
816instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (Text.pack . show)
818type ImageUrl = Text.Text
820newtype ButtonImages = ButtonImages { getButtonImages :: Map ButtonState ImageUrl }
823instance ToJSON ButtonImages where
824 toJSON = toJSON . getButtonImages
826instance ToSchema ButtonImages where
827 declareNamedSchema = genericDeclareNamedSchemaNewtype defaultSchemaOptions
828 declareSchemaBoundedEnumKeyMapping
830buttonImagesSchemaJSON :: Value
831buttonImagesSchemaJSON = [aesonQQ|
836 "Neutral": { "type": "string" },
837 "Focus": { "type": "string" },
838 "Active": { "type": "string" },
839 "Hover": { "type": "string" },
840 "Disabled": { "type": "string" }
845-- ========================================================================
846-- SingleMaybeField (single field data with optional field)
847-- ========================================================================
849data SingleMaybeField = SingleMaybeField { singleMaybeField :: Maybe String }
850 deriving (Show, Generic)
852instance ToJSON SingleMaybeField
853instance ToSchema SingleMaybeField
855singleMaybeFieldSchemaJSON :: Value
856singleMaybeFieldSchemaJSON = [aesonQQ|
861 "singleMaybeField": { "type": "string" }
867-- ========================================================================
869-- ========================================================================
874instance ToSchema TimeOfDay
875instance ToParamSchema TimeOfDay
878timeOfDaySchemaJSON :: Value
879timeOfDaySchemaJSON = [aesonQQ|
881 "example": "12:33:15",
887timeOfDayParamSchemaJSON :: Value
888timeOfDayParamSchemaJSON = [aesonQQ|
896-- ========================================================================
898-- ========================================================================
899data UnsignedInts = UnsignedInts
900 { unsignedIntsUint32 :: Word32
901 , unsignedIntsUint64 :: Word64
904instance ToSchema UnsignedInts where
905 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
906 { fieldLabelModifier = map toLower . drop (length "unsignedInts") }
908unsignedIntsSchemaJSON :: Value
909unsignedIntsSchemaJSON = [aesonQQ|
914 "uint32": { "type": "integer", "format": "int32", "minimum": 0, "maximum": 4294967295 },
915 "uint64": { "type": "integer", "format": "int64", "minimum": 0, "maximum": 18446744073709551615 }
917 "required": ["uint32", "uint64"]
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