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

Created on 4 Jul 2019  路  12Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] Have you search for related issues/PRs?
  • [x] 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.

TypeScript Bug

All 12 comments

馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

cc @TiFu

@gk4m would you like to give it a try and implement a fix? see also https://github.com/OpenAPITools/openapi-generator/pull/2453

Sorry for the annoying bump, but any movement on this one? @wing328 @macjohnny

@dalinarkholin would you like to implement a fix for that? i can point you to the relevant files.

cc @Place1 @someone1

@macjohnny Was this included in 4.1.0? I still see the same behavior. stringToJSON function is undefined.

@dalinarkholin this should be inlcuded in 4.1.0
Can you post the relevant part of your spec?

@macjohnny my mistake. This is resolved.

thanks again to @someone1 for fixing this

I'm still seeing this on 4.3.1. Here's a gist with a schema and the generated models/Listen.ts, invoked as npx @openapitools/openapi-generator-cli generate -i openapi.json -g typescript-fetch -o gen.

https://gist.github.com/markspolakovs/afd0bd3cdf6955a90fc1e75f60e166b1

@markspolakovs would you like to fix this issue?

Was this page helpful?
0 / 5 - 0 ratings