OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.85k stars 6.59k forks source link

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

Open sambolias opened 1 year ago

sambolias commented 1 year ago

Bug Report Checklist

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.

madeupname commented 1 year ago

I'm having a similar issue, but first, your type mapping line seems off:

--type-mappings yyyy-mm-ddThh:MM:ssZ=DateTimeString

You're listing a date format, not a type. I would expect something like Date=DateTimeString

That said, I also can't get the generator to use a type I have defined separately. I'm using Generator 7.0.1 and an OAS 3.0.1 definition with a Product schema defined. I'm trying to get the generator to use the interface I've defined. I have in my Gradle plugin:

    configOptions.put("withSeparateModelsAndApi", "true")
    configOptions.put("modelPackage", "model")
    configOptions.put("apiPackage", "api")
    languageSpecificPrimitives.set(["Product"]) // optional
    importMappings.put("Product", "../../domain")
    typeMappings.put("Product", "Product")

No combination seems to work - the generator is intent on generating it's own model. Setting languageSpecificPrimitives only makes it generate a ModelProduct interface so there is no name collision.

It's possible this feature is not supported. From https://openapi-generator.tech/docs/usage#examples:

Most generators allow for types bound to the OpenAPI Specification's types to be remapped to a user's desired types.

Emphasis mine, but when I look at the import mapping sections of other generators like Java, there is a list of types that are mapped, suggesting those specifically can be overridden. Generator docs are consistently formatted it looks like they have forbidden maintainers from providing any additional text to direct users, explain options, etc.

I'd be grateful if someone could just confirm this is not a supported feature for typescript-axios.

sambolias commented 1 year ago

@madeupname my type mapping is working as expected. I can't really use the type there, since that is just string. The definition didn't seem to work for that mapping, format was the only thing that worked.

For a temporary workaround on this I am using sed to write my desired import line to the generated code, and adding that command to my generation script so it can be automated. Hopefully this import mapping issue can be fixed, or explained better, so I no longer have to do that.

xiexin36 commented 1 month ago

Has the same issue, Hope resolve.