Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Data/OpenApi/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions src/Data/OpenApi/SchemaOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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'@.
Expand All @@ -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'.
--
Expand All @@ -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
}