using @body() in routes, 'examples' of '@apiOperation()' are overwritten in DTO's 'apiProperty()' example
See original GitHub issueI’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:
- Created 3 years ago
- Reactions:2
- Comments:13 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Project member is reviewing to can include this feature in v8 release.
@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
and
I will try to create now a PR and change the Typings so this ugly as any is not necessary.