Skip to content

[BUG][typescript-*] Incorrect behavior when using generics in typeMappings + top-level object schema #22251

@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

Support for generics in TypeScript generated libraries was added in #21414 after being reported as a bug in #21317; this bug report is a follow-up to that implementation.

When using Record<string, unknown> as a mapped type in the configuration and an API returns object as its schema, it incorrectly generates the FromJSON function to use that generic.

// These imports should not exist since `Record` is a built-in type
import type {
  Recordstringunknown,
} from '../models/index';
import {
    RecordstringunknownFromJSON,
    RecordstringunknownToJSON,
} from '../models/index';
    // Return type should be `Record<string, unknown>` to match `config.yml`
    async usersGetRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Recordstringunknown>> {
        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        const response = await this.request({
            path: `/users`,
            method: 'GET',
            headers: headerParameters,
            query: queryParameters,
        }, initOverrides);

        // This should be changed to not use a `FromJSON` maybe?
        return new runtime.JSONApiResponse(response, (jsonValue) => RecordstringunknownFromJSON(jsonValue));
    }

/cc @macjohnny for visibility since they made the original PR to fix the original bug

openapi-generator version

7.17.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:
                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

Related to #21317 and #21414

Suggest a fix

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