rtk-incubator / rtk-query-codegen

93 stars 13 forks source link

Code generation fails with an enum #37

Closed faragos closed 2 years ago

faragos commented 3 years ago

When I try to generate an APi with an enum in it it fails. Example enum in a OpenAPI.json:

...
    "ExStatus": {
        "enum": [
          0,
          1,
          2
        ],
        "type": "integer",
        "format": "int32"
      },
...

My call:

npx @rtk-incubator/rtk-query-codegen-openapi --file ./src/gen/TimeTrack.api.generated.ts --hooks ./api/TimeTrackAPI.json "--baseUrl" "https://localhost:5001"

The error:

TypeError: s.replace is not a function
    at escapeString (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:17131:18)
    at escapeNonAsciiString (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:17136:13)
    at Object.getLiteralText (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:14091:34)
    at getLiteralTextOfNode (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:104601:23)
    at emitLiteral (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:102107:24)
    at pipelineEmitWithHint (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:101919:32)
    at pipelineEmitWithComments (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:105009:17)
    at pipelineEmit (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:101547:13)
    at emitExpression (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:101532:20)
    at emitLiteralType (C:\Users\men\AppData\Roaming\npm-cache\_npx\2104\node_modules\@rtk-incubator\rtk-query-codegen-openapi\node_modules\typescript\lib\typescript.js:102505:13)

EDIT: it looks like it only happens with int enums :)

msutkowski commented 3 years ago

@faragos Can you please provide a copy of TimeTrackAPI.json?

faragos commented 3 years ago

I put the JSON into a .txt because GitHub doesn't support .json files.

TimeTrackAPI.txt

guvenkaranfil commented 3 years ago

Is there any update on issue? I'm getting the same error, here's the json file I'm trying to generate on https://yawa-pick-test.azurewebsites.net/swagger/v1/swagger.json

phryneas commented 3 years ago

I'm sorry, I'm just hopelessly swamped with other work :disappointed:

I have plans on getting the open issues some attention and get this whole repo over into https://github.com/reduxjs/redux-toolkit, but at the moment it seems like there are always coming things in between :confused:

guvenkaranfil commented 3 years ago

Thank you for quick response @phryneas . Could you please give me a how to debug this issue. Maybe I can send a fix about it. Is there any contribution or any guideline that i can follow

phryneas commented 3 years ago

The error message seems very strange to me as for me it reads like that error seems to appear during printing the code (I assume somewhere in the call stack it will finally end up in https://github.com/rtk-incubator/rtk-query-codegen/blob/next/src/generate.ts#L138, but the trace is cut off so I can't say for sure), which would mean that somewhere an AST node has gotten some seriously weird content.

I would honestly not really know where to start other than connecting the node debugger and breaking on exception, in the hopes of finding the exact AST node that's failing that way.

sampaioxsamuel commented 3 years ago

any updates?

theZieger commented 3 years ago

@phryneas if you could give me a hint how to start a debugging session in vscode with my own openapi.json than I could take a look into how to fix this. I'm not a big pro when it comes to nodejs debugging, yet.

phryneas commented 3 years ago

@theZieger there is a .vscode/launch.json in this project, although I think the project has since seen some refactoring. You theoretically should be able to open src/bin/cli.ts, set a breakpoint and then just do a "Run and Debug" -> "Current TS File".

ashnewport commented 3 years ago

I encountered this same issue when trying to convert one of our APIs. For completeness the API docs are from C# generated by Swashbuckle to Swagger and from there we grab the OpenAPI JSON spec.

So I spent a while on this and I think I've found the issue for this which is related specifically to int enums, e.g. "enum": [0,1,2].

RTKQ generate.ts has a method generateEndPoint() which calls out to another library's (oazapfts) method, getTypeFromResponse on line 188. On line 404 of the oazapfts library there is a call to createStringLiteral from the TypeScript library. It is this last method, createStringLiteral, which is expecting a string value as per the implementation.

So the RTKQ codegen tool fails because it supplies a non string value, which in this case is an int.

Not sure if the fix here is something RTK wants to handle by casting from int to string or if responsibility lies in one of the dependencies. I think it may be eventually situated with the oazapfts repo which already has an issue https://github.com/cellular/oazapfts/issues/45. Although it might be something fixed sooner if implemented in this codegen repo.

A fix that enabled the me to verify the codegen tool was able to complete was by modifying .\node_modules\oazapfts\lib\codegen\generate.js:351 to convert the number to a string with this replacement line: return typescript_1.factory.createLiteralTypeNode(typescript_1.factory.createStringLiteral(String(s)));. Not a great solution (for short or long term because node_modules shouldn't be modified) but proves the point.

I was able to arrive at this point by tinkering and eventually realised that commenting out ...apiGen['aliases'], got the tool to function. From there is was down a rabbit hole to see where it leads.


@theZieger it's not elegant but my method of debugging was to modify .vscode/launch.json on line for args to the following "args": ["${relativeFile}", "test/fixtures/petstore.json", "--file", "test/fixtures/generated.ts", "-h"],. You could change the file references to something that suits better, but for ease I just replaced the petstore.json file contents. When launching/restarting the debugger make sure you have open and focused the file src/bin/cli.ts as @phryneas mentioned. You should hit breakpoints now.

phryneas commented 3 years ago

@ashnewport great analysis! I think this should be fixed over in oazapfts - could you please file a PR over there and let me know when it is merged?

theZieger commented 3 years ago

@ashnewport thanks for the digging and explanation around debugging. Since I discovered the issue we changed our openapi to use strings instead of integers. Would also prefer seeing this fixed in the library instead having a workaround that must be removed later on again.

ashnewport commented 3 years ago

@phryneas and @theZieger this has been fixed in the oazapfts repo and the PR is now merged, https://github.com/cellular/oazapfts/pull/169

phryneas commented 3 years ago

@phryneas and @theZieger this has been fixed in the oazapfts repo and the PR is now merged, cellular/oazapfts#169

Great, I'll bump the dependency in the upcoming 1.0 beta

phryneas commented 2 years ago

The codegen is now published as @rtk-query/codegen-openapi and the code has moved over to the Redux Toolkit monorepo.

As usage has changed as well, please read the new documentation.

The new release references the new version of oazapfts, so this should be resolved.