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

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.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 21 (9 by maintainers)

Most upvoted comments

Any update in 2023? How is anyone using this generator if a simple application/x-www-form-urlencoded completely breaks it…

thanks again to @someone1 for fixing this

@macjohnny my mistake. This is resolved.