fix(cli): respect --src-lang schema for directory input - #2998
Open
schani wants to merge 2 commits into
Open
Conversation
Directory --src handling classified files purely by extension, so .json files inside a directory were always treated as JSON data samples even when -s/--src-lang schema was passed. Single-file --src already respected srcLang via typeSourcesForURIs. This made a directory of JSON Schema files (referencing each other with $ref) produce polluted models with schema/title/allOf properties and no derived enums, unlike the same files generated one at a time. Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
…age-2998-1784733605
No generated-output differences✅ Generated outputs are unchanged between the PR base and head revisions. |
No generated-output differences✅ Generated outputs are unchanged between the PR base and head revisions. |
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
When generating from a directory of JSON Schema files with
-s schema(e.g.quicktype -s schema -l swift -S ./event.json --src ./events --just-types), quicktype produced very different — and wrong — output compared to running the same command against a single file from that directory:schema,title, andallOfproperties instead of the schema's actual properties.TypeEnum) was generated forenum-typed properties.payload(and other properties) only showed up in one of several sibling models instead of all of them.--struct-or-class classhad no effect — structs were generated instead of classes.Single-file input (
--src ./events/event1.json) with the same-s schemaflag worked correctly, producing clean models.Root cause
src/index.tshas two separate code paths for turning--srcentries intoTypeSources:--srcentries go throughtypeSourcesForURIs(), which checksoptions.srcLang("schema"vs"json") to decide whether to parse a file as JSON Schema or as a JSON data sample.--srcentries went throughsamplesFromDirectory()/readFilesOrURLsInDirectory(), which classified each file purely by its file extension:.json/.urlfiles were always treated as JSON data samples, and only files with a literal.schemaextension were treated as JSON Schema.options.srcLangwas never consulted for directory input, so-s schemahad no effect on.jsonfiles found inside a directory — they were parsed as raw JSON data, causing the schema's own structure ($schema,title,allOf, ...) to be inferred as if it were the payload.This matches an earlier comment on the issue noting that renaming
.jsonfiles to.schema"fixed" the problem for a directory.Fix
samplesFromDirectory()now takes the CLI'ssrcLangand, when it is"schema", treats.jsonfiles in the directory as JSON Schema sources (same as.schemafiles) instead of JSON data samples — mirroring the behaviortypeSourcesForURIs()already has for non-directory input..urlhandling, GraphQL directories, and the default (json) source language for directories of plain JSON samples are unaffected.Test coverage
Added
test/unit/directory-schema-input.test.ts, a CLI-level regression test (viamain()fromsrc/index.ts) that builds a temp directory of two JSON Schema files (each$ref-ing a shared base schema, mirroring the issue's repro), runs quicktype withsrcLang: "schema"against the directory, and asserts on the generated Swift output: both classes are generated cleanly (no leakedschema/title/allOffields),payloadandnameappear in both models, the schema'senumvalues produce the expected enum cases, andstruct-or-class: "class"is honored. This test fails on unfixed code and passes after the fix.This bug is CLI directory-handling behavior in
src/index.tsrather than something the per-language fixture harness (which drives quicktype via its library API against single schema/JSON files) is set up to express, so a unit test was the appropriate coverage per repo conventions.Verification
npm run build— passes.npm run test:unit— 164/164 tests pass (including the new regression test).npx biome checkon changed files — passes.schema/title/allOf), no enum, missingpayloadin one model, structs generated even with--struct-or-class class.Event1andEvent2(correctid/name/payload/typefields, per-model enums, andclassoutput honored with--struct-or-class class).swiftfixture suite wasn't run locally; CI will validate that the change doesn't affect existing Swift/other-language fixtures.Fixes #1776
🤖 Generated with Claude Code