git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #32 from GetShopTV/int-format-#29

Use formats from Swagger spec where appropriate

commita4ac74c008a216f825c9d13ecd936112828b04d0 authorNickolay Kudasov <fizruk@users.noreply.github.com> dateFri 25 Dec 2015, 17:44 (11 years ago) parent77c2923112e7ef
3 files changed+30−28
src/Data/Swagger/Internal/ParamSchema.hs+14−8 src/Data/Swagger/Internal/Schema.hs+6−10 test/Data/Swagger/SchemaSpec.hs+10−10
src/Data/Swagger/Internal/ParamSchema.hs+14−8
@@ -82,8 +82,12 @@ instance ToParamSchema Integer where
8282instance ToParamSchema Int where toParamSchema = toParamSchemaBoundedIntegral
8383instance ToParamSchema Int8 where toParamSchema = toParamSchemaBoundedIntegral
8484instance ToParamSchema Int16 where toParamSchema = toParamSchemaBoundedIntegral
85−instance ToParamSchema Int32 where toParamSchema = toParamSchemaBoundedIntegral
86−instance ToParamSchema Int64 where toParamSchema = toParamSchemaBoundedIntegral
85+
86+instance ToParamSchema Int32 where
87+ toParamSchema proxy = toParamSchemaBoundedIntegral proxy & schemaFormat ?~ "int32"
88+
89+instance ToParamSchema Int64 where
90+ toParamSchema proxy = toParamSchemaBoundedIntegral proxy & schemaFormat ?~ "int64"
8791
8892instance ToParamSchema Word where toParamSchema = toParamSchemaBoundedIntegral
8993instance ToParamSchema Word8 where toParamSchema = toParamSchemaBoundedIntegral
@@ -111,21 +115,23 @@ instance ToParamSchema Scientific where
111115 toParamSchema _ = mempty & schemaType .~ SwaggerNumber
112116
113117instance ToParamSchema Double where
114− toParamSchema _ = mempty & schemaType .~ SwaggerNumber
118+ toParamSchema _ = mempty
119+ & schemaType .~ SwaggerNumber
120+ & schemaFormat ?~ "double"
115121
116122instance ToParamSchema Float where
117− toParamSchema _ = mempty & schemaType .~ SwaggerNumber
123+ toParamSchema _ = mempty
124+ & schemaType .~ SwaggerNumber
125+ & schemaFormat ?~ "float"
118126
119127timeParamSchema :: String -> ParamSchema t
120128timeParamSchema format = mempty
121129 & schemaType .~ SwaggerString
122130 & schemaFormat ?~ T.pack format
123131
124−-- |
125−-- >>> toParamSchema (Proxy :: Proxy Day) ^. schemaFormat
126−-- Just "yyyy-mm-dd"
132+-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
127133instance ToParamSchema Day where
128− toParamSchema _ = timeParamSchema "yyyy-mm-dd"
134+ toParamSchema _ = timeParamSchema "date"
129135
130136-- |
131137-- >>> toParamSchema (Proxy :: Proxy LocalTime) ^. schemaFormat
src/Data/Swagger/Internal/Schema.hs+6−10
@@ -128,7 +128,7 @@ declareSchema = fmap snd . declareNamedSchema
128128-- (Nothing,"{\"type\":\"string\"}")
129129--
130130-- >>> encode <$> toNamedSchema (Proxy :: Proxy Day)
131−-- (Just "Day","{\"format\":\"yyyy-mm-dd\",\"type\":\"string\"}")
131+-- (Just "Day","{\"format\":\"date\",\"type\":\"string\"}")
132132toNamedSchema :: ToSchema a => proxy a -> NamedSchema
133133toNamedSchema = undeclare . declareNamedSchema
134134
@@ -228,7 +228,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
228228-- | Convert a type into a schema without references.
229229--
230230-- >>> encode $ toInlinedSchema (Proxy :: Proxy [Day])
231−-- "{\"items\":{\"format\":\"yyyy-mm-dd\",\"type\":\"string\"},\"type\":\"array\"}"
231+-- "{\"items\":{\"format\":\"date\",\"type\":\"string\"},\"type\":\"array\"}"
232232--
233233-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
234234-- when inlining recursive schemas.
@@ -305,11 +305,9 @@ timeSchema format = mempty
305305 & schemaType .~ SwaggerString
306306 & schemaFormat ?~ format
307307
308−-- |
309−-- >>> toSchema (Proxy :: Proxy Day) ^. schemaFormat
310−-- Just "yyyy-mm-dd"
308+-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
311309instance ToSchema Day where
312− declareNamedSchema _ = pure $ named "Day" (timeSchema "yyyy-mm-dd")
310+ declareNamedSchema _ = pure $ named "Day" (timeSchema "date")
313311
314312-- |
315313-- >>> toSchema (Proxy :: Proxy LocalTime) ^. schemaFormat
@@ -317,11 +315,9 @@ instance ToSchema Day where
317315instance ToSchema LocalTime where
318316 declareNamedSchema _ = pure $ named "LocalTime" (timeSchema "yyyy-mm-ddThh:MM:ss")
319317
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.
323319instance 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"
325321
326322instance ToSchema NominalDiffTime where
327323 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
test/Data/Swagger/SchemaSpec.hs+10−10
@@ -152,8 +152,8 @@ pointSchemaJSON = [aesonQQ|
152152 "type": "object",
153153 "properties":
154154 {
155− "x": { "type": "number" },
156− "y": { "type": "number" }
155+ "x": { "type": "number", "format": "double" },
156+ "y": { "type": "number", "format": "double" }
157157 },
158158 "required": ["x", "y"]
159159}
@@ -458,8 +458,8 @@ characterInlinedSchemaJSON = [aesonQQ|
458458 "type": "object",
459459 "properties":
460460 {
461− "x": { "type": "number" },
462− "y": { "type": "number" }
461+ "x": { "type": "number", "format": "double" },
462+ "y": { "type": "number", "format": "double" }
463463 },
464464 "required": ["x", "y"]
465465 }
@@ -477,8 +477,8 @@ characterInlinedSchemaJSON = [aesonQQ|
477477 "type": "object",
478478 "properties":
479479 {
480− "x": { "type": "number" },
481− "y": { "type": "number" }
480+ "x": { "type": "number", "format": "double" },
481+ "y": { "type": "number", "format": "double" }
482482 },
483483 "required": ["x", "y"]
484484 }
@@ -545,9 +545,9 @@ lightSchemaJSON = [aesonQQ|
545545 "type": "object",
546546 "properties":
547547 {
548− "LightFreq": { "type": "number" },
548+ "LightFreq": { "type": "number", "format": "double" },
549549 "LightColor": { "$ref": "#/definitions/Color" },
550− "LightWaveLength": { "type": "number" }
550+ "LightWaveLength": { "type": "number", "format": "double" }
551551 },
552552 "maxProperties": 1,
553553 "minProperties": 1
@@ -560,13 +560,13 @@ lightInlinedSchemaJSON = [aesonQQ|
560560 "type": "object",
561561 "properties":
562562 {
563− "LightFreq": { "type": "number" },
563+ "LightFreq": { "type": "number", "format": "double" },
564564 "LightColor":
565565 {
566566 "type": "string",
567567 "enum": ["Red", "Green", "Blue"]
568568 },
569− "LightWaveLength": { "type": "number" }
569+ "LightWaveLength": { "type": "number", "format": "double" }
570570 },
571571 "maxProperties": 1,
572572 "minProperties": 1