OpenAPITools / OpenAPITools/openapi-generator

Regarding optional requestParams getting added to Body section during code generation

Open
#6,008 2 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Version of openapigenerator : v4.3.0
Issue description: In my openapi file if the entry of requestparams is like this
/v1/request-type/list:
post:
tags:
- request-type
summary: Testing
description: Testing
operationId: addRequestTypeList
requestBody:
content:
'/':
schema:
type: array
items:
$ref: '#/components/schemas/RequestType'

so the corresponding code that is getting gererated is like below
export interface AddRequestTypeListRequest {
requestType?: Array;
}
const response = await this.request({
path: /v1/request-type/list,
method: 'POST',
headers: headerParameters,
query: queryParameters,
body: requestParameters.requestType.map(RequestTypeToJSON),
});
which gives error during compile time as
(property) AddRequestTypeListRequest.requestType?: RequestType[] | undefined
Object is possibly 'undefined'.

Ideally the body content should be created like this optional to resolve the above error
body: requestParameters.requestType.?map(RequestTypeToJSON),

Also checked the github code for the same. Can we add this provision or is there any other way

https://github.com/OpenAPITools/openapi-generator/blob/v4.3.0/modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache
line no 255
line no 21

Can you help me to resolve this issue

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache at the referenced lines and inspect how optional request parameters are emitted into generated request bodies. Reproduce the v4.3.0 TypeScript fetch output from the issue and verify that the generated code compiles without an undefined optional-parameter error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.