@@ -189,22 +189,23 @@ gdatatypeSchemaName opts _ = case name of
189189instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
190190 gtoNamedSchema opts _ = unnamed . gtoSchema opts (Proxy :: Proxy f) . gtoSchema opts (Proxy :: Proxy g)
191191
192−-- | Single constructor data type.
193−instance {-# OVERLAPPABLE #-} (Datatype d, GToSchema f) => GToSchema (D1 d (C1 c f)) where
192+instance (Datatype d, GToSchema f) => GToSchema (D1 d f) where
194193 gtoNamedSchema opts _ s = (schemaName, gtoSchema opts (Proxy :: Proxy f) s)
195194 where
196195 schemaName = gdatatypeSchemaName opts (Proxy :: Proxy d)
197196
198−-- | Single field single constructor data type.
199−instance (Datatype d, Selector s, GToSchema f) => GToSchema (D1 d (C1 c (S1 s f))) where
197+instance {-# OVERLAPPABLE #-} GToSchema f => GToSchema (C1 c f) where
198+ gtoNamedSchema opts _ = unnamed . gtoSchema opts (Proxy :: Proxy f)
199+
200+-- | Single field constructor.
201+instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
200202 gtoNamedSchema opts _ s
201− | unwrapUnaryRecords opts = (schemaName, gtoSchema opts (Proxy :: Proxy f) s)
202− | otherwise = (schemaName,) $
203+ | unwrapUnaryRecords opts = unnamed fieldSchema
204+ | otherwise = unnamed $
203205 case schema ^. schemaItems of
204206 Just (SchemaItemsArray [_]) -> fieldSchema
205207 _ -> schema
206208 where
207− schemaName = gdatatypeSchemaName opts (Proxy :: Proxy d)
208209 schema = gtoSchema opts (Proxy :: Proxy (S1 s f)) s
209210 fieldSchema = gtoSchema opts (Proxy :: Proxy f) s
210211
@@ -245,6 +246,34 @@ instance {-# OVERLAPPABLE #-} (Selector s, GToSchema f) => GToSchema (S1 s f) wh
245246instance ToSchema c => GToSchema (K1 i c) where
246247 gtoNamedSchema _ _ _ = toNamedSchema (Proxy :: Proxy c)
247248
249+instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
250+ gtoNamedSchema opts _ = unnamed . gsumToSchema opts (Proxy :: Proxy (f :+: g))
251+
252+class GSumToSchema f where
253+ gsumToSchema :: SchemaOptions -> proxy f -> Schema -> Schema
254+
255+instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
256+ gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) . gsumToSchema opts (Proxy :: Proxy g)
257+
258+gsumConToSchema :: forall c f proxy. Constructor c => Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> Schema
259+gsumConToSchema tagSchemaRef opts _ schema = schema
260+ & schemaType .~ SchemaObject
261+ & schemaProperties . at tag ?~ tagSchemaRef
262+ & schemaMaxProperties ?~ 1
263+ & schemaMinProperties ?~ 1
264+ where
265+ tag = T.pack (conName (Proxy3 :: Proxy3 c f p))
266+
267+instance {-# OVERLAPPABLE #-} (Constructor c, GToSchema f) => GSumToSchema (C1 c f) where
268+ gsumToSchema opts = gsumConToSchema tagSchemaRef opts
269+ where
270+ tagSchemaRef = gtoSchemaRef opts (Proxy :: Proxy (C1 c f))
271+
272+instance (Constructor c, Selector s, GToSchema f) => GSumToSchema (C1 c (S1 s f)) where
273+ gsumToSchema opts = gsumConToSchema tagSchemaRef opts
274+ where
275+ tagSchemaRef = gtoSchemaRef opts (Proxy :: Proxy (C1 c (S1 s f)))
276+
248277data Proxy2 a b = Proxy2
249278
250279data Proxy3 a b c = Proxy3