git.iko.soy/typeable/swagger2
on master help sign in
Add support for unit types (e.g. ())
commit3e757208d9f8dc3f57ce0909a93ddb61c9fc054e authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateWed 25 Nov 2015, 13:37 (11 years ago) parentb4f1e60
2 files changed+24
src/Data/Swagger/Schema/Internal.hs+9 test/Data/Swagger/SchemaSpec.hs+15
src/Data/Swagger/Schema/Internal.hs+9
@@ -94,6 +94,7 @@ instance ToSchema Float where
9494instance ToSchema a => ToSchema (Maybe a) where
9595 toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
9696
97+instance ToSchema ()
9798instance (ToSchema a, ToSchema b) => ToSchema (a, b)
9899instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
99100instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d) => ToSchema (a, b, c, d)
@@ -188,6 +189,14 @@ gdatatypeSchemaName opts _ = case name of
188189 where
189190 name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
190191
192+nullarySchema :: Schema
193+nullarySchema = mempty
194+ & schemaType .~ SchemaArray
195+ & schemaEnum ?~ [ toJSON () ]
196+
197+instance GToSchema U1 where
198+ gtoNamedSchema _ _ _ = unnamed nullarySchema
199+
191200instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
192201 gtoNamedSchema opts _ = unnamed . gtoSchema opts (Proxy :: Proxy f) . gtoSchema opts (Proxy :: Proxy g)
193202
test/Data/Swagger/SchemaSpec.hs+15
@@ -29,6 +29,7 @@ checkSchemaName name proxy =
2929spec :: Spec
3030spec = do
3131 describe "Generic ToSchema" $ do
32+ context "Unit" $ checkToSchema (Proxy :: Proxy Unit) unitSchemaJSON
3233 context "Person" $ checkToSchema (Proxy :: Proxy Person) personSchemaJSON
3334 context "ISPair" $ checkToSchema (Proxy :: Proxy ISPair) ispairSchemaJSON
3435 context "Point (fieldLabelModifier)" $ checkToSchema (Proxy :: Proxy Point) pointSchemaJSON
@@ -324,3 +325,17 @@ statusSchemaJSON = [aesonQQ|
324325}
325326|]
326327
328+-- ========================================================================
329+-- Unit type
330+-- ========================================================================
331+
332+data Unit = Unit deriving (Generic, ToSchema)
333+
334+unitSchemaJSON :: Value
335+unitSchemaJSON = [aesonQQ|
336+{
337+ "type": "array",
338+ "enum": [[]]
339+}
340+|]
341+