openapi-generator
openapi-generator copied to clipboard
[BUG] [typescript-angular] DELETE method - TS2554: Expected 1-2 arguments, but got 3
Description
It generates a wrong way to call the HttpClient.delete method
openapi-generator version
5.3.1-SNAPSHOT
OpenAPI declaration file content or url
"/v1/project/{projectId}": {
"delete": {
"tags": [
"Project"
],
"parameters": [
{
"name": "projectId",
"in": "path",
"required": true,
"schema": {
"type": "string",
"format": "uuid"
}
}
],
"responses": {
"200": {
"description": "Success"
}
}
}
}
Generation Details
return this.httpClient.delete<any>(`${this.configuration.basePath}/v1/project/${encodeURIComponent(String(projectId))}`,
null, // <-- the issue
{
context: localVarHttpContext,
responseType: <any>responseType_,
withCredentials: this.configuration.withCredentials,
headers: localVarHeaders,
observe: observe,
reportProgress: reportProgress
}
);
Steps to reproduce
Generate from the DELETE method definition
Suggest a fix
In case of the DELETE method do not provide the body payload
I see the same issue here with ng 13.0.1 and generator 5.3.1-SNAPSHOT
I'm seeing the same issue with:
Angular CLI: 13.1.2
Node: 16.13.1
Package Manager: npm 8.3.0
OS: win32 x64
I ran:
npx @openapitools/openapi-generator-cli generate -i ./src/api/swagger.json -g typescript-angular -o ./src/api
Also seems like the methods like get
, delete
and so on of the httpClient
of Angular 13.1.1 is only taking two arguments: The url
and options
:
https://github.com/angular/angular/blob/13.1.1/packages/common/http/src/client.ts#L941-L949
Once, it took a middle argument, body
, but that's a long time ago. - Even at 5.0.x, it was only 2 arguments: https://github.com/angular/angular/blob/5.0.x/packages/common/http/src/client.ts
So it seems like a bug :)
There is a PR for this here #10976
Suggest a fix In case of the DELETE method do not provide the body payload
Isn't the fix simply to use the options.body property instead (as suggested in #11172 )?
I'm actually in need of a body for DELETE.
Duplicate of https://github.com/OpenAPITools/openapi-generator/issues/10975
@coder925 Unfortunately, the specification does not allow a body for delete methods. See https://swagger.io/specification/#operation-object mentionning The requestBody is only supported in HTTP methods where the HTTP 1.1 specification RFC7231 has explicitly defined semantics for request bodies. In other cases where the HTTP spec is vague, requestBody SHALL be ignored by consumers.
and RFC7231 metionning A payload within a DELETE request message has no defined semantics;
~When fixing this for angular13, i tried to update the test spec to include a delete body so that i could test the generated code, but i got errors due to the invalid spec.~ I think I edited a former spec I will give it another try
Same issue as @EE1234EE , I have a delete method without any request body and still get this issue.
Here is my specification:
delete:
summary: Deletes a user by ID.
operationId: 'deleteById'
tags:
- User
parameters:
- name: userId
in: path
required: true
description: This is the user identifier.
schema:
type: integer
format: int64
responses:
'204':
description: The user has been successfuly deleted.
'404':
description: The user could not be deleted because it doesn't exist.
content:
application/json:
schema:
$ref: './resources/shared/ServiceError.yaml'
The generator is attempting to add the request body whether it is in your specification or not that is why you are seeing the issues
any updates regarding this issue?
@pwiesinger a fix for this was merged in #10976 we are just waiting for a release that includes it
There was a long ongoing discussion about delete bodies over here: https://github.com/OAI/OpenAPI-Specification/issues/1801 The conclusion was: They should be supported and the 3.1 spec was updated accordingly. https://github.com/OAI/OpenAPI-Specification/pull/1937
Therefore I would vote to "revert the revert" and add proper support for bodies in delete methods. 😁
I am attempting to do so. What prompted the revert was that I couldn't produce a valid test spec with a delete body, but I hadn't noticed it was still using swagger v2. Now I'm in the process of making sure the delete body is tested across all generators supporting v3, but pull requests are held in the queue waiting for review. The fix itself was already committed in an abandoned pr here, it is quite comical the hurdle it causes with a fix somewhere in the history. Anyway sorry for that it appeared to me the revert was the quickest way to fix the issue until proper testing was in place.
So just checking, a release with the proposed fixes isn't out yet? Right? If so how can we circumvent this issue while using ng13 and openapi generator?
Using version 5.3.0 still seems to work with a13 for us at least
With version 5.4.0 I'm also getting this error.
Using the decorators from @nestjs/swagger
one of my delete functions looks like this:
@Delete(':id')
@ApiOkResponse({
type: Boolean,
})
@ApiParam({ name: 'id' })
remove(@Param('id') id: string) {
return this.projectsService.remove(id);
}
We overided the api.service.mustache as quickfix that uses the options.body for deletes. Its a bit dirty but it works for us for now: api.service.mustache.zip
We use version 5.3.1 I hope it helps.
5.3.0
where did you get this version? 2.4.26 is the latest
5.3.0
where did you get this version? 2.4.26 is the latest
@yurakhomitsky You're talking about the version of @openapitools/openapi-generator-cli
and the others about the version of the JAR that it uses. You can specify it in openapitools.json
. See https://github.com/OpenAPITools/openapi-generator/pull/10976#issuecomment-1085204909 and my followup comment.
5.3.0
where did you get this version? 2.4.26 is the latest
@yurakhomitsky You're talking about the version of
@openapitools/openapi-generator-cli
and the others about the version of the JAR that it uses. You can specify it inopenapitools.json
. See #10976 (comment) and my followup comment.
That's great but when this bug will be fixed and shipped via npm package?
The bug is not in the npm package but in the external dependency it downloads. See the README:
The first time you run the command openapi-generator-cli the last stable version of OpenAPITools/openapi-generator is downloaded by default.
That version is saved in the file openapitools.json.
The fix is in 6.0.0-beta
but even when they release it as stable it will not get autofixed for you because your openapitools.json
file will contain a reference to an older version that you need to update by yourself. And you can already do that today byt pointing it to 6.0.0-beta
. This version works for me without issues.
The bug is not in the npm package but in the external dependency it downloads. See the README:
The first time you run the command openapi-generator-cli the last stable version of OpenAPITools/openapi-generator is downloaded by default. That version is saved in the file openapitools.json.
The fix is in
6.0.0-beta
but even when they release it as stable it will not get autofixed for you because youropenapitools.json
file will contain a reference to an older version that you need to update by yourself. And you can already do that today byt pointing it to6.0.0-beta
. This version works for me without issues.
Sounds like I will have to push the openapitools.json
file to the git to keep the 6-beta
version because we didn't keep it before the code was generated every time in the pipeline.
Also, the problem doesn't appear on this version of the package @openapitools/openapi-generator-cli": "^1.0.18-4.3.1"
I guess it generates the code of the Angular 11 version.
Has this bug been resolved? I just upgraded from 5.4.0 (where I saw the bug) to 6.2.0, and so far it seems to be okay.
@paintcode Yes it has been fixed.
This bug report effectively duplicates https://github.com/OpenAPITools/openapi-generator/issues/10975 and as such should have been closed together with that one. @wing328
what version was this fixed in?
Broken compilation was fixed in 6.0.0 (but still no body support in DELETE) Full support for body in DELETE landed in 6.2.0
Ran into the same issues. Out of the blue I couldn't generate code for DELETE endpoints anymore. Updating to the latest @openapitools/openapi-generator-cli
also did not help.
Solved it by setting the 'ngVersion' additional-property to '12.0.0' (although the project is on v14) in the command:
openapi-generator-cli generate -i http://localhost:XXXX/.../swagger.json -g typescript-angular -o src/generated/api --additional-properties=withInterfaces=true --additional-properties=ngVersion=12.0.0
setting it to any major version above "12.0.0" will break the code generation.