Repository navigation
feat(v3): an inline discriminated variant takes the value it pins - #2579
Merged
mromaszewicz merged 6 commits intoSep 25, 2026
Merged
mromaszewicz merged 6 commits into
mromaszewicz merged 6 commits into
Conversation
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]>
Contributor
|
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]>
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]>
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]>
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]>
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Step 4 of the
schema-merging-behavior: v3work, 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.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)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)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)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:v3 maps such a variant to the value its discriminator property pins:
catanddog, no mappingcase "cat"andcase "dog"inValueByDiscriminator(); eachFrom*stamps its valuemappingfor a$ref$refcase "", and itsFrom*writes"petType": ""on the wireenabled: {const: true}/{const: false}with notype$refs mapped as'true'/'false': a string discriminator that stamps"enabled": "true"and can't read{"enabled": true}{"enabled": true}Which values count
const, or anenumwith one value besidesnull, pins the value. So does the 3.1 nullable spellingenum: [cat, null].$refbase counts, as does an allOf member of the property's own schema.const: true, the pinned values give the discriminator its JSON type. A declared type always wins. Pinned values of different JSON types, such asconst: truenext toconst: 1, are an error: no one discriminator reads them all.$refvariant keeps its explicit or implicit key, even if it also pins a value.Numbers and stamps
ValueByDiscriminator()decodes the property into anint64or afloat64and switches on the mapping values written as literals of that type, for explicit mapping keys and pinned values alike.1,1.0and1e0, are onefloat64value, as the variant's own field reads them, so{"version": 1.0}reaches the variant keyed1.int64. That's exact past 2^53: a mapping key of9007199254740993dispatches and stamps as written.1.0isn't an int64, so it's an error, just as it is for the variant's ownintfield.1.0or1e3when 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.From*would write a value that differs from the spec's. That is an error that suggests a$refvariant 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 itsFrom*stamps nothing, but itsAs*,From*andMerge*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
$ref's implicit name, with the path of the inline schema.enumorconstvalues.Tests
pkg/codegen/merge_schemas_v3_test.gocovers: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.TestDiscriminatorStampsPerVariantV3covers stamps next to aliased mapping keys;TestNumberLiteralandTestNumericMappingKeyErrorsV3cover how numeric mapping values are read.oneof_discriminator_inline(two pinned inline variants and one that pins nothing) andoneof_discriminator_number(a number discriminator with pinned values) andoneof_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, that1,1.0,1e0,2.50and25e-1all dispatch, and that 2^53+1 dispatches and stamps exactly.Fixtures
make generatechanges no committed file. Regenerating every fixture under v3 changes one type beyond #2578: ininternal/test/aggregates/oneof's discriminator fixture,FromConfigSshnow stampsssh_server, next toConfigHttp, whose three aliased keys still give it no stamp.🤖 Generated with Claude Code