Skip to content
Advertisement

@nestjs/swagger: How to add schema without @ApiOkResponse decorator?

I have two DTOs. UsersDTO and UserDTO. UsersDTO are showing in swagger, because I have an endpoint to get users list, and for that endpoint I have

JavaScript

In UsersDTO I’m using UserDTO using $ref. UsersDTO looks like

JavaScript

But it doesn’t work in swagger. Swagger show [string] as a value for the items.

Is there any way to make it work?

Advertisement

Answer

Looks like you dont have another endpoint that is using UserDTO in @ ApiOkResponse, like

JavaScript

It means that the Swagger cant make references between the Schemas.

If you add another endpoint, for example, to get a single user, and use provided @ApiOkResponse, it will work.

But, if you don’t need that endpoint, you also can provide DTO Schemas to Swagger on the setup stage in the following way

JavaScript

You can use it to add the Schemas that are not used in the endpoints directly using @ApiOkResponse decorator. extraModels is an aray with needed Schemas

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement