fix(cpp): initialize class members from JSON Schema default#3021
Merged
Conversation
Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
Co-Authored-By: Claude <noreply@anthropic.com>
# Conflicts: # packages/quicktype-core/src/input/JSONSchemaInput.ts
The default-value.1.fail.no-defaults.json fail sample is an empty object that must be rejected because the schema marks `id` as required. Languages that do not enforce required properties (and therefore already skip required.schema) cannot make that sample fail, so the schema-cjson fixture reported an unexpected pass. Add default-value.schema to skipSchema for exactly those languages (cjson, swift, haskell, typescript-zod, typescript-effect-schema, elixir), mirroring the existing required.schema skips. The positive and negative cases still run on every language that enforces required (cplusplus, csharp, java, python, rust, kotlin, dart, go, ...). Co-Authored-By: Claude <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.
Bug
For a JSON Schema property with a
default, the C++ generator ignored it entirely: the generated class member was declared with no initializer, leaving it uninitialized (garbage) when a class is default-constructed.Repro (
--lang cpp --src-lang schema) with:{ "type": "object", "properties": { "id": { "type": "integer", "default": 0 } }, "required": ["id"], "additionalProperties": false }Before:
After:
Root cause
JSONSchemaInput.tshad no attribute producer that captured a schema'sdefaultvalue onto the corresponding quicktypeType, so no downstream renderer ever had access to it. The C++ renderer'semitMemberalso had no notion of a default-value initializer.Fix
packages/quicktype-core/src/attributes/DefaultValue.ts: a newTypeAttributeKind<DefaultValue>(boolean | number | string | null) plus adefaultValueAttributeProducerthat readsschema.defaultand attaches it to the produced number/string/boolean/null type attributes.JSONSchemaInput.ts's attribute-producer list, and extendedJSONSchemaAttributeswithforBoolean/forNullso those primitive kinds can carry attributes the wayforNumber/forStringalready did.CPlusPlusRenderer.ts:emitMembernow accepts an optional default-value initializer Sourcelike and emitsType name = default;when present. A newcppDefaultValuehelper turns the attribute into the right C++ literal (numeric literal,true/false,nullptr, a quoted/escaped string literal, orEnumType::Casefor enum defaults), and both class-member and getter/setter emission paths pass it through.Scope: this focuses on the confirmed scalar-property repro from the issue (and covers enum defaults too, since the plumbing generalized naturally). It only affects JSON-Schema-sourced
defaultvalues reaching the C++ generator; no other language's default handling was touched.Test coverage
Added
test/inputs/schema/default-value.schema(+ matchingdefault-value.1.jsonsample) — a schema with a required integer property that hasdefault: 0. JSON Schema inputs undertest/inputs/schema/run automatically for every language'sschema-<lang>fixture unless opted out viaskipSchema, so this is picked up byschema-cplusplus(and all other schema fixtures) without further registration.Verification
npm run buildpasses cleanly.int64_t id = 0;instead of the uninitializedint64_t id;.schema-cplusplusfixture locally:QUICKTEST=true FIXTURE=schema-cplusplus script/test— all 68 tests pass, including the newdefault-value.schemacase.JSONSchemaAttributeschange don't regress other generators.Fixes #1535
🤖 Generated with Claude Code