OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-fetch] Schema with type array generates undefined function

Open
#3,278 21 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript 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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

I'm generating a typescript-fetch client using OpenAPI Generator 4.0.2. After running the generator, function stringToJSON is included in the output, but it is not defined.

openapi-generator version

4.0.2

OpenAPI declaration file content or url
{
  "swagger": "2.0",
  "info": {
    "description": "MyApi",
    "title": "MyApi",
    "version": "1.0.0"
  },
  "basePath": "/v1",
  "schemes": ["http"],
  "paths": {
    "/test": {
      "put": {
        "consumes": ["application/json"],
        "produces": ["application/json"],
        "parameters": [
          {
            "in": "body",
            "name": "body",
            "required": true,
            "schema": {
              "items": {
                "type": "string"
              },
              "type": "array"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  }
}

Command line used for generation
node_modules/.bin/openapi-generator generate -g typescript-fetch -i ./openapi.json -o ./src
Steps to reproduce

I've created a gist that should help to reproduce the issue.

git clone https://gist.github.com/gk4m/67045978063c7e2caf97fc8725620d62 bug-report
cd bug-report
yarn install
yarn generate
yarn test

You should see the following output:

src/apis/DefaultApi.ts:44:46 - error TS2304: Cannot find name 'stringToJSON'.

44             body: requestParameters.body.map(stringToJSON),

The source of the error can be found in src/apis/DefaultApi.ts which contains the following:

export interface TestPutRequest {
    body: Array<string>;
}

/**
 * no description
 */
export class DefaultApi extends runtime.BaseAPI {

    /**
     */
    async testPutRaw(requestParameters: TestPutRequest): Promise<runtime.ApiResponse<void>> {
        if (requestParameters.body === null || requestParameters.body === undefined) {
            throw new runtime.RequiredError('body','Required parameter requestParameters.body was null or undefined when calling testPut.');
        }

        const queryParameters: runtime.HTTPQuery = {};

        const headerParameters: runtime.HTTPHeaders = {};

        headerParameters['Content-Type'] = 'application/json';

        const response = await this.request({
            path: `/test`,
            method: 'PUT',
            headers: headerParameters,
            query: queryParameters,
            body: requestParameters.body.map(stringToJSON),
        });

        return new runtime.VoidApiResponse(response);
    }

    /**
     */
    async testPut(requestParameters: TestPutRequest): Promise<void> {
        await this.testPutRaw(requestParameters);
    }

}
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/1877

Suggest a fix

Function stringToJSON should be added to generated file.

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

Reproduce the issue with the provided OpenAPI declaration and the openapi-generator command, then inspect the generated src/apis/DefaultApi.ts where stringToJSON is referenced but undefined. Trace the typescript-fetch generator entry point or templates responsible for array request bodies, and verify completion with yarn test using the supplied reproduction steps.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.