fix(core): preserve JSON Schema enum case order#3028
Merged
Conversation
ConvenienceRenderer.forEachEnumCase always alphabetized enum cases by their rendered name before handing them to every language renderer's emitEnum, so C++ (and other languages) generated enums in alphabetical order instead of the order declared in the source JSON Schema/JSON. Stop sorting and let the existing insertion order (already preserved by the underlying case-names map) flow through unchanged. 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.
Bug
C++ (and, as it turns out, every other language) emitted enum cases in
alphabetical order instead of the order they appear in the source JSON
Schema / JSON
enumarray.Repro from the issue:
with schema property
"enum": ["B", "A", "E"]produced:instead of preserving the declared order
B, A, E.Root cause
ConvenienceRenderer.forEachEnumCase(inpackages/quicktype-core/src/ConvenienceRenderer.ts) unconditionallysorted enum case names alphabetically by their rendered
Namebeforehanding them to a language renderer's
emitEnum:forEachEnumCaseis called by essentially every language'semitEnum(C++, C#, Java, Kotlin, Swift, Python, Go, Rust, PHP, Ruby, etc.), so
this alphabetization was not C++-specific — it affected all generators.
The underlying
caseNamesmap already reflects the originalschema/JSON input order (JS
Map/Setpreserve insertion order), sothe fix is simply to stop forcibly re-sorting it, mirroring how
forEachClassPropertyalready preserves declared property order bydefault (only alphabetizing when the existing, opt-in
_alphabetizePropertiesrenderer option is set).Fix
Removed the unconditional
mapSortByalphabetization inforEachEnumCase, so enum cases are emitted in the same order theyappear in the source input.
Test coverage
test/unit/enum-order.test.ts, which reproduces the issue'sexact schema (
enum: ["B", "A", "E"]) and asserts the generated C++contains
enum class ErrorCode : int { B, A, E };. This fails onunfixed code (previously emitted
A, B, E) and passes after the fix.test/inputs/schema/enum.schema, whose enum arrays(
["lawful", "neutral", "chaotic"],["good", "neutral", "evil"])are non-alphabetical and are exercised for C++ through the
schema-cplusplusfixture (not in that language'sskipSchemalist),giving ongoing round-trip regression coverage for this case.
Verification
npm run buildpasses.npx vitest run test/unit— all 171 unit tests pass, including thenew regression test.
QUICKTEST=true FIXTURE=schema-cplusplus script/test— all 70 testspass locally (g++ toolchain available in this environment).
enum class ErrorCode : int { B, A, E };, matching input order.forEachEnumCase.Fixes #1289.
🤖 Generated with Claude Code