Repository navigation
feat(types): allow file imports for schemas - #44
Merged
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Top-level imports remove generated types from the global scope under default settings, while alias collisions and reference-handling defects can produce invalid declarations or imports of nonexistent exports.
5 open issues
Keep generated declarations global when adding imports · New Preserve external references in local-reference shortcuts · New Detect import alias collisions across all files · New Prefix sibling-file imports with ./ for URI-relative paths · New Export unreferenced definitions for external schema references · New
What changed in this PR
Adds cross-file JSON Schema references to the type-generation pipeline, supporting imports of root types and named definitions.
Changes:
- Introduces import fragments and groups imports by source file.
- Adds default exports when namespace exports are enabled.
- Updates identifier handling and tests for external references.
| File | Description |
|---|---|
src/utils/typeDefinitions.ts |
Treats dots as separators when building identifiers. |
src/utils/jsonSchema.ts |
Creates external-reference imports and root default exports. |
src/utils/fragments.ts |
Adds import fragments to the fragment model. |
src/index.ts |
Groups import fragments and emits import declarations. |
src/index.test.ts |
Tests external imports and updates export snapshots. |
src/__snapshots__/index.test.ts.snap |
Updates callback identifiers affected by dot handling. |
Review effort: Balanced
nfroidure
force-pushed
the
feat/file_imports
branch
from
November 3, 2026 17:27
45b741d to
aaa774c
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Shared identifier normalization can generate duplicate enum members and invalid TypeScript, while external-reference handling can emit unresolvable imports or reject valid schemas through alias collisions.
8 open issues
Preserve shared identifier normalization for existing callers · New Propagate alwaysAddDefs into schema options · New Resolve external refs against the effective $id base URI · New Handle external refs in OpenAPI parameter schemas · New Reject unsupported URI references before generating imports · New Reject unsupported nested external JSON pointers · New Allocate unique aliases for external schema imports · New Prefix bare relative refs with ./ in import paths · New
5 resolved since last review
Review effort: Max
| export function buildIdentifier(part: string): string { | ||
| const identifier = part | ||
| .replace(/[^a-z0-9-_ ]/gi, '') | ||
| .replace(/[^a-z0-9-_. ]/gi, '') |
| const fragments = await jsonSchemaToFragments(context, rootSchema); | ||
|
|
||
| if ( | ||
| context.jsonSchemaOptions.alwaysAddDefs && |
|
|
||
| if ('$ref' in schema && typeof schema.$ref === 'string') { | ||
| const referenceParts = relativeReferenceToNamespace(schema.$ref); | ||
| const [filePart, pointerPart] = schema.$ref.split('#'); |
Comment on lines
170
to
+171
| if ('$ref' in schema && typeof schema.$ref === 'string') { | ||
| const referenceParts = relativeReferenceToNamespace(schema.$ref); | ||
| const [filePart, pointerPart] = schema.$ref.split('#'); |
|
|
||
| if ('$ref' in schema && typeof schema.$ref === 'string') { | ||
| const referenceParts = relativeReferenceToNamespace(schema.$ref); | ||
| const [filePart, pointerPart] = schema.$ref.split('#'); |
| ? relativeReferenceToNamespace(`#${pointerPart}`) | ||
| : []; | ||
|
|
||
| if (referenceParts.length && referenceParts[0] !== '$defs') { |
Comment on lines
+190
to
+192
| const baseIdentifier = buildIdentifier( | ||
| filePart + (isDefault ? '_default' : '_definitions'), | ||
| ); |
nfroidure
force-pushed
the
feat/file_imports
branch
from
November 3, 2026 20:05
aaa774c to
76b1980
Compare
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.


No description provided.