@@ -157,11 +157,6 @@ toSchemaBoundedIntegral _ = mempty
157 & schemaMinimum ?~ fromInteger (toInteger (minBound :: a))
157 & schemaMinimum ?~ fromInteger (toInteger (minBound :: a))
158 & schemaMaximum ?~ fromInteger (toInteger (maxBound :: a))
158 & schemaMaximum ?~ fromInteger (toInteger (maxBound :: a))
160−toSchemaBoundedEnum :: forall a proxy. (ToJSON a, Bounded a, Enum a) => proxy a -> Schema
161−toSchemaBoundedEnum _ = mempty
162− & schemaType .~ SchemaString
163− & schemaEnum ?~ map toJSON [minBound..maxBound :: a]
165genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
160genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
166 ( Bounded a, Integral a
161 ( Bounded a, Integral a
167 , Generic a, Rep a ~ D1 d f, Datatype d)
162 , Generic a, Rep a ~ D1 d f, Datatype d)
@@ -169,13 +164,6 @@ genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
169genericToNamedSchemaBoundedIntegral opts proxy
164genericToNamedSchemaBoundedIntegral opts proxy
170 = (gdatatypeSchemaName opts (Proxy :: Proxy d), toSchemaBoundedIntegral proxy)
165 = (gdatatypeSchemaName opts (Proxy :: Proxy d), toSchemaBoundedIntegral proxy)
172−genericToNamedSchemaBoundedEnum :: forall a d f proxy.
173− ( ToJSON a, Bounded a, Enum a
174− , Generic a, Rep a ~ D1 d f, Datatype d)
175− => SchemaOptions -> proxy a -> NamedSchema
176−genericToNamedSchemaBoundedEnum opts proxy
177− = (gdatatypeSchemaName opts (Proxy :: Proxy d), toSchemaBoundedEnum proxy)
179genericToSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Schema
167genericToSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Schema
180genericToSchema opts = snd . genericToNamedSchema opts
168genericToSchema opts = snd . genericToNamedSchema opts
@@ -258,30 +246,51 @@ instance ToSchema c => GToSchema (K1 i c) where
258 gtoNamedSchema _ _ _ = toNamedSchema (Proxy :: Proxy c)
246 gtoNamedSchema _ _ _ = toNamedSchema (Proxy :: Proxy c)
260instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
248instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
249+ gtoNamedSchema opts _ s
250+ | allNullary = unnamed (toStringTag sumSchema)
251+ | otherwise = unnamed sumSchema
261− gtoNamedSchema opts _ = unnamed . gsumToSchema opts (Proxy :: Proxy (f :+: g))
253+ (All allNullary, sumSchema) = gsumToSchema opts (Proxy :: Proxy (f :+: g)) s
255+ toStringTag schema = mempty
256+ & schemaType .~ SchemaString
257+ & schemaEnum ?~ map toJSON (schema ^.. schemaProperties.ifolded.asIndex)
259+type AllNullary = All
263class GSumToSchema f where
261class GSumToSchema f where
264− gsumToSchema :: SchemaOptions -> proxy f -> Schema -> Schema
262+ gsumToSchema :: SchemaOptions -> proxy f -> Schema -> (AllNullary, Schema)
266instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
264instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
267− gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) . gsumToSchema opts (Proxy :: Proxy g)
265+ gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) <.> gsumToSchema opts (Proxy :: Proxy g)
267+ (f <.> g) s = (a <> b, s'')
272+gsumConToSchema :: forall c f proxy. Constructor c =>
269−gsumConToSchema :: forall c f proxy. Constructor c => Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> Schema
273+ Bool -> Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> (AllNullary, Schema)
270−gsumConToSchema tagSchemaRef opts _ schema = schema
274+gsumConToSchema isNullary tagSchemaRef opts _ schema = (All isNullary, schema
271 & schemaType .~ SchemaObject
275 & schemaType .~ SchemaObject
272 & schemaProperties . at tag ?~ tagSchemaRef
276 & schemaProperties . at tag ?~ tagSchemaRef
273 & schemaMaxProperties ?~ 1
277 & schemaMaxProperties ?~ 1
274− & schemaMinProperties ?~ 1
278+ & schemaMinProperties ?~ 1)
276 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
280 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
278instance {-# OVERLAPPABLE #-} (Constructor c, GToSchema f) => GSumToSchema (C1 c f) where
282instance {-# OVERLAPPABLE #-} (Constructor c, GToSchema f) => GSumToSchema (C1 c f) where
279− gsumToSchema opts = gsumConToSchema tagSchemaRef opts
283+ gsumToSchema opts = gsumConToSchema False tagSchemaRef opts
281 tagSchemaRef = gtoSchemaRef opts (Proxy :: Proxy (C1 c f))
285 tagSchemaRef = gtoSchemaRef opts (Proxy :: Proxy (C1 c f))
287+instance Constructor c => GSumToSchema (C1 c U1) where
288+ gsumToSchema opts = gsumConToSchema True tagSchemaRef opts
290+ tagSchemaRef = gtoSchemaRef opts (Proxy :: Proxy (C1 c U1))
283instance (Constructor c, Selector s, GToSchema f) => GSumToSchema (C1 c (S1 s f)) where
292instance (Constructor c, Selector s, GToSchema f) => GSumToSchema (C1 c (S1 s f)) where
284− gsumToSchema opts = gsumConToSchema tagSchemaRef opts
293+ gsumToSchema opts = gsumConToSchema False tagSchemaRef opts
286 tagSchemaRef = gtoSchemaRef opts (Proxy :: Proxy (C1 c (S1 s f)))
295 tagSchemaRef = gtoSchemaRef opts (Proxy :: Proxy (C1 c (S1 s f)))