Skip to content

fix(cli): honor --src-lang for directory JSON/URL inputs#3034

Open
schani wants to merge 2 commits into
masterfrom
agent/fix-issue-1179
Open

fix(cli): honor --src-lang for directory JSON/URL inputs#3034
schani wants to merge 2 commits into
masterfrom
agent/fix-issue-1179

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

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 (or quicktype --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's pattern regex 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/.url were always treated as kind: "json" and only .schema as kind: "schema". Crucially, getSources called samplesFromDirectory(dataDir, options.httpHeader) without ever passing options.srcLang, so an explicit --src-lang schema had 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 typeSourcesForURIs already switches on options.srcLang and forces every URI to kind: "schema" when --src-lang schema is 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/.url files discovered in a directory are now routed through the existing typeSourcesForURIs helper (the same one used for single-file/URL sources), so --src-lang is honored consistently whether the input is a single file or a directory. .schema/.gqlschema/.graphql extension handling, and the default (--src-lang json) behavior for .json files, 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 *.json and asserts that quicktype({ 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).
  • Re-ran the exact repro (--src-lang schema against a directory of .json-named schema files) — now correctly produces typealias Name = String / typealias AnotherName = String instead of bogus structs.
  • Verified .schema-extension directories (already-correct case) still work, and that default (--src-lang json, no explicit override) directories of .json files 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

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>
@github-actions

Copy link
Copy Markdown

No generated-output differences

✅ Generated outputs are unchanged between the PR base and head revisions.

@github-actions

Copy link
Copy Markdown

No generated-output differences

✅ Generated outputs are unchanged between the PR base and head revisions.

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.

[BUG] Quicktype generates models for json-schema simpletypes when generating from a directory to a single file

1 participant