fix(cli): respect --src-lang schema for directory sources#3029
Open
schani wants to merge 2 commits into
Open
Conversation
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.
The bug
When
--srcpointed at a directory,--src-lang schemawas silentlyignored for
.jsonfiles in that directory.samplesFromDirectory()(usedonly for directory sources) classified every file purely by its extension —
.json/.urlwere always parsed as plain JSON samples,.schemawasalways parsed as JSON Schema — with no way to override this via
--src-lang. This meant a directory of*.schema.jsonfiles passed with--src-lang schemagenerated interfaces from the raw JSON structure of theschema documents (
$schema,$id,title,properties, anAgehelpertype, 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 onoptions.srcLang.Root cause
getSources()insrc/index.tscallssamplesFromDirectory(dataDir, options.httpHeader)without ever passingoptions.srcLangthrough, andsamplesFromDirectory/its innerreadFilesOrURLsInDirectoryhad noparameter for it — extension-based detection was the only path.
The fix
samplesFromDirectorynow accepts an optionalsourceLanguageparameter.getSourcespassesoptions.srcLanginto it.sourceLanguage === "schema",.json/.urlfiles inside adirectory are treated as JSON Schema sources (matching what happens when
the same file is passed directly, without going through a directory).
--src-langis not explicitly"schema", the previousextension-based auto-detection (
.json/.url→ JSON,.schema→ schema,.gqlschema/.graphql→ GraphQL) is unchanged, so existing directoryfixtures relying on that default behavior are unaffected.
Test coverage
Added
test/unit/directory-source-language.test.ts, which:main()entry point: runs--src-lang schemaonce on a single schema file and once on a directorycontaining 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).
--src-lang) directory-scanning behavior stillauto-detects
.schemafiles as JSON Schema by extension, guardingagainst a regression of the pre-existing behavior.
This was placed in
test/unit/rather than as an end-to-end JSON/JSONSchema fixture because the bug is specifically in the CLI's
directory-scanning/dispatch logic in
src/index.ts(which extension maps towhich source kind, given
--src-lang), a layer the fixture harness (whichdrives 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.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 ofthe previous JSON-derived interface with bogus
$schema/$id/title/properties/Agefields.languages.
Fixes #1278
🤖 Generated with Claude Code