Skip to content

feat(v3): an inline discriminated variant takes the value it pins - #2579

Merged
mromaszewicz merged 6 commits into
oapi-codegen:mainfrom
mromaszewicz:aggregates/v3-unions-inline-keys
Sep 25, 2026
Merged

mromaszewicz merged 6 commits into
oapi-codegen:mainfrom
mromaszewicz:aggregates/v3-unions-inline-keys

Conversation

@mromaszewicz

@mromaszewicz mromaszewicz commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Step 4 of the schema-merging-behavior: v3 work, the last of four PRs, after #2576, #2577 and #2578. It changes only v3, which is still opt-in and undocumented. v1 and v2 output doesn't change.

  1. a oneOf or anyOf that only adds constraints makes no union (feat(v3): a oneOf or anyOf that only adds constraints makes no union #2576)
  2. a child that is an allOf of its parent isn't a union of its siblings (feat(v3): a child that is an allOf of its parent isn't a union of its siblings #2577)
  3. an allOf of unions is one of each union's variants at once (feat(v3): an allOf of unions is one of each union's variants at once #2578)
  4. an inline discriminated variant takes the value it pins (this PR)

What changes

A discriminator maps its values to schema names: explicitly through its mapping, or implicitly by each $ref's name. An inline variant has no name. But it often says which value it takes anyway:

Pet:
  oneOf:
    - type: object
      properties:
        petType: {type: string, enum: [cat]}
        meow: {type: string}
    - type: object
      properties:
        petType: {const: dog}
        bark: {type: string}
  discriminator:
    propertyName: petType

v3 maps such a variant to the value its discriminator property pins:

Spec v2 v3
inline variants pinning cat and dog, no mapping error: "discriminator: not all schemas were mapped" case "cat" and case "dog" in ValueByDiscriminator(); each From* stamps its value
a pinning inline variant next to an explicit mapping for a $ref error: "ambiguous discriminator.mapping: please replace inlined object with $ref" both variants dispatch and stamp
an inline variant that pins nothing, next to a $ref case "", and its From* writes "petType": "" on the wire no discriminator value for it, see below
3.1 variants pinning enabled: {const: true} / {const: false} with no type inline: the "not all schemas were mapped" error; $refs mapped as 'true'/'false': a string discriminator that stamps "enabled": "true" and can't read {"enabled": true} a boolean discriminator that stamps and reads {"enabled": true}

Which values count

  • A const, or an enum with one value besides null, pins the value. So does the 3.1 nullable spelling enum: [cat, null].
  • The pin is found on any declaration of the property, in the variant or its allOf members. A pinning member after a $ref base counts, as does an allOf member of the property's own schema.
  • Numbers and booleans are spelled as encoding/json writes them, as mapping keys are.
  • When no declaration of the property gives it a type, as with 3.1's const: true, the pinned values give the discriminator its JSON type. A declared type always wins. Pinned values of different JSON types, such as const: true next to const: 1, are an error: no one discriminator reads them all.
  • A $ref variant keeps its explicit or implicit key, even if it also pins a value.

Numbers and stamps

  • An integer or number discriminator is read as a number, not compared as text. ValueByDiscriminator() decodes the property into an int64 or a float64 and switches on the mapping values written as literals of that type, for explicit mapping keys and pinned values alike.
    • A number discriminator's spellings, such as 1, 1.0 and 1e0, are one float64 value, as the variant's own field reads them, so {"version": 1.0} reaches the variant keyed 1.
    • An integer discriminator is read as encoding/json reads an int64. That's exact past 2^53: a mapping key of 9007199254740993 dispatches and stamps as written. 1.0 isn't an int64, so it's an error, just as it is for the variant's own int field.
    • At generation, every mapping value of such a discriminator must be a JSON number of its kind. An integer's may be written as 1.0 or 1e3 when it's exact. Two values that are the same number but lead to different variants are an error.
    • Discriminator() still returns the text as written.
  • v2 stamps no variant when an explicit mapping gives one variant two keys (Support unions with multiple mappings pointing to a single underlying type #2071). v3 stamps each variant that exactly one value leads to. The aliased variant still gets no stamp, since picking one of its values would be arbitrary.
  • kin-openapi reads numbers as float64, so a pinned integer of magnitude 2^53 or more may already have been rounded, and From* would write a value that differs from the spec's. That is an error that suggests a $ref variant with an explicit mapping key.

Variants that pin nothing

An inline variant that pins no value has no discriminator value, with or without a mapping. ValueByDiscriminator() has no case for it and its From* stamps nothing, but its As*, From* and Merge* work as for any variant, so the caller converts to it directly. The stamps of the variants that do have a value keep working.

v2 errors on such a variant next to a mapping. Without one it maps the variant to the empty string, which several such variants can't share.

Errors

  • A pinned value that is already an explicit mapping key, another variant's value, or a $ref's implicit name, with the path of the inline schema.
  • A pinned value holding a quote, backtick, backslash or control character. The value is written into Go and JSON string literals, and the spec validator checks mapping keys, but not enum or const values.
  • Pinned values of different JSON types, and pinned integers of magnitude 2^53 or more, as above.

Tests

  • pkg/codegen/merge_schemas_v3_test.go covers:
    • TestInlineDiscriminatorValue: which values count, including later allOf members, the property's own allOf, nullable enums, and number spelling.
    • TestInlineDiscriminatedVariantsV3: generated code with and without a mapping, integer pins, and typeless and typed 3.1 pins.
    • TestInlineUnpinnedVariantsV3: variants that pin nothing.
    • TestInlineDiscriminatedVariantErrorsV3: the collisions and the rejected characters.
  • TestDiscriminatorStampsPerVariantV3 covers stamps next to aliased mapping keys; TestNumberLiteral and TestNumericMappingKeyErrorsV3 cover how numeric mapping values are read.
  • The aggregate matrix gains oneof_discriminator_inline (two pinned inline variants and one that pins nothing) and oneof_discriminator_number (a number discriminator with pinned values) and oneof_discriminator_int64 (explicit keys including 2^53+1). v1 and v2 reject the first two with their old error. The v3 cells round-trip their samples at every position, also on Go 1.25. Hand-written tests check dispatch, stamping, that the unpinned variant is left to the caller, that 1, 1.0, 1e0, 2.50 and 25e-1 all dispatch, and that 2^53+1 dispatches and stamps exactly.

Fixtures

make generate changes no committed file. Regenerating every fixture under v3 changes one type beyond #2578: in internal/test/aggregates/oneof's discriminator fixture, FromConfigSsh now stamps ssh_server, next to ConfigHttp, whose three aliased keys still give it no stamp.

🤖 Generated with Claude Code

A discriminator maps its values to schema names: explicitly through its
mapping, or implicitly by each $ref's name. An inline variant has no
name, so v2 fails on a discriminated oneOf of inline variants ("not all
schemas were mapped"), or, next to a mapping, asks for a $ref instead.

An inline variant often says its value anyway:

    oneOf:
      - type: object
        properties:
          petType: {type: string, enum: [cat]}
          meow: {type: string}
      - type: object
        properties:
          petType: {const: dog}
          bark: {type: string}
    discriminator:
      propertyName: petType

schema-merging-behavior v3 maps such a variant to the value that a
const, or an enum of one value besides null, on its discriminator
property pins: on any declaration of the property, in the variant or
its allOf members (so a pinning member after a $ref base counts), or in
an allOf member of the property's own schema. Discriminator(),
ValueByDiscriminator() and the value From* stamps then work as for
mapped variants. A number or boolean value is spelled as encoding/json
writes it, as mapping keys are; and when the property declares no type,
as with 3.1's `const: true`, the pinned values give the discriminator
its JSON type. It is an error when that value is already an explicit
mapping key, another variant's value, or a $ref's implicit name, or when
it holds a quote, backtick, backslash or control character, which the
generated string literals can't hold (the validator checks mapping keys,
but not enum or const values). An inline variant that pins no value has
no discriminator value, with or without a mapping: ValueByDiscriminator
doesn't lead to it and its From* stamps nothing, while its As* and
From* work as for any variant. v2 errors on it next to a mapping and,
without one, maps it to the empty string, which several such variants
can't share.

The matrix gains oneof_discriminator_inline, two pinned variants and
one unpinned, which v1 and v2 reject with their old error, with a v3
test of dispatch and stamping. Generating the committed fixtures with
v3 changes nothing beyond oapi-codegen#2578.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 25, 2026 03:42
@greptile-apps

greptile-apps Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Adds test cases for discriminated union handling in code generation.

The PR appears safe to merge; the remaining negative-zero change does not break numeric dispatch.

Summary

This PR adds v3 support for discriminator values pinned by inline variants and numeric discriminator dispatch.

  • Adds matrix and unit coverage for pinned and unpinned variants, numeric spellings, and exact int64 mappings.
  • Normalizes negative zero so equivalent numeric mapping keys cannot generate duplicate switch cases.

Reviews (6) · Last reviewed commit: "fix(v3): -0 of a number discriminator is..."

Comment thread pkg/codegen/union_v3.go Resolved
Comment thread pkg/codegen/union_v3.go Resolved
Comment thread pkg/codegen/union_v3.go Resolved
Comment thread pkg/codegen/union_v3.go Resolved
Review fixes for inline discriminated variants:

- JSON can write one number several ways, such as 1, 1.0 and 1e0, but
  ValueByDiscriminator compared the text, so `{"version": 1.0}` didn't
  reach the variant keyed "1". For an integer or number discriminator,
  v3 now compares the value as encoding/json writes the number, and
  cases spelled differently for one variant are one. Discriminator()
  still returns the text as written. This holds for explicit numeric
  mapping keys too.
- Variants whose typeless pins have different JSON types, such as
  `const: true` and `const: 1`, fell back to a string discriminator
  that stamps "true" and can't read `true`. That is now an error.
- An explicit mapping with two keys for one variant turned off the
  stamps of every variant, as v2 does (oapi-codegen#2071), so an inline variant
  pinning `cat` next to them stamped nothing. v3 now stamps each
  variant exactly one value leads to; the aliased one still gets none.
  Under v3, internal/test/aggregates/oneof's ConfigSsh now stamps
  ssh_server next to the aliased ConfigHttp.
- kin-openapi reads numbers as float64, so a pinned integer of
  magnitude 2^53 or more may already be rounded, and From* would write
  a different value than the spec's. That is now an error that suggests
  a $ref variant with an explicit mapping key.

The matrix gains oneof_discriminator_number, a number discriminator
with pinned inline variants, which v1 and v2 reject with their old
error, with a v3 test of dispatch under several spellings.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread pkg/codegen/schema.go Resolved
Comment thread pkg/codegen/templates/union.tmpl Resolved
A numeric discriminator compares values as encoding/json writes the
number, so mapping keys like 1 and 1.0 are one value. When they lead to
different variants, the generated ValueByDiscriminator had two
`case "1":` labels and didn't compile. That is now a generation error
naming both keys; spellings of one number for the same variant are
still one case.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread pkg/codegen/union_v3.go Resolved
With 1 and 1.0 both mapped to one variant of a numeric discriminator,
ValueByDiscriminator has one case for them, but the stamp counted two
keys and gave the variant none, so From* could leave the discriminator
unset. A number's spellings now count as one value for the stamp, which
writes it as encoding/json does. v1 and v2, which don't compare numbers,
are unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread pkg/codegen/schema.go Resolved
An integer or number discriminator was compared as text, normalized by
rounding through float64: 9007199254740993, which an int64 holds and an
explicit mapping key can spell exactly, became 9007199254740992 in both
the switch and the stamp, breaking the workaround the error for large
inline pins suggests.

ValueByDiscriminator now decodes the discriminator property into the
Go type of its JSON kind, int64 for an integer and float64 for a
number, and switches on the mapping values written as literals of that
type. A number's spellings, like 1, 1.0 and 1e0, are one float64
value, as the variant's own field reads them; an integer is read as
encoding/json reads it into an int64, so 1.0 isn't one and is an error.
Stamps write the same literals.

At generation, each mapping value of such a discriminator must be a
JSON number of its kind: an integer's may be written with a zero
fraction or an exponent, like 1.0 or 1e3, when it's exact; 2.5, a value
beyond an int64 or a float64, or text that isn't a JSON number is an
error. Two values that are one number and lead to different variants
are still an error. String and boolean discriminators, and v1 and v2,
are unchanged.

The matrix gains oneof_discriminator_int64, whose mapping includes
2^53+1, with a v3 test that it dispatches and stamps exactly.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread pkg/codegen/templates/union.tmpl Resolved
Comment thread pkg/codegen/union_v3.go Resolved
A number discriminator's mapping values -0 and 0 became the literals -0
and 0, so the check for one number leading to two variants missed
them, and the generated float64 switch had two equal cases (Go
constants have no negative zero) and didn't compile. Zero is now
written as 0: -0 and 0 leading to different variants is the "same
number" error, and leading to one variant is one case. Incoming -0
already matched, since -0.0 == 0.0.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mromaszewicz
mromaszewicz merged commit 2b1706a into oapi-codegen:main Sep 25, 2026
15 checks passed
@mromaszewicz
mromaszewicz deleted the aggregates/v3-unions-inline-keys branch September 26, 2026 00:46
@mromaszewicz mromaszewicz added the enhancement New feature or request label Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant