git.iko.soy/typeable/swagger2
on master help sign in
Add ToSchema instance for Maybe, fix derived Schema type for records
commit83d4aec26f0f53cb74406c082751a2a3b22209bb authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateMon 23 Nov 2015, 16:26 (11 years ago) parente35d033
1 file changed+13−7
src/Data/Swagger/Schema/Internal.hs+13−7
src/Data/Swagger/Schema/Internal.hs+13−7
@@ -45,6 +45,9 @@ instance ToSchema Int where toSchema = toSchemaBoundedIntegral
4545instance ToSchema Double where
4646 toSchema _ = mempty { _schemaType = SchemaNumber }
4747
48+instance ToSchema a => ToSchema (Maybe a) where
49+ toSchema _ = toSchema (Proxy :: Proxy a)
50+
4851toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
4952toSchemaBoundedIntegral _ = mempty
5053 & schemaType .~ SchemaInteger
@@ -66,22 +69,25 @@ instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
6669instance GToSchema f => GToSchema (D1 d (C1 c f)) where
6770 gtoSchema _ = gtoSchema (Proxy :: Proxy f)
6871
72+-- | Optional record fields.
73+instance {-# OVERLAPPING #-} (Selector s, ToSchema c) => GToSchema (S1 s (K1 i (Maybe c))) where
74+ gtoSchema _ schema = schema
75+ & schemaType .~ SchemaObject
76+ & schemaProperties . at fieldName ?~ Inline fieldSchema
77+ where
78+ fieldName = Text.pack (selName (undefined :: S1 s f p))
79+ fieldSchema = toSchema (Proxy :: Proxy c)
80+
6981-- | Record fields.
7082instance {-# OVERLAPPABLE #-} (Selector s, GToSchema f) => GToSchema (S1 s f) where
7183 gtoSchema _ schema = schema
84+ & schemaType .~ SchemaObject
7285 & schemaProperties . at fieldName ?~ Inline fieldSchema
7386 & schemaRequired %~ (fieldName :)
7487 where
7588 fieldName = Text.pack (selName (undefined :: S1 s f p))
7689 fieldSchema = gtoSchema (Proxy :: Proxy f) mempty
7790
78−-- | Optional record fields.
79−instance {-# OVERLAPPING #-} (Selector s, ToSchema c) => GToSchema (S1 s (K1 i (Maybe c))) where
80− gtoSchema _ = schemaProperties . at fieldName ?~ Inline fieldSchema
81− where
82− fieldName = Text.pack (selName (undefined :: S1 s f p))
83− fieldSchema = toSchema (Proxy :: Proxy c)
84−
8591instance ToSchema c => GToSchema (K1 i c) where
8692 gtoSchema _ _ = toSchema (Proxy :: Proxy c)
8793