-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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] Generating incorrect types #3869
Comments
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
can you explain why it should be |
First,
It should be The easiest way to see what type it should be is to look at the code. If we look at the return statement from the function starting at line 182: return new runtime.JSONApiResponse(response, (jsonValue) => ListFromJSON(jsonValue)); We can see that the parameter to |
@keean can you try with the latest version |
4.1.1 seems to have the same problem, but less serious because it at least compiles (with warnings). However the client code has to treat the value as a "List" because it has this interface: export interface List {
/**
*
* @type {number}
* @memberof List
*/
start?: number;
/**
*
* @type {Array<object>}
* @memberof List
*/
hits?: Array<object>;
/**
*
* @type {number}
* @memberof List
*/
total?: number;
/**
*
* @type {number}
* @memberof List
*/
size?: number;
} It is clearly not an I don't really understand why TypeScript allows this, it seems to be a limitation in the type checking, effectively it allows:
The type really is not an Array at all. My code works now without hand editing, so I am not worried about this, but you should be worried about this because a type is completely wrong and all it would take would be one dereference to crash at runtime. Is there a deeper problem here? |
Have just discovered this is an Error in production mode, it's a warning only for development compilation, so it is important to me to get this resolved.
So I am back to hand editing the |
Would you like to implement a fix? |
If we add a console.log, we can see the actual type there is a
So we can clearly see the Promise resolves to a The ListFromJSON is returning the correct type. You can clearly see what it is meant to be looking at the swagger.json file: "200": {
"description": "Success",
"schema": {
"$ref": "#/definitions/list"
}
}, and the reference to "list": {
"properties": {
"hits": {
"items": {
"type": "object"
},
"type": "array"
},
"size": {
"type": "integer"
},
"start": {
"type": "integer"
},
"total": {
"type": "integer"
}
},
"type": "object"
}, So postEventSearch returns a single List object not an Array. I am happy to look at implementing a fix, but I have not looked at the OpenAPI generator code before. Where is the code that derives what the return type should be? In the template it gets referred to as |
@macjohnny I agree with @keean that it should be of type I think the bug is in AbstractTypeScriptClientCodegen: It defines that the type @keean would you be willing to test this and submit a PR if this fixes the bug? |
I am happy to test. As far as I am aware there is no generic https://www.typescriptlang.org/docs/handbook/basic-types.html And TypeScript advanced types: https://www.typescriptlang.org/docs/handbook/advanced-types.html But whatever language is used, unless OpenAPI defines it as a reserved word, I would expect the schema custom definition to be used, which means in a language where "List" is reserved, then the OpenAPI type would need to be mapped to "ApiList" or some other identifier. I would assume that an OpenAPI sphema referencing an undefined "List" object would fail to validate? |
"List" is not an OpenAPI type from what I can tell. |
So if I comment out that line, it generates the correct types. However I still have to make a manual change to get it to compile without error, that I might as well mention here, I have to replace GlobalFetch with WindowOrWorkerGlobalScope in src/runtime.ts |
An option to generate code for typescript 3+ was introduced, which should fix that |
@macjohnny oh finally, you saved my life. An option for ts3+.. how long I've been searching for that just to switch to newest ts version 3.7. Thanks! |
It it possible to fix this compilation error also ?
Thanx Sami |
@dngconsulting yes, I would be happy to review your pull-request. |
@macjohnny here we go => #4711 hoping that the merge will be as quick as your answer 😉 |
Cannot find name 'GlobalFetch'. TS2304 We are using typescript-fetch to generate models. Can you please confirm which release has the fix? |
@Bakkiyaraj A general workaround is to replace |
@janniclas I'm confused: shouldn't TS3+ be the default? |
Related: #9973 |
I am experiencing the typescript error due to |
When using openapi-generator to generate a typescript-fetch API from this specification:
openapi-generator generate -i https://api.demo.kaizenep.com/swagger.json -g typescript-fetch -o kaizen-api
The generated code has incorrect types where "List" is referenced. For example line 182 and 219 of output apis/EventsApi.ts
This should be:
this is using openapi-generator 4.0.3
The text was updated successfully, but these errors were encountered: