question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to change body parameter name derived from @Body()

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I wrote a controller like this:

  @Post('/')
  @ApiImplicitBody({ name: 'body', type: MemberCreatePayloadApi })
  public async create(@Body() payload: MemberCreatePayloadApi): Promise<Member> {
  }

SwaggerModule produces the below api from my controller.

      "post": {
        "parameters": [
          {
            "name": "body",
            "required": true,
            "in": "body",
            "isArray": false,
            "schema": {
              "$ref": "#/definitions/MemberCreatePayloadApi"
            }
          },
          {
            "name": "MemberCreatePayloadApi",
            "required": true,
            "in": "body",
            "schema": {
              "$ref": "#/definitions/MemberCreatePayloadApi"
            }
          },
        ],

Expected behavior

@Body() makes a parameter named by argument type. But I want to change parameter name as I like.

# Expected output
      "post": {
        "parameters": [
          {
            "name": "body",
            "required": true,
            "in": "body",
            "isArray": false,
            "schema": {
              "$ref": "#/definitions/MemberCreatePayloadApi"
            }
          },
        ],

I try to modify explorer, the below code is expected to me but it may lose backward compatibility.

https://github.com/nestjs/swagger/blob/master/lib/explorers/api-parameters.explorer.ts#L90

- val => val.in === DEFAULT_PARAM_TOKEN || (val.name && val.in === 'body')
+ val => val.in === DEFAULT_PARAM_TOKEN || (val.in === 'body')

What is the motivation / use case for changing the behavior?

A client library swagger-js users should name request body parameter body.

Environment


@nestjs/core: 5.5.0
@nestjs/swagger: 2.5.1

Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
akkunchoicommented, Sep 12, 2019

@kamilmysliwiec Hi, I fixed this issue as above. Can I get your opinion?

2reactions
kamilmysliwieccommented, Oct 30, 2019

Added in the next version (to install run npm i @nestjs/swagger@next). Note: remember to update @nestjs/common, @nestjs/core and typescript as well to ensure that you’re using the latest versions.

Steps to migrate: https://github.com/nestjs/swagger/pull/355#issuecomment-547925879

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How can I change the parameter name in the request body?
I have this model 'LoginModel'. I use this class on the controller tho get the values from the body like this. So to...
Read more >
Describing Request Body - Swagger
The POST, PUT and PATCH requests can have the request body (payload), such as JSON or XML data. In Swagger terms, the request...
Read more >
Body - Multiple Parameters - FastAPI
First, of course, you can mix Path , Query and request body parameter ... BaseModel app = FastAPI() class Item(BaseModel): name: str description:...
Read more >
Parameter Binding in ASP.NET Web API - ASP.NET 4.x
In this example, Web API will use a media-type formatter to read the value of name from the request body. Here is an...
Read more >
Request Handling — Connexion 3.0.dev0 documentation
In the OpenAPI 3.x.x spec, the requestBody does not have a name. By default it will be passed in as 'body'. You can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found