Skip to content

Support unions with multiple mappings pointing to a single underlying type - #2071

Merged
mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
tobio:multi-mapping
Feb 16, 2026
Merged

mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
tobio:multi-mapping

Conversation

@tobio

@tobio tobio commented Sep 2, 2025

Copy link
Copy Markdown
Contributor

Fixes #1530
Fixes #1664

This PR:

  • Correctly generates the ValueByDiscriminator function when multiple discriminator values map to a single type
  • Prevents From*/Merge* functions from setting an arbitrary discriminator in the case where there isn't a 1:1 mapping between type and discriminator value

@tobio
tobio requested a review from a team as a code owner September 2, 2025 12:57
@kusari-inspector

Copy link
Copy Markdown

Kusari Inspector

Kusari Analysis Results:

Proceed with these changes

✅ No Flagged Issues Detected
All values appear to be within acceptable risk parameters.

No pinned version dependency changes, code issues or exposed secrets detected!

@kusari-inspector rerun - Trigger a re-analysis of this PR
@kusari-inspector feedback [your message] - Send feedback to our AI and team
See Kusari's documentation for setup and configuration.
Commit: 71958af, performed at: 2025-09-02T12:57:33Z

Found this helpful? Give it a 👍 or 👎 reaction!

@tobio

tobio commented Sep 23, 2025

Copy link
Copy Markdown
Contributor Author

@jamietanna are you able to take a look at this one?

@tobio

tobio commented Feb 16, 2026

Copy link
Copy Markdown
Contributor Author

@mromaszewicz are you able to take a look at this?

@mromaszewicz

Copy link
Copy Markdown
Member

This looks good, I had to think about it for a while, since this code path is so fragile. Thanks for doing this!

@mromaszewicz mromaszewicz added this to the v2.6.0 milestone Feb 16, 2026
@mromaszewicz
mromaszewicz merged commit b658fcf into oapi-codegen:main Feb 16, 2026
26 checks passed
@jamietanna jamietanna added the enhancement New feature or request label Feb 27, 2026
mromaszewicz added a commit to mromaszewicz/oapi-codegen that referenced this pull request Sep 25, 2026
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]>
mromaszewicz added a commit that referenced this pull request Sep 25, 2026
)

* feat(v3): an inline discriminated variant takes the value it pins

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 #2578.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* fix(v3): numeric discriminators, per-variant stamps and inexact pins

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 (#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]>

* fix(v3): reject one discriminator number leading to two variants

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]>

* fix(v3): stamp a variant whose numeric keys spell one value

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]>

* refactor(v3): read a numeric discriminator as a number, not as text

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]>

* fix(v3): -0 of a number discriminator is 0

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]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.

Polymorphism & Discriminator Use discriminator with several mappings for one schema

3 participants