Skip to content

fix(codegen): explain unhandled Schema type errors - #2531

Merged
mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
bendrucker:unhandled-schema-type-error
Nov 9, 2026
Merged

mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
bendrucker:unhandled-schema-type-error

Conversation

@bendrucker

@bendrucker bendrucker commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

The primitive-type dispatch printed a Go pointer and left the user to guess. Closes #1977.

before: unhandled Schema type: &[int]
after:  unhandled Schema type "int": not a valid JSON Schema type (expected one of array, boolean, integer, null, number, object, string)

before: unhandled Schema type: &[strng number]
after:  unhandled Schema type ["strng", "number"]: "strng" is not a valid JSON Schema type (expected one of array, boolean, integer, null, number, object, string)

before: unhandled Schema type: &[string number]
after:  unhandled Schema type ["string", "number"]: a list of types requires OpenAPI 3.1 or later, but this document declares OpenAPI 3.0.0

The first is #1976 exactly. examples/client/api.yaml still carries type: int, and diagnosing it took an external linter run.

After #2522 the dispatch handles every JSON Schema type, so reaching the end of it leaves two causes: a name that is no JSON Schema type, or the 3.1-only list form in an earlier document. Both stay errors, and nothing is widened to any. The message reports the type as declared, so it matches what the reader has in their spec. The version suffix drops whenever it wouldn't explain the failure, so a caller that never ran Generate() still gets the requirement without a bogus version.

Schema Position

Component schemas already carried a path. Four positions that reach this error did not:

before: error generating body definitions: error generating request body definition: ...
after:  error generating body definitions: error generating request body definition for PostThing (application/json): ...

before: error generating response definitions: error generating request body definition: ...
after:  error generating response definitions: error generating response body definition for GetThing.200 (application/json): ...

before: error generating response header definition: ...
after:  error generating response header definition for GetThing.200 header "X-Thing": ...

before: error generating type for oneOf: ...
after:  error generating type for oneOf: branch 1: ...

The response wrapper called itself a request body. generateUnion computed a branch index for naming and dropped it on the error path. Branch numbering stays zero-based to match the generated type names (ParamOneOf0, ParamOneOf1) and the /oneOf/1 pointer linters report.

Reusable components.responses generate with no operation, so an operationID.statusCode label rendered as .NotFound. That is reachable with -generate strict-server and no models. It now falls back to the component name.

allOf members still report only their parent. MergeSchemas composes them into one synthetic schema before the failure, so naming a member needs provenance through the merge.

Added tests covering each message shape and each new label.

The primitive-type dispatch reported `unhandled Schema type: &[int]` -- a
Go pointer formatting of kin-openapi's type slice, with no reason attached.
Issue oapi-codegen#1976 hit it with a misspelled `type: int` and had to run vacuum to
find out what was wrong.

The dispatch handles every JSON Schema type, so falling off the end of it
means the `type` is not one the document may carry. That narrows to two
causes, and both are cheap to tell apart: a name that is not a JSON Schema
type, or the 3.1-only list form in an earlier document. The message now
names the declared value verbatim (quoted, so a typo is visible) and which
of the two it is, listing the valid type names for the first and the
document's declared version for the second.

The `type` reported is the declared one, not the "null"-stripped value the
dispatch runs on, so it matches what the reader has in their spec.

Three wrappers above it lacked the context to say which schema failed.
Request and response bodies now name the operation and content type, and
the response body one no longer calls itself a request body; response
headers name the header.
…nestly

Review of the error-message change turned up three gaps in it.

The response body and header wrappers built an "operation.status" label, but
components.responses generate with no operation, so `-generate strict-server`
without `models` reported `for .NotFound` with a stray leading dot. The label
now falls back to the component name alone.

The version hint read the declared version off the spec while the branch
gating it read globalState.is31. SetGlobalStateSpec sets one without the
other, so a 3.1 document could be told "is OpenAPI 3.1 syntax, but this
document declares OpenAPI 3.1.0". The version is now a suffix that is
omitted whenever it would not explain the failure.

generateUnion computed a branch index for naming and dropped it on the error
path, so an inline oneOf/anyOf branch with a bad type reported only the parent
schema. It now names the branch.

Also collapses the three near-identical unknown-name branches into one, and
adds the Precondition note the file's other globalState-dependent functions
carry.
@bendrucker
bendrucker requested a review from a team as a code owner August 19, 2026 02:52
@greptile-apps

greptile-apps Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] This PR appears safe to merge.

Summary

This PR makes unsupported schema-type errors explain the declared value and why generation failed.

  • Schema errors now explain unknown types and OpenAPI version limits.
  • Schema errors now point to the request, response, or union branch.

Reviews (3) · Last reviewed commit: "Update for code drift" · Reviewed by Greptile

Comment thread pkg/codegen/schema.go Resolved
@bendrucker

Copy link
Copy Markdown
Contributor Author

@greptileai please redo the review.

The one comment from the last pass was the zero-based oneOf/anyOf branch label, and you agreed on the thread that zero-based is right here. Nothing changed on the branch since, so the 4/5 is scored against a finding that no longer stands.

Merge upstream/main and adapt the PR to two upstream refactors:

- Union generation moved out of schema.go into union.go, union_v2.go
  and union_v3.go. The PR's copy of the old generateUnion is dropped,
  and its "branch N" error wrapping now lives in generateUnionElement
  (union.go), which both the v2 and v3 merging paths call, so either
  path names the failing oneOf/anyOf branch.
- oapiSchemaToGoType now takes a genContext as its first argument. The
  PR's tests pass newGenContext(nil), matching upstream's tests.

The unhandledSchemaTypeError helper and the operations.go label changes
merged cleanly; globalState.is31 and globalState.spec are still present
upstream.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mromaszewicz
mromaszewicz merged commit 19944d2 into oapi-codegen:main Nov 9, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve error message from unhandled Schema type

2 participants