git.iko.soy/typeable/swagger2
on master help sign in
Fix default schema for custom unit data types
commit41a26198fad1034d73016e9054512eb7ecb1e2f3 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateFri 15 Jan 2016, 22:23 (11 years ago) parent6eb4cb3
2 files changed+20−15
src/Data/Swagger/Internal/Schema.hs+18−13 test/Data/Swagger/SchemaSpec.hs+2−2
src/Data/Swagger/Internal/Schema.hs+18−13
@@ -292,7 +292,9 @@ instance ToSchema a => ToSchema (Maybe a) where
292292
293293instance (ToSchema a, ToSchema b) => ToSchema (Either a b)
294294
295−instance ToSchema ()
295+instance ToSchema () where
296+ declareNamedSchema _ = pure (Nothing, nullarySchema)
297+
296298instance (ToSchema a, ToSchema b) => ToSchema (a, b)
297299instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
298300instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d) => ToSchema (a, b, c, d)
@@ -425,9 +427,6 @@ gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
425427gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare Definitions Schema
426428gdeclareSchema opts proxy = snd <$> gdeclareNamedSchema opts proxy mempty
427429
428−instance GToSchema U1 where
429− gdeclareNamedSchema _ _ _ = plain nullarySchema
430−
431430instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
432431 gdeclareNamedSchema opts _ schema = do
433432 (_, gschema) <- gdeclareNamedSchema opts (Proxy :: Proxy g) schema
@@ -441,6 +440,9 @@ instance (Datatype d, GToSchema f) => GToSchema (D1 d f) where
441440instance {-# OVERLAPPABLE #-} GToSchema f => GToSchema (C1 c f) where
442441 gdeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy f)
443442
443+instance {-# OVERLAPPING #-} Constructor c => GToSchema (C1 c U1) where
444+ gdeclareNamedSchema = gdeclareNamedSumSchema
445+
444446-- | Single field constructor.
445447instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
446448 gdeclareNamedSchema opts _ s
@@ -511,16 +513,19 @@ instance {-# OVERLAPPABLE #-} ToSchema c => GToSchema (K1 i c) where
511513 gdeclareNamedSchema _ _ _ = declareNamedSchema (Proxy :: Proxy c)
512514
513515instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
514− gdeclareNamedSchema opts _ s
515− | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
516− | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
517− where
518− declareSumSchema = gsumToSchema opts (Proxy :: Proxy (f :+: g)) s
519− (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
516+ gdeclareNamedSchema = gdeclareNamedSumSchema
517+
518+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare Definitions NamedSchema
519+gdeclareNamedSumSchema opts proxy s
520+ | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
521+ | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
522+ where
523+ declareSumSchema = gsumToSchema opts proxy s
524+ (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
520525
521− toStringTag schema = mempty
522− & schemaType .~ SwaggerString
523− & schemaEnum ?~ map toJSON (schema ^.. schemaProperties.ifolded.asIndex)
526+ toStringTag schema = mempty
527+ & schemaType .~ SwaggerString
528+ & schemaEnum ?~ map toJSON (schema ^.. schemaProperties.ifolded.asIndex)
524529
525530type AllNullary = All
526531
test/Data/Swagger/SchemaSpec.hs+2−2
@@ -404,8 +404,8 @@ data Unit = Unit deriving (Generic, ToSchema)
404404unitSchemaJSON :: Value
405405unitSchemaJSON = [aesonQQ|
406406{
407− "type": "array",
408− "enum": [[]]
407+ "type": "string",
408+ "enum": ["Unit"]
409409}
410410|]
411411