Skip to content

[BUG][typescript-*] Bad behavior when trying to map generics #21317

@allejo

Description

@allejo

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

I have an OpenAPI file where one of the components has a data type of "object." In TypeScript, the object data type is very generic and doesn't help with data typing but I do think it's an appropriate default for most cases. However, for my use case on a particular project, I would like to map object to Record<string, unknown> but this leads to generation problems.

The first problem is that Record is a built-in type, so there's no need to import it. This results in bad imports being generated.

import type { Recordstringunknown } from './Recordstringunknown';
import {
    RecordstringunknownFromJSON,
    RecordstringunknownFromJSONTyped,
    RecordstringunknownToJSON,
    RecordstringunknownToJSONTyped,
} from './Recordstringunknown';

The second problem (also seen in the import above) is that the <> for generics are stripped out of what's generated.

export interface User {
    // ...

    /**
     * 
     * @type {Recordstringunknown}
     * @memberof User
     */
    metadata?: Recordstringunknown;
}

The above generated code should be Record<string,unknown> as specified in the config.yml file I have listed below. Related also, even though my coding style preference is to have a space after the comma (i.e., Record<string, unknown>), that leads to even worse generated results.

Note: the Java template can handle the <> (generic syntax) and spaces without issue.

import type { Recordstring unknown } from './Recordstring unknown';
import {
    Recordstring unknownFromJSON,
    Recordstring unknownFromJSONTyped,
    Recordstring unknownToJSON,
    Recordstring unknownToJSONTyped,
} from './Recordstring unknown';

// ...

export interface User {
    // ...

    /**
     * 
     * @type {Recordstring unknown}
     * @memberof User
     */
    metadata?: Recordstring unknown;
}
openapi-generator version

7.13.0

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Sample API
  description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
  version: 1.0.0

servers:
  - url: http://api.example.com/v1
    description: Optional server description, e.g. Main (production) server
  - url: http://staging-api.example.com
    description: Optional server description, e.g. Internal staging server for testing

paths:
  /users:
    get:
      summary: Returns a list of users.
      description: Optional extended description in CommonMark or HTML.
      responses:
        "200": # status code
          description: A JSON array of user names
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
          format: int64
          description: The user ID.
        name:
          type: string
          description: The user name.
        email:
          type: string
          format: email
          description: The user email address.
        metadata:
          type: object
Generation Details
# config.yml

typeMappings:
 object: Record<string,unknown>
Steps to reproduce
docker run --rm \
  -v ".:/local" \
  openapitools/openapi-generator-cli:latest generate \
  -c "/local/config.yml" \
  -g "typescript-fetch" \
  -i "/local/openapi.yml" \
  -o /local/src
Related issues/PRs

None that I've been able to find.

Suggest a fix

I believe this is a problem with the typescript-* templates; I've tested this with typescript-fetch and typescript-axios and both have the same problem. But if I switch the template to java, it correctly handles generics/spaces

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions