Skip to content

fix(cpp): initialize class members from JSON Schema default#3021

Merged
schani merged 4 commits into
masterfrom
agent/fix-issue-1535
Jul 22, 2026
Merged

fix(cpp): initialize class members from JSON Schema default#3021
schani merged 4 commits into
masterfrom
agent/fix-issue-1535

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

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:

private:
int64_t id;

After:

private:
int64_t id = 0;

Root cause

JSONSchemaInput.ts had no attribute producer that captured a schema's default value onto the corresponding quicktype Type, so no downstream renderer ever had access to it. The C++ renderer's emitMember also had no notion of a default-value initializer.

Fix

  • Added packages/quicktype-core/src/attributes/DefaultValue.ts: a new TypeAttributeKind<DefaultValue> (boolean | number | string | null) plus a defaultValueAttributeProducer that reads schema.default and attaches it to the produced number/string/boolean/null type attributes.
  • Wired the new producer into JSONSchemaInput.ts's attribute-producer list, and extended JSONSchemaAttributes with forBoolean/forNull so those primitive kinds can carry attributes the way forNumber/forString already did.
  • CPlusPlusRenderer.ts: emitMember now accepts an optional default-value initializer Sourcelike and emits Type name = default; when present. A new cppDefaultValue helper turns the attribute into the right C++ literal (numeric literal, true/false, nullptr, a quoted/escaped string literal, or EnumType::Case for 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 default values reaching the C++ generator; no other language's default handling was touched.

Test coverage

Added test/inputs/schema/default-value.schema (+ matching default-value.1.json sample) — a schema with a required integer property that has default: 0. JSON Schema inputs under test/inputs/schema/ run automatically for every language's schema-<lang> fixture unless opted out via skipSchema, so this is picked up by schema-cplusplus (and all other schema fixtures) without further registration.

Verification

  • npm run build passes cleanly.
  • Manually re-ran the repro from the issue: generated C++ now shows int64_t id = 0; instead of the uninitialized int64_t id;.
  • Ran the full schema-cplusplus fixture locally: QUICKTEST=true FIXTURE=schema-cplusplus script/test — all 68 tests pass, including the new default-value.schema case.
  • Did not re-run every other language's schema fixture locally; CI will validate that the new schema input and JSONSchemaAttributes change don't regress other generators.

Fixes #1535

🤖 Generated with Claude Code

schani and others added 4 commits July 20, 2026 17:49
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>
@schani
schani merged commit c7892c9 into master Jul 22, 2026
28 checks passed
@schani
schani deleted the agent/fix-issue-1535 branch July 22, 2026 00:32
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.

default values for c++ class from json schema

1 participant