OpenAPITools / OpenAPITools/openapi-generator

[BUG][Typescript] `contentType` name clash in api file

Open
#11,365 0 comments 0 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

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

If Content-Type is declared as a path parameter there is a naming clash in the generated api method function - because contentType is statically declared already in the template file here

// contentType is expected as a function argument
public async uploadMedia(accountId: string, mediaId: string, body: HttpFile, contentType?: string, cacheControl?: string, _options?: Configuration): Promise<RequestContext> {


// The function argument is passed in here - within the same function - causing a naming conflict
requestContext.setHeaderParam("Content-Type", ObjectSerializer.serialize(contentType, "string", ""));

// A different contentType const is declared here
// Body Params
const contentType = ObjectSerializer.getPreferredMediaType([
    "application/octet-stream"
]);
requestContext.setHeaderParam("Content-Type", contentType);
const serializedBody = ObjectSerializer.stringify(
    ObjectSerializer.serialize(body, "HttpFile", ""),
    contentType
);

Generates this error on build:

src/apis/MediaApi.ts:147:82 - error TS2300: Duplicate identifier 'contentType'.

147     public async uploadMedia(accountId: string, mediaId: string, body: HttpFile, contentType?: string, cacheControl?: string, _options?: Configuration): Promise<RequestContext> {

src/apis/MediaApi.ts:187:15 - error TS2300: Duplicate identifier 'contentType'.

187         const contentType = ObjectSerializer.getPreferredMediaType([
                  ~~~~~~~~~~~

src/apis/MediaApi.ts:190:55 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

190         requestContext.setHeaderParam("Content-Type", contentType);
                                                          ~~~~~~~~~~~

src/apis/MediaApi.ts:193:13 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

193             contentType
openapi-generator version

5.4.0-SNAPSHOT

OpenAPI declaration file content or url

You can find a sample spec here

Generation Details

Using the experimental Typescript client generator

Steps to reproduce

Generate an SDK with the provided sample spec and run npm install

Related issues/PRs

N/A

Suggest a fix

Could easily be resolved by renaming the declared contentType const - but not sure what that would break, if anything, or what convention to follow to rename

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 with modules/openapi-generator/src/main/resources/typescript/api/api.mustache at the linked contentType declaration, then generate the SDK from the provided sample spec and run npm install. Done means the generated MediaApi.ts builds without the duplicate contentType identifier or related type errors while preserving the expected headers and body serialization.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.