Skip to content

Add WithAesonOptions: derive ToJSON, FromJSON and ToSchema from one set of options - #125

Open
BrianRS wants to merge 1 commit into
biocad:masterfrom
BrianRS:aeson-options-deriving
Open

BrianRS wants to merge 1 commit into
biocad:masterfrom
BrianRS:aeson-options-deriving

Conversation

@BrianRS

@BrianRS BrianRS commented Nov 12, 2026

Copy link
Copy Markdown

Adds Data.OpenApi.Deriving with WithAesonOptions, a DerivingVia target. It takes Aeson Options from a type-level tag and derives generic ToJSON, FromJSON and ToSchema from them, the schema through fromAesonOptions:

data Unwrapped
instance AesonOptions Unwrapped where
  aesonOptions _ = defaultOptions { unwrapUnaryRecords = True }

data Flags = Flags { flags :: [Bool] }
  deriving Generic
  deriving (ToJSON, FromJSON, ToSchema) via WithAesonOptions Unwrapped Flags

Without it, the options are written twice, once for aeson and once for the schema, and nothing checks that the two copies agree. When they differ, the schema silently describes a different JSON shape from the one on the wire.

The change adds one module and no dependencies. It uses the GToJSON/GToEncoding synonyms, which aeson exports from 1.4 through 2.2, so it needs no CPP.

Tests: Data.OpenApi.DerivingSpec covers unwrapUnaryRecords and constructorTagModifier. It checks the encoding, the decoding, the schema, and validateToJSON against the derived schema. cabal test all (spec and doctests) passes with GHC 9.6, 9.10 and 9.12. There is a changelog entry under Unreleased.

…et of options

A DerivingVia target that takes its Aeson Options from a type-level tag
and builds the generic ToJSON, FromJSON and ToSchema instances from them
(the schema through fromAesonOptions), so the schema cannot drift from
the JSON encoding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant