git.iko.soy/typeable/swagger2
on master help sign in
Use formats from Swagger spec where appropriate
112e7efNickolay Kudasov
src/Data/Swagger/Internal/ParamSchema.hs+14−8
@@ -82,8 +82,12 @@ instance ToParamSchema Integer where
82instance ToParamSchema Int where toParamSchema = toParamSchemaBoundedIntegral
82instance ToParamSchema Int where toParamSchema = toParamSchemaBoundedIntegral
83instance ToParamSchema Int8 where toParamSchema = toParamSchemaBoundedIntegral
83instance ToParamSchema Int8 where toParamSchema = toParamSchemaBoundedIntegral
84instance ToParamSchema Int16 where toParamSchema = toParamSchemaBoundedIntegral
84instance ToParamSchema Int16 where toParamSchema = toParamSchemaBoundedIntegral
85+
85−instance ToParamSchema Int32 where toParamSchema = toParamSchemaBoundedIntegral
86+instance ToParamSchema Int32 where
87+ toParamSchema proxy = toParamSchemaBoundedIntegral proxy & schemaFormat ?~ "int32"
88+
86−instance ToParamSchema Int64 where toParamSchema = toParamSchemaBoundedIntegral
89+instance ToParamSchema Int64 where
90+ toParamSchema proxy = toParamSchemaBoundedIntegral proxy & schemaFormat ?~ "int64"
87
91
88instance ToParamSchema Word where toParamSchema = toParamSchemaBoundedIntegral
92instance ToParamSchema Word where toParamSchema = toParamSchemaBoundedIntegral
89instance ToParamSchema Word8 where toParamSchema = toParamSchemaBoundedIntegral
93instance ToParamSchema Word8 where toParamSchema = toParamSchemaBoundedIntegral
@@ -111,21 +115,23 @@ instance ToParamSchema Scientific where
111 toParamSchema _ = mempty & schemaType .~ SwaggerNumber
115 toParamSchema _ = mempty & schemaType .~ SwaggerNumber
112
116
113instance ToParamSchema Double where
117instance ToParamSchema Double where
118+ toParamSchema _ = mempty
114− toParamSchema _ = mempty & schemaType .~ SwaggerNumber
119+ & schemaType .~ SwaggerNumber
120+ & schemaFormat ?~ "double"
115
121
116instance ToParamSchema Float where
122instance ToParamSchema Float where
123+ toParamSchema _ = mempty
117− toParamSchema _ = mempty & schemaType .~ SwaggerNumber
124+ & schemaType .~ SwaggerNumber
125+ & schemaFormat ?~ "float"
118
126
119timeParamSchema :: String -> ParamSchema t
127timeParamSchema :: String -> ParamSchema t
120timeParamSchema format = mempty
128timeParamSchema format = mempty
121 & schemaType .~ SwaggerString
129 & schemaType .~ SwaggerString
122 & schemaFormat ?~ T.pack format
130 & schemaFormat ?~ T.pack format
123
131
124−-- |
132+-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
125−-- >>> toParamSchema (Proxy :: Proxy Day) ^. schemaFormat
126−-- Just "yyyy-mm-dd"
127instance ToParamSchema Day where
133instance ToParamSchema Day where
128− toParamSchema _ = timeParamSchema "yyyy-mm-dd"
134+ toParamSchema _ = timeParamSchema "date"
129
135
130-- |
136-- |
131-- >>> toParamSchema (Proxy :: Proxy LocalTime) ^. schemaFormat
137-- >>> toParamSchema (Proxy :: Proxy LocalTime) ^. schemaFormat
src/Data/Swagger/Internal/Schema.hs+6−10
@@ -128,7 +128,7 @@ declareSchema = fmap snd . declareNamedSchema
128-- (Nothing,"{\"type\":\"string\"}")
128-- (Nothing,"{\"type\":\"string\"}")
129--
129--
130-- >>> encode <$> toNamedSchema (Proxy :: Proxy Day)
130-- >>> encode <$> toNamedSchema (Proxy :: Proxy Day)
131−-- (Just "Day","{\"format\":\"yyyy-mm-dd\",\"type\":\"string\"}")
131+-- (Just "Day","{\"format\":\"date\",\"type\":\"string\"}")
132toNamedSchema :: ToSchema a => proxy a -> NamedSchema
132toNamedSchema :: ToSchema a => proxy a -> NamedSchema
133toNamedSchema = undeclare . declareNamedSchema
133toNamedSchema = undeclare . declareNamedSchema
134
134
@@ -228,7 +228,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
228-- | Convert a type into a schema without references.
228-- | Convert a type into a schema without references.
229--
229--
230-- >>> encode $ toInlinedSchema (Proxy :: Proxy [Day])
230-- >>> encode $ toInlinedSchema (Proxy :: Proxy [Day])
231−-- "{\"items\":{\"format\":\"yyyy-mm-dd\",\"type\":\"string\"},\"type\":\"array\"}"
231+-- "{\"items\":{\"format\":\"date\",\"type\":\"string\"},\"type\":\"array\"}"
232--
232--
233-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
233-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
234-- when inlining recursive schemas.
234-- when inlining recursive schemas.
@@ -305,11 +305,9 @@ timeSchema format = mempty
305 & schemaType .~ SwaggerString
305 & schemaType .~ SwaggerString
306 & schemaFormat ?~ format
306 & schemaFormat ?~ format
307
307
308−-- |
308+-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
309−-- >>> toSchema (Proxy :: Proxy Day) ^. schemaFormat
310−-- Just "yyyy-mm-dd"
311instance ToSchema Day where
309instance ToSchema Day where
312− declareNamedSchema _ = pure $ named "Day" (timeSchema "yyyy-mm-dd")
310+ declareNamedSchema _ = pure $ named "Day" (timeSchema "date")
313
311
314-- |
312-- |
315-- >>> toSchema (Proxy :: Proxy LocalTime) ^. schemaFormat
313-- >>> toSchema (Proxy :: Proxy LocalTime) ^. schemaFormat
@@ -317,11 +315,9 @@ instance ToSchema Day where
317instance ToSchema LocalTime where
315instance ToSchema LocalTime where
318 declareNamedSchema _ = pure $ named "LocalTime" (timeSchema "yyyy-mm-ddThh:MM:ss")
316 declareNamedSchema _ = pure $ named "LocalTime" (timeSchema "yyyy-mm-ddThh:MM:ss")
319
317
320−-- |
321−-- >>> toSchema (Proxy :: Proxy ZonedTime) ^. schemaFormat
322−-- Just "yyyy-mm-ddThh:MM:ss(Z|+hh:MM)"
318+-- | Format @"date"@ corresponds to @yyyy-mm-ddThh:MM:ss(Z|+hh:MM)@ format.
323instance ToSchema ZonedTime where
319instance ToSchema ZonedTime where
324− declareNamedSchema _ = pure $ named "ZonedTime" $ timeSchema "yyyy-mm-ddThh:MM:ss(Z|+hh:MM)"
320+ declareNamedSchema _ = pure $ named "ZonedTime" $ timeSchema "date-time"
325
321
326instance ToSchema NominalDiffTime where
322instance ToSchema NominalDiffTime where
327 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
323 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
test/Data/Swagger/SchemaSpec.hs+10−10
@@ -152,8 +152,8 @@ pointSchemaJSON = [aesonQQ|
152 "type": "object",
152 "type": "object",
153 "properties":
153 "properties":
154 {
154 {
155− "x": { "type": "number" },
155+ "x": { "type": "number", "format": "double" },
156− "y": { "type": "number" }
156+ "y": { "type": "number", "format": "double" }
157 },
157 },
158 "required": ["x", "y"]
158 "required": ["x", "y"]
159}
159}
@@ -458,8 +458,8 @@ characterInlinedSchemaJSON = [aesonQQ|
458 "type": "object",
458 "type": "object",
459 "properties":
459 "properties":
460 {
460 {
461− "x": { "type": "number" },
461+ "x": { "type": "number", "format": "double" },
462− "y": { "type": "number" }
462+ "y": { "type": "number", "format": "double" }
463 },
463 },
464 "required": ["x", "y"]
464 "required": ["x", "y"]
465 }
465 }
@@ -477,8 +477,8 @@ characterInlinedSchemaJSON = [aesonQQ|
477 "type": "object",
477 "type": "object",
478 "properties":
478 "properties":
479 {
479 {
480− "x": { "type": "number" },
480+ "x": { "type": "number", "format": "double" },
481− "y": { "type": "number" }
481+ "y": { "type": "number", "format": "double" }
482 },
482 },
483 "required": ["x", "y"]
483 "required": ["x", "y"]
484 }
484 }
@@ -545,9 +545,9 @@ lightSchemaJSON = [aesonQQ|
545 "type": "object",
545 "type": "object",
546 "properties":
546 "properties":
547 {
547 {
548− "LightFreq": { "type": "number" },
548+ "LightFreq": { "type": "number", "format": "double" },
549 "LightColor": { "$ref": "#/definitions/Color" },
549 "LightColor": { "$ref": "#/definitions/Color" },
550− "LightWaveLength": { "type": "number" }
550+ "LightWaveLength": { "type": "number", "format": "double" }
551 },
551 },
552 "maxProperties": 1,
552 "maxProperties": 1,
553 "minProperties": 1
553 "minProperties": 1
@@ -560,13 +560,13 @@ lightInlinedSchemaJSON = [aesonQQ|
560 "type": "object",
560 "type": "object",
561 "properties":
561 "properties":
562 {
562 {
563− "LightFreq": { "type": "number" },
563+ "LightFreq": { "type": "number", "format": "double" },
564 "LightColor":
564 "LightColor":
565 {
565 {
566 "type": "string",
566 "type": "string",
567 "enum": ["Red", "Green", "Blue"]
567 "enum": ["Red", "Green", "Blue"]
568 },
568 },
569− "LightWaveLength": { "type": "number" }
569+ "LightWaveLength": { "type": "number", "format": "double" }
570 },
570 },
571 "maxProperties": 1,
571 "maxProperties": 1,
572 "minProperties": 1
572 "minProperties": 1