diff --git a/src/Data/OpenApi/Internal/Schema.hs b/src/Data/OpenApi/Internal/Schema.hs index 212b2a40..43e9e438 100644 --- a/src/Data/OpenApi/Internal/Schema.hs +++ b/src/Data/OpenApi/Internal/Schema.hs @@ -951,9 +951,11 @@ instance {-# OVERLAPPING #-} Constructor c => GToSchema (C1 c U1) where gdeclareNamedSchema = gdeclareNamedSumSchema -- | Single field constructor. -instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s f)) where +instance (Selector s, GToSchema f, GToSchema (S1 s f), GSumToSchema (S1 s f)) => GToSchema (C1 c (S1 s f)) where gdeclareNamedSchema opts _ s | unwrapUnaryRecords opts = fieldSchema + | tagSingleConstructors opts = do + gdeclareNamedSumSchema opts (Proxy :: Proxy (S1 s f)) s | otherwise = case schema ^. items of Just (OpenApiItemsArray [_]) -> fieldSchema @@ -1116,7 +1118,7 @@ instance {-# OVERLAPPABLE #-} (Constructor c, GToSchema f) => GSumToSchema (C1 c tell (All False) lift $ gsumConToSchema opts proxy -instance (Constructor c, Selector s, GToSchema f) => GSumToSchema (C1 c (S1 s f)) where +instance (Constructor c, Selector s, GToSchema f, GSumToSchema (S1 s f)) => GSumToSchema (C1 c (S1 s f)) where gsumToSchema opts proxy = do tell (All False) lift $ gsumConToSchema opts proxy diff --git a/src/Data/OpenApi/SchemaOptions.hs b/src/Data/OpenApi/SchemaOptions.hs index ed95881f..27bd5f37 100644 --- a/src/Data/OpenApi/SchemaOptions.hs +++ b/src/Data/OpenApi/SchemaOptions.hs @@ -24,6 +24,7 @@ data SchemaOptions = SchemaOptions , unwrapUnaryRecords :: Bool -- | Specifies how to encode constructors of a sum datatype. , sumEncoding :: Aeson.SumEncoding + , tagSingleConstructors :: Bool } -- | Default encoding @'SchemaOptions'@. @@ -39,14 +40,7 @@ data SchemaOptions = SchemaOptions -- } -- @ defaultSchemaOptions :: SchemaOptions -defaultSchemaOptions = SchemaOptions - { fieldLabelModifier = id - , constructorTagModifier = id - , datatypeNameModifier = id - , allNullaryToStringTag = True - , unwrapUnaryRecords = False - , sumEncoding = Aeson.defaultTaggedObject - } +defaultSchemaOptions = fromAesonOptions Aeson.defaultOptions -- | Convert 'Aeson.Options' to 'SchemaOptions'. -- @@ -56,21 +50,25 @@ defaultSchemaOptions = SchemaOptions -- * 'constructorTagModifier' -- * 'allNullaryToStringTag' -- * 'unwrapUnaryRecords' +-- * 'sumEncoding' -- -- Note that these fields have no effect on `SchemaOptions`: -- -- * 'Aeson.omitNothingFields' -- * 'Aeson.tagSingleConstructors' +-- * 'Aeson.rejectUnknownFields' -- -- The rest is defined as in 'defaultSchemaOptions'. -- -- @since 2.2.1 -- fromAesonOptions :: Aeson.Options -> SchemaOptions -fromAesonOptions opts = defaultSchemaOptions +fromAesonOptions opts = SchemaOptions { fieldLabelModifier = Aeson.fieldLabelModifier opts , constructorTagModifier = Aeson.constructorTagModifier opts + , datatypeNameModifier = id , allNullaryToStringTag = Aeson.allNullaryToStringTag opts , unwrapUnaryRecords = Aeson.unwrapUnaryRecords opts , sumEncoding = Aeson.sumEncoding opts + , tagSingleConstructors = Aeson.tagSingleConstructors opts }