OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-*] Incorrect behavior when using generics in typeMappings + top-level object schema
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?
- 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
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 the issue with config.yml and openapi.yml using the typescript-fetch generator command. Compare the generated model imports, FromJSON calls, and usersGetRaw return type with the Record<string,unknown> mapping; done means the built-in generic is used directly without generated model imports or conversion helpers.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100