OpenAPITools / OpenAPITools/openapi-generator
[BUG] typescript-fetch doesn't work with external libraries when using importMappings
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When I have an import mapping set up for an external library, typescript-fetch doesn't handle it correctly. I checked using typescript-angular, and that handles it correctly.
The use case here is that the OpenAPI spec needs to be able to reference model class names that exist in another external TypeScript package. In my example, @somecompany/package is a package that contains TypeScript types and interfaces for all of our models, and the example-api-client ideally can reference that package. It would be a dependency of the client.
Workaround: I haven't tried this yet, but I assume I can use templates to put the correct import mappings in place.
openapi-generator version
openapi-generator-cli 6.0.1
commit : 9e412ed
built : 2022-07-03T16:21:27Z
source : https://github.com/openapitools/openapi-generator
docs : https://openapi-generator.tech/
OpenAPI declaration file content or url
OpenAPI schema (schema.json):
https://gist.github.com/robbieaverill/63d28f9e89ec6184af333fb6a8fb5f30
Config file (codegen.json):
{
"npmName": "example-api-client",
"importMappings": {
"SanitizedAgent": "@somecompany/package/types/agents"
},
"validateSpec": false
}
Generation Details
Steps to reproduce
openapi-generator generate -i schema.json -g typescript-fetch -c codegen.json -o client
Inspect client/src/apis/DefaultApi.ts, look at the imports.
Result:
import * as runtime from '../runtime';
import type {
@somecompany/package/types/agents,
} from '../models';
import {
@somecompany/package/types/agentsFromJSON,
@somecompany/package/types/agentsToJSON,
} from '../models';
Expected result:
import * as runtime from '../runtime';
import { SanitizedAgent } from '@somecompany/package/types/agents';
import {
SanitizedAgentFromJSON,
SanitizedAgentToJSON,
} from '../models';
If I run the same generation command using typescript-angular, I get the correct import mapping:
openapi-generator generate -i schema.json -g typescript-angular -c codegen.json -o angularclient
Inspect angularclient/src/api/default.service.ts, look at the imports.
I see the expected import mapping from my external package:
// @ts-ignore
import { SanitizedAgent } from '@somecompany/package/types/agents';
Related issues/PRs
- #5079
- https://github.com/OpenAPITools/openapi-generator/pull/10644 Adds support for this feature to typescript-angular
Suggest a fix
Perhaps port the same changes from the PR above into typescript-fetch
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce with schema.json, codegen.json, and the openapi-generator generate command for typescript-fetch, then inspect client/src/apis/DefaultApi.ts. Compare the typescript-angular handling and the changes in PR #10644; done means the generated imports use SanitizedAgent from the external package while the serialization helpers remain valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100