Skip to content

fix(cli): respect --src-lang schema for directory input - #2998

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

fix(cli): respect --src-lang schema for directory input#2998
schani wants to merge 2 commits into
masterfrom
agent/fix-issue-1776

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

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:

  • Models were polluted with schema, title, and allOf properties instead of the schema's actual properties.
  • No enum (TypeEnum) was generated for enum-typed properties.
  • payload (and other properties) only showed up in one of several sibling models instead of all of them.
  • --struct-or-class class had no effect — structs were generated instead of classes.

Single-file input (--src ./events/event1.json) with the same -s schema flag worked correctly, producing clean models.

Root cause

src/index.ts has two separate code paths for turning --src entries into TypeSources:

  • Non-directory --src entries go through typeSourcesForURIs(), which checks options.srcLang ("schema" vs "json") to decide whether to parse a file as JSON Schema or as a JSON data sample.
  • Directory --src entries went through samplesFromDirectory() / readFilesOrURLsInDirectory(), which classified each file purely by its file extension: .json/.url files were always treated as JSON data samples, and only files with a literal .schema extension were treated as JSON Schema. options.srcLang was never consulted for directory input, so -s schema had no effect on .json files 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 .json files to .schema "fixed" the problem for a directory.

Fix

samplesFromDirectory() now takes the CLI's srcLang and, when it is "schema", treats .json files in the directory as JSON Schema sources (same as .schema files) instead of JSON data samples — mirroring the behavior typeSourcesForURIs() already has for non-directory input. .url handling, 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 (via main() from src/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 with srcLang: "schema" against the directory, and asserts on the generated Swift output: both classes are generated cleanly (no leaked schema/title/allOf fields), payload and name appear in both models, the schema's enum values produce the expected enum cases, and struct-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.ts rather 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 check on changed files — passes.
  • Manually reran the exact repro from the issue before and after the fix:
    • Before: directory input produced polluted structs (schema/title/allOf), no enum, missing payload in one model, structs generated even with --struct-or-class class.
    • After: directory input produces the same clean shape as single-file input for both Event1 and Event2 (correct id/name/payload/type fields, per-model enums, and class output honored with --struct-or-class class).
  • Swift toolchain isn't available in this environment, so the swift fixture 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

schani and others added 2 commits July 20, 2026 17:15
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>
@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.

Model generated for a directory is different from when it's generated from a single schema

1 participant