-
Notifications
You must be signed in to change notification settings - Fork 12
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
blog/nestjs-swagger-generics #12
Comments
Hey! Thanks a lot for that explanation, it really helped me! |
Not working for me |
Works great even with the latest version of nestJS (v7.6). Here is a full example which doesn't require putting import {ApiOkResponse, getSchemaPath} from '@nestjs/swagger';
import {applyDecorators, Type} from '@nestjs/common';
export class ListResponse<T> {
count: number;
next?: string;
previous?: string;
results: T[];
}
export const ApiListResponse = <TModel extends Type<unknown>>(model: TModel) => {
return applyDecorators(
ApiOkResponse({
schema: {
allOf: [
{
properties: {
count: {
type: 'number'
},
next: {
type: 'string',
nullable: true
},
previous: {
type: 'string',
nullable: true
},
results: {
type: 'array',
items: {$ref: getSchemaPath(model)}
}
}
}
]
}
})
);
}; |
@hakimio ListResponse is missing to generate the API. |
@markus2610 Not sure I understand what issue you are having. Can you show how you are using the decorator and what error you are getting? |
@hakimio No error, only the result model is generated without the wrapper class with count etc. |
@markus2610 Try the |
Also, for those having issue with the original solution, remember to add |
Great article, and thanks for the code snippet @hakimio. I also experienced an error like @markus2610 with the decorator code from @hakimio on a project running For example, a controller method returning a paginated list of "data": [
"string"
] My entities and DTO's are named per NestJS conventions as required by the default configuration of the nestjs cli plugin from Other features implemented by the plugin seem to work. To fix/workaround, I simply added An example of the fixed (expected) result: {
"count": 0,
"data": [
{
"uuid": "string",
"email": "string",
"name": "string",
}
]
} Interestingly I didn't have add my generic Pagination DTO via |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
How to generate Generics DTOs with nestjs/swagger | Chau Tran
nestjs/swagger provides consumers with enough tools to generate proper OpenAPI specs for Generics. Although, it is not straight-forward, yet it is possible.
https://nartc.me/blog/nestjs-swagger-generics
The text was updated successfully, but these errors were encountered: