Skip to content

fix(cli): respect --src-lang schema for directory sources#3029

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

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

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

The bug

When --src pointed at a directory, --src-lang schema was silently
ignored for .json files in that directory. samplesFromDirectory() (used
only for directory sources) classified every file purely by its extension —
.json/.url were always parsed as plain JSON samples, .schema was
always parsed as JSON Schema — with no way to override this via
--src-lang. This meant a directory of *.schema.json files passed with
--src-lang schema generated interfaces from the raw JSON structure of the
schema documents ($schema, $id, title, properties, an Age helper
type, etc.) instead of from the schema they describe. Passing the exact same
file directly (not through a directory) worked correctly, because the
non-directory code path (typeSourcesForURIs) does switch on
options.srcLang.

Root cause

getSources() in src/index.ts calls samplesFromDirectory(dataDir, options.httpHeader) without ever passing options.srcLang through, and
samplesFromDirectory/its inner readFilesOrURLsInDirectory had no
parameter for it — extension-based detection was the only path.

The fix

  • samplesFromDirectory now accepts an optional sourceLanguage parameter.
  • getSources passes options.srcLang into it.
  • When sourceLanguage === "schema", .json/.url files inside a
    directory are treated as JSON Schema sources (matching what happens when
    the same file is passed directly, without going through a directory).
  • When --src-lang is not explicitly "schema", the previous
    extension-based auto-detection (.json/.url → JSON, .schema → schema,
    .gqlschema/.graphql → GraphQL) is unchanged, so existing directory
    fixtures relying on that default behavior are unaffected.

Test coverage

Added test/unit/directory-source-language.test.ts, which:

  1. Reproduces the bug end-to-end via the CLI's main() entry point: runs
    --src-lang schema once on a single schema file and once on a directory
    containing only that file, and asserts the generated TypeScript is
    byte-identical between the two invocations (this fails on unfixed code,
    since the directory case previously produced JSON-derived output instead
    of schema-derived output).
  2. Confirms the default (no --src-lang) directory-scanning behavior still
    auto-detects .schema files as JSON Schema by extension, guarding
    against a regression of the pre-existing behavior.

This was placed in test/unit/ rather than as an end-to-end JSON/JSON
Schema fixture because the bug is specifically in the CLI's
directory-scanning/dispatch logic in src/index.ts (which extension maps to
which source kind, given --src-lang), a layer the fixture harness (which
drives the library API directly rather than the CLI's argument/directory
handling) does not exercise.

Verification

  • npm run build — passes.
  • npm run test:unit — 172/172 tests pass (including the two new tests).
  • QUICKTEST=true FIXTURE=schema-typescript script/test — 70/70 tests pass,
    confirming the fix doesn't affect ordinary (non-directory,
    non---src-lang-schema-on-directory) schema handling.
  • Manually re-ran the original repro:
    node dist/index.js --src-lang schema --lang ts --src <dir-with-person.schema.json>
    now produces the correct schema-derived interface
    (age?: number; name: string; [property: string]: unknown;) instead of
    the previous JSON-derived interface with bogus $schema/$id/title/
    properties/Age fields.
  • CI will additionally validate the broader fixture matrix across all
    languages.

Fixes #1278

🤖 Generated with Claude Code

@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.

Schema flag for folders incorrectly treats "*.schema.json" files

1 participant