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.

using @body() in routes, 'examples' of '@apiOperation()' are overwritten in DTO's 'apiProperty()' example

See original GitHub issue

I’m submitting a…


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

Current behavior

Hello!

If using @nestjs/swagger to describe examples in requestBody, it occur weird action.

When a default example is specified in the DTO and examples are added in ApiOperation

When the @Body() annotation is used in the route, the default example specified in the DTO is displayed in the swagger, not the added examples.


export class User {
    @ApiProperty({
        name: "name",
        example: "default foo name example"
    })
    name: string
    @ApiProperty({
        name: "type",
        enum: ["FOO", "BAR"],
        example: "BAR"
    })
    type: "FOO" | "BAR"
    @ApiProperty({
        name: "age",
        example: 10
    })
    age: number
}


@ApiExtraModels(User)
@Controller()
export class AppController {

    @ApiOperation({
        description: "with body annotation",
        requestBody: {
            description: "User body Example",
            required: true,
            content: {
                "application/json": {
                    schema: { $ref: getSchemaPath(User) },
                    examples: {
                        "Foo User": {
                            value: {
                                name: "foo name diffrent",
                                age: 20,
                                type: "FOO"
                            } as User,
                            description: "This is Foo user Example",
                        },
                        "Bar User": {
                            value: {
                                name: "bar name diffrent",
                                age: 30,
                                type: "BAR"
                            } as User,
                            description: "This is Bar user Example",
                        }
                    }
                }
            }
        }
    })
    @Post("/with")
    withBodyAnnotation(@Body() payload: User) {
        return payload
    }

    @ApiOperation({
        description: "with body annotation",
        requestBody: {
            description: "User body Example",
            required: true,
            content: {
                "application/json": {
                    schema: { $ref: getSchemaPath(User) },
                    examples: {
                        "Foo User": {
                            value: {
                                name: "foo name diffrent",
                                age: 20,
                                type: "FOO"
                            } as User,
                            description: "This is Foo user Example",
                        },
                        "Bar User": {
                            value: {
                                name: "bar name diffrent",
                                age: 30,
                                type: "BAR"
                            } as User,
                            description: "This is Bar user Example",
                        }
                    }
                }
            }
        }
    })
    @Post("/without")
    withOuthBodyAnnotation(@Req() request: Request) {
        return request.body
    }
}

Expected behavior

I think that Even with the @Body() annotation, if “examples” are specified, “examples” should appear in swagger.

Currently, if need to apply swagger examples, using @Req() request:Request // req.body instead of @Body().

If these actions are intended, i will close this issue.

Minimal reproduction of the problem with instructions

https://github.com/si-hyeon-ee/reports-nestjs-swagger-body-examples

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

Environment


Nest version: "^7.5.1"

 
For Tooling issues:
- Node version: v14.14.0  
- Platform:  Windows 

Others: "@nestjs/swagger": "^4.7.5",

some Documentation : https://www.viralpatel.net/openapi-multiple-examples/

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
yura-lovecommented, May 25, 2021

Project member is reviewing to can include this feature in v8 release.

Is there anything blocking the PR from @konne to be merged? This functionality would be really useful for us. Please let me know if I can support.

1reaction
konnecommented, Mar 22, 2021

@si-hyeon-ee I got it working with ApiBody if I change the following lines: https://github.com/nestjs/swagger/blob/53800aed1c3964d5edfd867ba116f46ccee9c4fd/lib/swagger-explorer.ts#L351

const keysToRemove = ['schema', 'in', 'name','examples'];
    document.root.requestBody = {
      ...omit(requestBody, keysToRemove),
      ...this.mimetypeContentWrapper.wrap(consumes, pick(requestBody, ['schema','examples']))
    };

and

    @ApiBody({
        schema: { 
            $ref: getSchemaPath(User),
        },
        examples:
        {
            'test': {
                value: {
                    name: "EXAMPLE APIBody beside schema",
                    age: 20,
                    type: "BAR"
                } as User,
                description: "This is Bar user Example",
            }
        }
    } as any)

I will try to create now a PR and change the Typings so this ugly as any is not necessary.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid to write `@ApiProperty()` in each dto for NestJs ...
I'm researching the way on how to avoid to specify @ApiProperty() in each dto. I know ...
Read more >
Some DTO won't show in the Swagger Schema #954 - GitHub
After playing around with it. It turns out that Swagger Schema only shows the DTO that are wrapped within "@Body()" decorate. Is that...
Read more >
Describing Parameters - Swagger
Each parameter has name , value type (for primitive value parameters) or schema (for request body), and optional description . Here is an...
Read more >
Using a dynamic DTO property in a NestJS API
Below I show how to use the discriminator property on the @Type decorator to tell class-transformer how we want to create the class...
Read more >
Integrating Swagger with a NestJS Application | by Anukriti Garg
Use the @ApiProperty() decorator above each property in your schema/dto that is required in the object and @ApiPropertyOptional() for a ...
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