OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-axios] import-mappings option is not adding any imports to generated code

Open
#16,743 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

Using the typescript-axios generator I am trying to import some custom types in the generated file so I can map to them. I add the option --import-mappings CustomType=<relativepath to ts file that exports it> but no import is generated. I can use a type mapping to translate desired types to my custom type. I am just forced to manually import after generation, and that does not work well with pipeline.

openapi-generator version

tested with 5.4.0, 7.0.1, and 7.1.0 via docker

OpenAPI declaration file content or url

relevant part of spec

{
  "swagger": "2.0",
  "definitions": {
    "DTO": {
      "type": "object",
      "properties": {
        "dateTime": {
          "$ref": "#/definitions/UTCTime"
        },
    "UTCTime": {
      "format": "yyyy-mm-ddThh:MM:ssZ",
      "type": "string",
      "example": "2016-07-22T00:00:00Z"
    },
  }
   ...
}
Generation Details

Most details don't seem very important. Everything works except the generated import. I am working on a react project and using npm installed openapi-generator-cli.

Steps to reproduce

I have the custom types file defined with a simple type alias

export type DateTimeString = string

and without the type mapping the type generated for this field is string.

the command to try to map and import my types is

npx openapi-generator-cli generate -i <path to json> -g typescript-axios -o <path to generated client> \ 
  --import-mappings DateTimeString=<path to custom types file> \
  --type-mappings yyyy-mm-ddThh:MM:ssZ=DateTimeString  

As described this generates the api client, it assigns the DateTimeString type to my DTO dateTime field, but it does not import the file that defines that type.

Related issues/PRs

Could not find any.

Suggest a fix

I would think it would add the line import { DateTimeString } from '<specified import path>' to the generated file so that the mapped types could use the custom type. If I am mistaken on that then my suggestion is improving the docs. But the description for import-mappings

specifies mappings between a given class and the import that should be used for that class in the format of type=import,type=import. You can also have multiple occurrences of this option

seems clear enough that I assume this is just not working with the typescript-axios generator.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported npx openapi-generator-cli command, the typescript-axios generator, the Swagger 2.0 snippet, and the custom DateTimeString alias. Trace how --import-mappings is handled when --type-mappings produces the custom type. Done means the generated DTO includes the mapped type's import and regression coverage verifies the behavior.

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
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.