openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [typescript-angular] DELETE method - TS2554: Expected 1-2 arguments, but got 3

Open constantant opened this issue 3 years ago • 26 comments

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

constantant avatar Nov 16 '21 08:11 constantant

I see the same issue here with ng 13.0.1 and generator 5.3.1-SNAPSHOT

eiswind avatar Dec 05 '21 09:12 eiswind

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 :)

PeterOeClausen avatar Dec 23 '21 02:12 PeterOeClausen

There is a PR for this here #10976

maxs-rose avatar Dec 23 '21 13:12 maxs-rose

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.

coder925 avatar Dec 28 '21 11:12 coder925

Duplicate of https://github.com/OpenAPITools/openapi-generator/issues/10975

amakhrov avatar Dec 29 '21 23:12 amakhrov

@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

cghislai avatar Jan 27 '22 15:01 cghislai

Hey guys,

link to comment. I do not have a requestBody but still have the same issue.

EE1234EE avatar Jan 30 '22 13:01 EE1234EE

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'

lfarci avatar Jan 30 '22 14:01 lfarci

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

maxs-rose avatar Jan 30 '22 17:01 maxs-rose

any updates regarding this issue?

pwiesinger avatar Feb 15 '22 15:02 pwiesinger

@pwiesinger a fix for this was merged in #10976 we are just waiting for a release that includes it

maxs-rose avatar Feb 15 '22 16:02 maxs-rose

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

Danielku15 avatar Feb 22 '22 07:02 Danielku15

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.

cghislai avatar Feb 22 '22 09:02 cghislai

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?

matteobaldini-quix avatar Feb 22 '22 14:02 matteobaldini-quix

Using version 5.3.0 still seems to work with a13 for us at least

maxs-rose avatar Feb 22 '22 15:02 maxs-rose

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);
}

chrisae avatar Mar 18 '22 12:03 chrisae

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.

Tasades avatar Mar 21 '22 16:03 Tasades

5.3.0

where did you get this version? 2.4.26 is the latest

yurakhomitsky avatar Apr 15 '22 10:04 yurakhomitsky

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.

mgol avatar Apr 15 '22 13:04 mgol

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 #10976 (comment) and my followup comment.

That's great but when this bug will be fixed and shipped via npm package?

yurakhomitsky avatar Apr 15 '22 13:04 yurakhomitsky

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.

mgol avatar Apr 15 '22 13:04 mgol

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.

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.

yurakhomitsky avatar Apr 15 '22 14:04 yurakhomitsky

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 avatar Oct 12 '22 21:10 paintcode

@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

amakhrov avatar Oct 12 '22 21:10 amakhrov

what version was this fixed in?

evanjmg avatar Oct 18 '22 11:10 evanjmg

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

amakhrov avatar Oct 18 '22 17:10 amakhrov

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.

sverbach-zuehlke avatar Feb 01 '24 14:02 sverbach-zuehlke