Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
gk4m opened this issue Jul 4, 2019 · 21 comments · Fixed by #3517
Open
5 of 6 tasks

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

gk4m opened this issue Jul 4, 2019 · 21 comments · Fixed by #3517

Comments

@gk4m
Copy link

gk4m commented Jul 4, 2019

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

#1877

Suggest a fix

Function stringToJSON should be added to generated file.

@auto-labeler
Copy link

auto-labeler bot commented Jul 4, 2019

👍 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.

@macjohnny
Copy link
Member

cc @TiFu

@macjohnny
Copy link
Member

@gk4m would you like to give it a try and implement a fix? see also #2453

@dalinarkholin
Copy link

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

@macjohnny
Copy link
Member

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

@macjohnny
Copy link
Member

cc @Place1 @someone1

@dalinarkholin
Copy link

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

@macjohnny
Copy link
Member

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

@dalinarkholin
Copy link

@macjohnny my mistake. This is resolved.

@macjohnny
Copy link
Member

thanks again to @someone1 for fixing this

@markspolakovs
Copy link

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

@macjohnny
Copy link
Member

macjohnny commented Dec 7, 2020

@markspolakovs would you like to fix this issue?

@macjohnny macjohnny reopened this Dec 7, 2020
@markocevar
Copy link

Any workaround possible for this issue?

@wing328
Copy link
Member

wing328 commented Feb 18, 2021

@markspolakovs did you try the latest master or stable version v5.0.1?

What do the actual code and expected code look like?

@markspolakovs
Copy link

markspolakovs commented Feb 18, 2021

@wing328 Can still reproduce on v5.0.1, here's the input and output.

On line 40 the generator outputs syntactically invalid TypeScript ('boolean' only refers to a type, but is being used as a value here.). Will try master in a sec.

@ssalimit
Copy link

For me this happens when I use v5.0.0-beta3 onwards, but it looks fine when using v5.0.0-beta2. Is there any possible workaround?

@jacek-rzrz
Copy link

jacek-rzrz commented Jul 15, 2021

I'm seeing seemingly the same problem on v. 5.2.0 now:

JsonAPI spec:

...
"included" : {
  "type" : "array",
  "description" : "...",
  "items" : {
    "oneOf" : [ {
      "type" : "object"
    }, {
      "type" : "boolean"
    }, {
      "type" : "string"
    }, {
      "type" : "number"
    } ]
  }
}
...

Typescript type looks like that:

export interface V1BunnyBunnyId204409787 {
    included?: Array<object | boolean | string | number>;
}

And the generated fromJSON / toJSON functions are invalid:

return {
    'included': !exists(json, 'included') ? undefined : ((json['included'] as Array<any>).map(object | boolean | string | numberFromJSON)),
};

@simPod
Copy link
Contributor

simPod commented Jan 17, 2023

Has anyone found any workaround? 6.2.1 and I see a lot of undefined functions like stringToJSON etc.

@rohanrajpal
Copy link

Facing the same issue here, shall post a workaround tho if I find one

@Holt59
Copy link

Holt59 commented Aug 9, 2023

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

@wing328
Copy link
Member

wing328 commented Aug 11, 2023

If anyone would like to contribute a fix or sponsor a fix, please reply to let us know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.