I guess this can be seen more as a reference as this post comes up when looking for instructions for Swagger/OpenAPI.
I have set up an example repo which shows the basic usage.
You can find it here: https://gitlab.com/WaldemarLehner/nestjs-swagger-example
Missing Summary
Use the @ApiOperation
-Decorator to define a Endpoint-Description.
@ApiOperation({description: "This is the main Description of an Endpoint."})
Want to manually add request schema
First of all, note that you have a GET-Endpoint. As a result any request towards that endpoint cannot have a request body.
So.. assuming you use a HTTP Method that allows for a Request-Body (like POST), you can use the @ApiBody
-Decorator.
Here you can define the Body-Summary, a Schema (using an OpenAPI-Schema Object), or a Type (Schema is inferred from the Class and its Decorators).
@ApiBody({
type: PostHelloBodyDTO,
description: "The Description for the Post Body. Please look into the DTO. You will see the @ApiOptionalProperty used to define the Schema.",
examples: {
a: {
summary: "Empty Body",
description: "Description for when an empty body is used",
value: {} as PostHelloBodyDTO
},
b: {
summary: "Hello Body",
description: "Hello is used as the greeting",
value: {greeting: "Hello"} as PostHelloBodyDTO
}
}
})
Further Reference
Using the following Decorations will result in a Swagger-Document as shown below.
@ApiOperation({description: "This is the main Description of an Endpoint."})
/// Request Documentation
@ApiParam({
name: "name",
description: "This Decorator specifies the documentation for a specific Parameter, in this case the <b>name</b> Param.",
allowEmptyValue: false,
examples: {
a: {
summary: "Name is Pete",
description: "Pete can be provided as a name. See how it becomes a selectable option in the dropdown",
value: "Pete"
},
b: {
summary: "Name is Joe",
value: "Joe"
}
}
})
@ApiQuery({
name: "useExclamation",
description: "This is the description of a query argument. In this instance, we have a boolean value.",
type: Boolean,
required: false // This value is optional
})
@ApiBody({
type: PostHelloBodyDTO,
description: "The Description for the Post Body. Please look into the DTO. You will see the @ApiOptionalProperty used to define the Schema.",
examples: {
a: {
summary: "Empty Body",
description: "Description for when an empty body is used",
value: {} as PostHelloBodyDTO
},
b: {
summary: "Hello Body",
description: "Hello is used as the greeting",
value: {greeting: "Hello"} as PostHelloBodyDTO
}
}
})
/// Response Documentation
@ApiOkResponse({
description: "This description defines when a 200 (OK) is returned. For @Get-Annotated Endpoints this is always present. When, for example, using a @Post-Endpoint, a 201 Created is always present",
schema: {
type: "string",
example: "Hello, Pete!"
// For instructions on how to set a Schema, please refer to https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schema-object-examples
}
})
@ApiBadRequestResponse({
description: "This description is for a 400 response. It is returned when additional query params are passed, or when the <b>useExclamation</b>-Argument could not be parsed as a boolean."
})
@ApiResponse({
status: 417,
description: "One can also provided a Status-Code directly, as seen here"
})
@Post(":name")
public postHello(...){...}
Result