Skip to content

[typescript-angular] Fix multiple imports from same module in importMappings and duplicate model re-exports - #25353

Open
slaesh wants to merge 2 commits into
OpenAPITools:masterfrom
slaesh:fix/typescript-angular-import-mapping
Open

slaesh wants to merge 2 commits into
OpenAPITools:masterfrom
slaesh:fix/typescript-angular-import-mapping

Conversation

@slaesh

@slaesh slaesh commented Oct 21, 2026 •

Copy link
Copy Markdown

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (Samples are unaffected as existing samples do not use multiple importMappings from the same module).
  • If your PR is targeting a particular programming language, @mention the technical committee members: @macjohnny @topce @akehir

Description of the change

This PR addresses two related issues when using importMappings with the TypeScript Angular generator:

  1. Service Import Map Collision (TypeScriptAngularClientCodegen.java):
    When multiple schemas mapped to the same module/package (e.g. @contracts or @foo/bar) via importMappings, imports were previously reduced by target module key, resulting in only one class being imported into generated API service files.

    • Fix: Recomputed model imports in postProcessOperationsWithModels from the operations' imports so all imported symbols are preserved without collision.
  2. Duplicate Re-Exports in models.ts (TypeScriptAngularClientCodegen.java & models.mustache):
    In models.ts, models mapped to external packages via importMappings were repeatedly re-exported via export * from '<module>'; for every mapped model (causing duplicate export statements when multiple models shared the same import path).

    • Fix: External mapped models are now recognized in postProcessSupportingFileData and deduplicated per module so export * from '<module>'; is emitted only once per external module in models.ts.
  3. Automated Tests:
    Added unit tests in TypeScriptAngularClientCodegenTest.java (testMultipleImportsFromSameModuleAndExternalModelReExport and testIssue22533ImportMappingsSamePathInServices) verifying that all external symbols from the same module are imported in services and re-exported without duplicates in models.ts.

Fixes #22533
Supersedes #23758
Relates to #7790

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

Comment thread modules/openapi-generator/src/main/resources/typescript-angular/models.mustache Outdated
@slaesh

slaesh commented Oct 21, 2026

Copy link
Copy Markdown
Author

Addressed both review comments in commit bbeeb01ca2a:

  1. Angular 9 & TypeScript < 3.8 Support: Added supportsExportType (active for ngVersion >= 10.0.0). In Angular 9 (ngVersion: 9.0.0 with TypeScript 3.6–3.7), the generator now safely falls back to export { ... } from '<module>'; instead of export type { ... }, while still deduplicating imports. Added unit test coverage for this case.
  2. Comparator: Restored the original classname-only comparator Comparator.comparing(o -> o.get("classname")) in DefaultGenerator.toImportsObjects to avoid altering dedupe behavior for other generators.

@slaesh slaesh changed the title [typescript-angular][core] Fix multiple imports from same module in importMappings and isolatedModules re-export [typescript-angular] Fix multiple imports from same module in importMappings and isolatedModules re-export Oct 22, 2026
…appings and isolatedModules re-export

- In TypeScriptAngularClientCodegen.postProcessOperationsWithModels, recompute service
  model imports so all types mapping to the same external module/package (e.g. via importMappings)
  are preserved and imported in the generated API services.
- In TypeScriptAngularClientCodegen.postProcessSupportingFileData, deduplicate external models
  per module and re-export them via 'export type { ... }' in models.mustache (with fallback
  to 'export { ... }' for Angular 9 / TypeScript < 3.8) to eliminate duplicate 'export *' lines
  and prevent TS1205 errors under isolatedModules: true.
- Add unit test testMultipleImportsFromSameModuleAndExternalModelReExport in
  TypeScriptAngularClientCodegenTest covering both fixes and Angular 9 backwards compatibility.

Relates to OpenAPITools#7790
Relates to OpenAPITools#6805
@slaesh
slaesh force-pushed the fix/typescript-angular-import-mapping branch from 8e64533 to ae43427 Compare October 22, 2026 08:28
@slaesh

slaesh commented Oct 22, 2026

Copy link
Copy Markdown
Author

Update:
Cleaned up the PR and squashed commits on top of the latest master:

  • Reverted changes in DefaultGenerator.java to avoid unintended side effects on other generators (e.g. C++, Lua, Nim header deduplication in samples).
  • Scoped the service import mapping fix cleanly into TypeScriptAngularClientCodegen.postProcessOperationsWithModels.
  • Changes are now strictly isolated to typescript-angular files without affecting other language samples.
  • Linked [BUG] [typescript] not compatible with --isolatedModules #6805 in the PR description as well to reference the isolatedModules / export type compatibility.

@slaesh

slaesh commented Oct 23, 2026

Copy link
Copy Markdown
Author

sorry, my fault - could some1 please re-approve the workflows? =)

@wing328 @macjohnny @topce @akehir

@thekhegay thekhegay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #22533 too (its spec now imports both Foo and Bar), #23758 is older attempt at same bug

Comment on lines +9 to +14
{{#supportsExportType}}
export type { {{{ types }}} } from '{{{ module }}}';
{{/supportsExportType}}
{{^supportsExportType}}
export { {{{ types }}} } from '{{{ module }}}';
{{/supportsExportType}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export type drops value side, so re-exported mapped enum or class can't be used as value anymore (Status.Active gives TS1362), while deduped export * alredy passes isolatedModules since TS1205 only hits named re-exports

Suggested change
{{#supportsExportType}}
export type { {{{ types }}} } from '{{{ module }}}';
{{/supportsExportType}}
{{^supportsExportType}}
export { {{{ types }}} } from '{{{ module }}}';
{{/supportsExportType}}
export * from '{{{ module }}}';

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for testing and for the great catch @thekhegay!

You are completely right: deduplicating export * from '{{{ module }}}'; already satisfies isolatedModules without stripping the value side of mapped enums/classes (and also removes the need for any ngVersion / TypeScript version branching).

@slaesh slaesh changed the title [typescript-angular] Fix multiple imports from same module in importMappings and isolatedModules re-export [typescript-angular] Fix multiple imports from same module in importMappings and duplicate model re-exports Oct 24, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

@thekhegay thekhegay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion aplied in dd87438 and #22533 spec is covered, lgtm from my side

This branch has not been deployed

No deployments
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] [typescript-angular] Import mappings with same path do not work properly when generating services

2 participants