OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-fetch] A request body of type `Set` fails at runtime
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 (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
When the request body is directly a set of strings (NOT wrapped in an object), the application crashes at runtime because the Set<string> is not converted to an Array first, thus it generates the JSON {}.
If the request body is wrapped in an object, then it works fine.
openapi-generator version
I am using the openapitools/openapi-generator-cli:latest Docker image.
OpenAPI declaration file content or url
openapi: 3.0.0
info:
version: 1.0.0
title: OpenAPI Petstore
paths:
/pet:
post:
operationId: addPet
responses:
'204':
description: successful operation
requestBody:
content:
application/json:
schema:
type: array
items:
type: string
uniqueItems: true
The generated Typescript file is:
import * as runtime from '../runtime';
export interface AddPetRequest {
requestBody?: Set<string>;
}
export class DefaultApi extends runtime.BaseAPI {
async addPetRaw(requestParameters: AddPetRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<void>> {
const queryParameters: any = {};
const headerParameters: runtime.HTTPHeaders = {};
headerParameters['Content-Type'] = 'application/json';
const response = await this.request({
path: `/pet`,
method: 'POST',
headers: headerParameters,
query: queryParameters,
body: requestParameters['requestBody'],
}, initOverrides);
return new runtime.VoidApiResponse(response);
}
async addPet(requestParameters: AddPetRequest = {}, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<void> {
await this.addPetRaw(requestParameters, initOverrides);
}
}
⚠️ Notice requestParameters['requestBody']
I think it should actually be something like (I'm no Typescript expert so maybe it should be slightly different):
- body: requestParameters['requestBody'],
+ body: Array.from(requestParameters['requestBody']),
Generation Details
$ docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate -i /local/pet.yaml -g typescript-fetch -o /local/dist
Steps to reproduce
- Generate the client code
- Call it with
addPet({requestBody: new Set<string>(["a"])})(it typechecks) - At runtime it actually fails because it sent the JSON body
{}instead of["a"]
Related issues/PRs
- https://github.com/OpenAPITools/openapi-generator/issues/8258
- The error is different and it is about
typescript-angular, nottypescript-fetch
- The error is different and it is about
- https://github.com/OpenAPITools/openapi-generator/issues/11508
- The issue is about
typescript-rxjs
- The issue is about
- https://github.com/OpenAPITools/openapi-generator/issues/10770
- Fixed by https://github.com/OpenAPITools/openapi-generator/pull/13195
- I think the fix is partial, and does not support request bodies that are not wrapped in an object
- Fixed by https://github.com/OpenAPITools/openapi-generator/pull/13195
Note that using the workaround --type-mappings='set=Array' as suggested in some related issues does NOT work, it results in a type-checking error.
The workaround I have to use for now is to wrap the request body in an object.
Then Array.from is used in the generated code.
But it adds an unnecessary layer in the model.
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
Use the provided OpenAPI YAML and Docker generation command to reproduce the issue, then inspect the generated TypeScript client's DefaultApi.addPetRaw request-body handling. Done means a direct Set request body is serialized as a JSON array rather than {}, while wrapped request bodies continue to work.
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
- 48/100