From 949d324e57e1a3ebc37f5116baf12e242569f928 Mon Sep 17 00:00:00 2001 From: Uladzimir Stsepchanka Date: Sat, 6 May 2023 10:22:53 +0300 Subject: [PATCH 1/3] Derive defaultSchemaOptions from Aeson.defaultOptions --- src/Data/OpenApi/SchemaOptions.hs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Data/OpenApi/SchemaOptions.hs b/src/Data/OpenApi/SchemaOptions.hs index ed95881f..e7de05ff 100644 --- a/src/Data/OpenApi/SchemaOptions.hs +++ b/src/Data/OpenApi/SchemaOptions.hs @@ -39,14 +39,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,20 +49,23 @@ 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 From 274d368f22cd8a44cd130c62fa626b26ab54fc07 Mon Sep 17 00:00:00 2001 From: Junaid Rasheed Date: Fri, 20 Jun 2025 17:29:37 +0100 Subject: [PATCH 2/3] fix: try minimal --- src/Data/OpenApi/Internal/Schema.hs | 3 +++ src/Data/OpenApi/SchemaOptions.hs | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/Data/OpenApi/Internal/Schema.hs b/src/Data/OpenApi/Internal/Schema.hs index 212b2a40..d2442431 100644 --- a/src/Data/OpenApi/Internal/Schema.hs +++ b/src/Data/OpenApi/Internal/Schema.hs @@ -954,6 +954,9 @@ instance {-# OVERLAPPING #-} Constructor c => GToSchema (C1 c U1) where instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s f)) where gdeclareNamedSchema opts _ s | unwrapUnaryRecords opts = fieldSchema + | tagSingleConstructors opts = do + NamedSchema _ schema' <- recordSchema + return (unnamed schema') | otherwise = case schema ^. items of Just (OpenApiItemsArray [_]) -> fieldSchema diff --git a/src/Data/OpenApi/SchemaOptions.hs b/src/Data/OpenApi/SchemaOptions.hs index e7de05ff..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'@. @@ -69,4 +70,5 @@ fromAesonOptions opts = SchemaOptions , allNullaryToStringTag = Aeson.allNullaryToStringTag opts , unwrapUnaryRecords = Aeson.unwrapUnaryRecords opts , sumEncoding = Aeson.sumEncoding opts + , tagSingleConstructors = Aeson.tagSingleConstructors opts } From 568e57d5a94f59b990364983913305799128dcaa Mon Sep 17 00:00:00 2001 From: Junaid Rasheed Date: Fri, 20 Jun 2025 18:04:34 +0100 Subject: [PATCH 3/3] wip try fix --- src/Data/OpenApi/Internal/Schema.hs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Data/OpenApi/Internal/Schema.hs b/src/Data/OpenApi/Internal/Schema.hs index d2442431..43e9e438 100644 --- a/src/Data/OpenApi/Internal/Schema.hs +++ b/src/Data/OpenApi/Internal/Schema.hs @@ -951,12 +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 - NamedSchema _ schema' <- recordSchema - return (unnamed schema') + gdeclareNamedSumSchema opts (Proxy :: Proxy (S1 s f)) s | otherwise = case schema ^. items of Just (OpenApiItemsArray [_]) -> fieldSchema @@ -1119,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