Repository navigation
fix(jsonschema): infer "boolean" type for boolean default and enum values - #1726
Merged
Merged
Conversation
…lues
inferJSONType returned "bool" for jx.Bool, but the type table in
parseSchema is keyed with the JSON Schema names object/array/string/
integer/number/boolean/null. With infer_types enabled, a schema whose
type is inferred from a boolean default or enum therefore always failed:
parameters:
- name: verbose
in: query
schema:
default: true
spec.yml:13:13 -> type: unexpected schema type: "bool"
The reported type is one the user never wrote, so the error points away
from the actual schema. The other branches of inferJSONType already
return the JSON Schema names, as does the sample-based inference in
Infer.Apply, which emits "boolean".
With the fix the schema above parses as boolean and generates
`Verbose OptBool`.
Present since 69578a9. `make generate examples` produces no diff: none
of the checked-in specs infer a type from a boolean value.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
With
infer_typesenabled, a schema whose type has to be inferred from a booleandefaultorenumnever generates — it fails with an error naming a type the usernever wrote.
Inference from the other JSON value kinds works, so the failure is specific to booleans:
type)default: trueunexpected schema type: "bool"booleanenum: [true, false]unexpected schema type: "bool"booleandefault: 1numbernumberdefault: "x"stringstringCause
inferJSONType(jsonschema/parser_enum.go) returns Go's"bool"forjx.Bool, whilethe type table in
parseSchema(jsonschema/parser.go) is keyed with the JSON Schemanames
object/array/string/integer/number/boolean/null. The lookup misses andparseSchemareports the inferred string back to the user as if they had written it,which points the error at the wrong thing — the spec says
default: true, nottype: bool.The neighbouring branches of the same function already return JSON Schema names
(
"string","number"), and the sample-based inference inInfer.Apply(
jsonschema/infer.go) emits"boolean", so"bool"is the odd one out.Present since 69578a9 (2022).
Fix
One word,
"bool"→"boolean". With it, the spec above parses asbooleanandgenerates
Verbose OptBool.infer_typesis used by five of ogen's own integration configs (sample_api,sample_api_nc,sample_api_ns,sample_api_nsnc,sample_api_no_otel), but none ofthe checked-in specs infers a type from a boolean value, so
make generate examplesproduces a 0-file diff — no regenerated code in this PR.
This does not implement #1529, which asks for enum/const inference without opting into
infer_types; this PR only fixes the path that already runs when it is on.Tests
jsonschema/parser_test.gogains two table tests:TestInferJSONType—inferJSONTypemapstrue/falseto"boolean", alongside theexisting string/number/null/object/array rows.
TestSchemaInferTypes— underSettings{InferTypes: true}, a schema with a booleandefaultand one with a booleanenumboth parse toType: Boolean, withstringand
numberrows pinning the branches that already worked.I verified both fail without the fix by reverting only
jsonschema/parser_enum.gotothe base commit and re-running:
Restoring the one-word change makes all subtests pass.
Verification
Run locally on macOS/arm64, go1.25.10,
CGO_ENABLED=0, caches cleared first.main@5c62f50 is green, so these are all comparisons against a clean baseline.
go build ./...go test --timeout 15m ./..../go.test.sh(race)cd examples && go test ./...golangci-lint v2.12.2 run -c .golangci.yml ./...make generate examples && git diff --exit-codeAlternative
The other direction is to leave
inferJSONTypealone and add"bool"to the type tablein
parseSchema. That would also accept a literaltype: boolin a spec, which is not avalid JSON Schema type, so I went with correcting the inferred string. Happy to switch if
you prefer the other shape.