Repository navigation
Validate implicit discriminator mapping keys and the contents of whole-document $refs - #2648
Merged
mromaszewicz merged 6 commits intoOct 31, 2026
Merged
Conversation
…e-document $refs ValidateSpec left two spec values unchecked on their way into generated Go source. Both are now rejected, with a message pointing at the offending part of the spec, the way the other checks do. - A $ref variant of a discriminated oneOf/anyOf that the mapping does not name is mapped implicitly, under the last segment of its ref (the schema name). That key is written into the generated union's switch like an explicit mapping key, but only explicit keys were validated, and a schema name is allowed to contain quotes since it otherwise only reaches a doc comment and a sanitized identifier. A ref into another document also names a schema the walk never sees. walkSchema now derives the implicit key the way the union generators do, for every $ref variant the mapping does not name, and checks it as text. - A whole-document $ref (one with no fragment, such as `./other.yaml`) was skipped like every other ref, on the assumption that external content is never copied into the output. It is: IsGoTypeReference is false for such a ref, so code generation inlines the referenced document as if it were written in place, and its property names, parameter names, header names and so on reach struct tags and string literals unchecked. The walk now descends into a whole-document ref's resolved value for schemas, parameters, request bodies, responses, headers and callbacks, walking each document once so documents that refer to each other do not loop. Refs with a fragment are still not followed. No generated file changes: `make generate` produces no diff on this branch, so every valid document renders as before. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Contributor
[Medium impact] The PR appears safe to merge; no new blocking issues were found. SummaryThe PR checks implicit discriminator keys and values copied from whole-document references before generating Go.
Reviews (5) · Last reviewed commit: "Keep ValidateSpec independent of the opt..." · Reviewed by Greptile |
Review follow-ups: - A backslash starts an escape sequence inside the `case "<key>":` label and in the JSON the From* helpers stamp, so `Dog\Name` produced Go that did not compile. checkText does not cover it (a backslash is fine in a struct tag or a quoted literal), so discriminator mapping keys, explicit and implicit, now go through checkDiscriminatorValue, which also rejects a backslash, as the union generators already do for the value an inline variant pins. - A null-typed branch of a discriminated union is a nullability marker that both union generators skip before mapping, so its name never becomes a key. The implicit-key check now skips it too, instead of rejecting a document that generated working code. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ype on inlined documents Review follow-ups: - The allOf merge concatenates the members' oneOf and anyOf lists, transitively through nested allOf and through $ref members, and lets the one member that declares a discriminator govern the result. A union split across members, variants in one and the discriminator in another, so reached the union generator with implicit mapping keys the validator never saw, since it only looked at a schema whose oneOf and discriminator sit together. walkSchema now gathers the discriminators and variants of the whole composition the way the merge does (collectUnionView) and checks the implicit key of every $ref variant a discriminator governs. - An x-go-type next to a whole-document $ref, or at the root of the document it points at, replaces the document: generateGoSchema returns the override before it reads any of the document's contents. Walking the document anyway rejected property names that never reach the generated code, in documents that generated fine before. The walk now stops at the override, still checking the document root's own extensions, the override among them. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…e for allOf unions Review follow-ups: - The loader hands every whole-document $ref to the same document the same value. The override check ran after descend had marked that value as walked, so a ref with x-go-type, walked first, hid the document from a later ref without one, which inlines it. The override is now decided before the visited set is consulted. - The v1 merge (`old-merge-schemas: true`) generates each allOf member on its own, so a union split across members never gets a discriminator and the merged view rejected a document that generates fine under it. The validator now reads the merge behavior of the current run, as the generators do, and only combines the members when the merge does. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Generate leaves its options in globalState, which ValidateSpec now reads for the merge behavior. The v1 run at the end of the test leaked old-merge-schemas into tests that ran after it, so a focused run of this test together with TestValidateSpec_ImplicitDiscriminatorMappingKey failed. The options are saved and restored around the test. Co-Authored-By: Claude Fable 5.1 <[email protected]>
ValidateSpec read the merge behavior from globalState, so after a run with old-merge-schemas the same document validated differently on its own. The public function now always assumes the default merge, and Generate validates through validateSpec with the behavior its options select. The v1 test cases use validateSpec directly, and the end-to-end test no longer needs to restore the run options. Co-Authored-By: Claude Fable 5.1 <[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.
Summary
ValidateSpecleft two spec values unchecked on their way into the generated Go source. Both now fail validation with a message that points at the offending part of the spec, like the existing checks.$refvariant of a discriminatedoneOf/anyOfthat the mapping does not name is mapped under the last segment of its ref, the schema name, and that key is emitted into the union'sValueByDiscriminatorswitch exactly like an explicit mapping key. Only explicit keys were validated, while a schema name may contain quotes (it otherwise reaches only a doc comment and a sanitized identifier), and a ref into another document names a schema the walk never sees at all.walkSchemanow derives the implicit key the way the union generators do and checks it as text.$refs. A ref with no fragment ($ref: ./other.yaml) was skipped like every other ref, on the assumption that external content is never copied into the output. For this kind of ref that is false:IsGoTypeReferenceis false for it, so code generation inlines the referenced document as if it were written in place, and its property names, parameter names, header names and so on reach struct tags and string literals unchecked. The walk now descends into such a ref's resolved value for schemas, parameters, request bodies, responses, headers and callbacks, visiting each document once so documents that refer to each other do not loop. Refs with a fragment are still not followed.The change is confined to
pkg/codegen/validate_spec.go, which is byte-identical onmainandrelease/v2.8.x, so the same patch applies to both.Compatibility
No generated file changes:
make generateproduces no diff, so every valid document renders byte-for-byte as before. The new checks only reject documents that already produced code that did not compile, or code the document author had no business producing.Testing
New unit tests in
validate_spec_test.gocover the implicit key (local ref, external ref,anyOf, explicit mapping exempting the ref, no discriminator) and whole-document refs (schema, parameter, response, fragment refs still skipped, mutual references terminating), plus an end-to-end test throughGenerate. All of them fail against the previous validator.go test ./...,make generateandgolangci-lintpass locally.🤖 Generated with Claude Code