FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(codegen): explain unhandled Schema type errors by bendrucker · Pull Request #2531 · oapi-codegen/oapi-codegen · GitHub

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

Open
bendrucker wants to merge 3 commits into
oapi-codegen:mainfrom
bendrucker:unhandled-schema-type-error
Open

fix(codegen): explain unhandled Schema type errors#2531
bendrucker wants to merge 3 commits into
oapi-codegen:mainfrom
bendrucker:unhandled-schema-type-error

Conversation

bendrucker commented Aug 19, 2026
edited
Loading

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 requested a review from a team as a code owner August 19, 2026 02:52

greptile-apps Bot commented Aug 19, 2026
edited
Loading

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR improves invalid-schema diagnostics without accepting previously invalid schemas.

  • Explains unknown JSON Schema type names and OpenAPI-version-incompatible type lists.
  • Adds request, response, header, and union-branch context to generation errors.
  • Covers diagnostic variants and component-response labeling with unit tests.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; the previously reported branch-numbering issue is invalid because zero-based numbering matches generated branch names and existing JSON Pointer-style indexing.

Important Files Changed

Filename Overview
pkg/codegen/schema.go Adds actionable schema-type diagnostics and zero-based union branch context consistent with existing naming and validation conventions.
pkg/codegen/operations.go Adds request and response location details to schema-generation errors, including component-response fallback labels.
pkg/codegen/schema_test.go Adds focused coverage for invalid type names, OpenAPI version hints, component-response labels, and union branch indices.

Reviews (2): Last reviewed commit: "fix(codegen): name the failing response,..." | Re-trigger Greptile

Comment thread pkg/codegen/schema.go

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve error message from unhandled Schema type

1 participant


Back | FazBrowse Home | New Git URL