OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JS] “this” is undefined inside map function

Open
#7,910 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

Description

There depending on how a method is called, this may be undefined. This specifically happens when buildCollectionParam is called statically instead of from an instance.

Based on the Array.prototype.map() spec we can/should pass this as an arg to avoid this issue.

openapi-generator version

4.3.1 ... doesn't look to be a regression. Just missing handling

Suggest a fix
switch (collectionFormat) {
      case 'csv':
        return param.map(this.paramToString, this).join(',');
      case 'ssv':
        return param.map(this.paramToString, this).join(' ');
      case 'tsv':
        return param.map(this.paramToString, this).join('\t');
      case 'pipes':
        return param.map(this.paramToString, this).join('|');
      case 'multi':
        // return the array directly as SuperAgent will handle it as expected
        return param.map(this.paramToString, this);
      default:
        throw new Error('Unknown collection format: ' + collectionFormat);

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

Open modules/openapi-generator/src/main/resources/Javascript/ApiClient.mustache around lines 303-322 and inspect buildCollectionParam, focusing on its Array.prototype.map calls and the static-call case. Use the suggested map thisArg handling; done means the collection formats retain access to the instance context when buildCollectionParam is called statically.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.