Skip to content

Validate implicit discriminator mapping keys and the contents of whole-document $refs - #2648

Merged
mromaszewicz merged 6 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/validate-inlined-refs
Oct 31, 2026
Merged

mromaszewicz merged 6 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/validate-inlined-refs

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Summary

ValidateSpec left 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.

  • Implicit discriminator mapping keys. A $ref variant of a discriminated oneOf/anyOf that 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's ValueByDiscriminator switch 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. walkSchema now derives the implicit key the way the union generators do and checks it as text.
  • Whole-document $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: IsGoTypeReference is 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 on main and release/v2.8.x, so the same patch applies to both.

Compatibility

No generated file changes: make generate produces 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.go cover 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 through Generate. All of them fail against the previous validator. go test ./..., make generate and golangci-lint pass locally.

🤖 Generated with Claude Code

…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]>
@greptile-apps

greptile-apps Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issues were found.

Summary

The PR checks implicit discriminator keys and values copied from whole-document references before generating Go.

  • Whole-document refs are checked before their contents become Go code.
  • Discriminator unions reject unsafe keys derived from schema refs.

Reviews (5) · Last reviewed commit: "Keep ValidateSpec independent of the opt..." · Reviewed by Greptile

Comment thread pkg/codegen/validate_spec.go Resolved
Comment thread pkg/codegen/validate_spec.go Resolved
mromaszewicz and others added 2 commits October 31, 2026 16:00
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]>
Comment thread pkg/codegen/validate_spec.go Resolved
Comment thread pkg/codegen/validate_spec.go Resolved
…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]>
Comment thread pkg/codegen/validate_spec.go Resolved
mromaszewicz and others added 2 commits October 31, 2026 16:19
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]>
@mromaszewicz
mromaszewicz merged commit ace1813 into oapi-codegen:main Oct 31, 2026
13 checks passed
@mromaszewicz mromaszewicz added the bug Something isn't working label Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant