fix(cli): honor --src-lang for directory JSON/URL inputs#3034
Open
schani wants to merge 2 commits into
Open
Conversation
Directory-mode input classification decided JSON vs. JSON Schema purely from file extension (.json/.url -> JSON data, .schema -> JSON Schema), completely ignoring an explicit --src-lang schema. This made `quicktype --src-lang schema ./schemas -l swift` parse schema files named *.json as JSON data samples, generating bogus structs/enums from the schema's own keys (`$schema`, `type`, `pattern`) instead of typing the samples the schema describes. Route .json/.url files found in a directory through the same typeSourcesForURIs() used for single-file/URL sources, so --src-lang is honored consistently. .schema/.gqlschema/.graphql handling is unchanged. Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
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
#1179 — Quicktype generates models for json-schema simpletypes when generating from a directory to a single file
Running
quicktype ./schemas -o api.swift -l swift(orquicktype --src-lang schema ./schemas -l swift) against a directory of*.json-named JSON Schema files produced bogus structs/enums built from the schema's own keys ($schema,type,pattern) — e.g. an enum whose cases are the schema'spatternregex strings — instead of generating types for the values the schema describes.Root cause
Directory-mode input classification in
src/index.ts(samplesFromDirectory/readFilesOrURLsInDirectory) decided whether each file was JSON data or JSON Schema purely from its file extension:.json/.urlwere always treated askind: "json"and only.schemaaskind: "schema". Crucially,getSourcescalledsamplesFromDirectory(dataDir, options.httpHeader)without ever passingoptions.srcLang, so an explicit--src-lang schemahad no effect at all for directory sources —.json-named schema files were always parsed as JSON data samples.This contrasts with the single-file path, where
typeSourcesForURIsalready switches onoptions.srcLangand forces every URI tokind: "schema"when--src-lang schemais given, regardless of extension — which is why a single file (quicktype --src-lang schema schema.json) worked correctly while the same file inside a directory did not.Fix
.json/.urlfiles discovered in a directory are now routed through the existingtypeSourcesForURIshelper (the same one used for single-file/URL sources), so--src-langis honored consistently whether the input is a single file or a directory..schema/.gqlschema/.graphqlextension handling, and the default (--src-lang json) behavior for.jsonfiles, are unchanged.Test coverage
This bug is about directory-mode source classification, which the fixture harness (which invokes quicktype on one file at a time) cannot express. Added
test/unit/directory-input.test.ts, which builds a temp directory of two schema documents named*.jsonand asserts thatquicktype({ srcLang: "schema", src: [dir], ... })produces the expected typealiases and does not emit a struct built from the schema's own keys. Confirmed this test fails on unfixed code and passes after the fix.Verification performed locally
npm run build— passes.npx vitest run test/unit— 171/171 tests pass (no regressions).--src-lang schemaagainst a directory of.json-named schema files) — now correctly producestypealias Name = String/typealias AnotherName = Stringinstead of bogus structs..schema-extension directories (already-correct case) still work, and that default (--src-lang json, no explicit override) directories of.jsonfiles still correctly generate JSON-derived structs (no regression to existing default behavior).QUICKTEST=true FIXTURE=schema-typescript script/test— 70/70 tests pass, exit code 0.FIXTURE=schema-swift(matching the language used in the original issue) could not be run in this environment because the Swift toolchain (swiftc) is not installed here; CI will cover this fixture.Fixes #1179
🤖 Generated with Claude Code