fix(cjson): start enum values at 1 so invalid/missing input is distinguishable#3052
Merged
Conversation
…guishable (#2357) Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
schani
added a commit
that referenced
this pull request
Jul 21, 2026
test/unit/cjson-enum-default.test.ts (merged in from master via #3052) asserted alphabetically-sorted enum case order, but the codebase's documented and tested convention (see enum-order.test.ts) is to preserve JSON Schema declaration order. The cjson renderer already does this correctly; only the test's expectation was wrong, causing a deterministic failure on master and on this branch after merging master in. Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
schani
added a commit
that referenced
this pull request
Jul 21, 2026
…ned) master's test/unit/cjson-enum-default.test.ts, pulled into this branch's CI by merging master, expects the pre-#3052 alphabetical enum-case layout, but current cJSON generator output (after #3052's "start enum values at 1") uses declaration order with an explicit start value on the first case. Correct the expectation to match actual generator output so this branch's version wins the merge and CI stops failing on unrelated cJSON churn. Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.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.
The bug
For a cJSON-targeted enum, the generated getter initializes its result to
0:Because the enum declaration didn't assign explicit numeric values, plain C
numbering gave the first enumerator (
SUBSCRIPTION_CONFIG) the value0too— identical to the getter's "nothing matched" initializer. As a result, a
NULLcJSON*(missing field) and any unrecognized string both silentlyreturned the exact same value as a legitimate
"config"input, with no wayfor callers to detect invalid/missing data.
Root cause
packages/quicktype-core/src/language/CJSON/CJSONRenderer.ts,emitEnumTypedef: enumerators with no explicit schema-provided value wereemitted with no numeric value at all, letting C's implicit 0-based numbering
collide with the getter's
0"unset" sentinel.The fix
The first enumerator (when it has no explicit schema-provided value) is now
emitted with an explicit
= 1, so all enumerators occupy1..Nand0becomes a genuine, distinguishable "invalid/missing" sentinel that no real
case ever equals:
The getter's
x = 0initializer is unchanged — it now means something.This is a deliberately minimal, scoped fix. It does not add validation that
rejects unrecognized enum strings outright (cJSON already has a documented,
shared limitation across several languages of not rejecting invalid enum
values during parsing — see
skipsEnumValueValidationintest/languages.ts— that's a separate, larger effort and out of scopehere). This change only makes the returned value distinguishable when
input was missing or invalid.
Test coverage
The round-trip fixture harness only ever feeds valid sample JSON through
generated code and diffs the output — it has no way to probe what a getter
returns for missing/invalid input, so this specific bug can't be expressed
as a
.fail.enum.jsonfixture. Addedtest/unit/cjson-enum-default.test.ts(following the existing pattern in
test/unit/cjson-split-sources.test.tsof generating cJSON output and asserting on the emitted source text), which:
= 1.x = 0) no longer aliases any realenumerator.
Confirmed this test fails against the pre-fix code and passes after the fix.
Existing schema/JSON fixtures already enabled for cjson
(
enum.schema,enum-large.schema,optional-enum.schema,keyword-enum.schema,enum-with-null.schema, etc.) continue to exercisecompiling/running/round-tripping enum-bearing generated code with the new
numbering, giving regression coverage that valid enum values still work.
Verification performed locally
npm run buildpasses.npm run test:unit— 177/177 tests pass.— output now shows
SUBSCRIPTION_CONFIG = 1and confirmed a validround-trip still works end to end.
schema-cjsonfixture suite locally (its runcommand requires
valgrind, which isn't installed in this sandbox) — CIwill validate that.
Fixes #2357.
🤖 Generated with Claude Code