Comments (9)
Hey! Thanks a lot for that explanation, it really helped me!
from tnc-scully.
Not working for me
from tnc-scully.
Works great even with the latest version of nestJS (v7.6). Here is a full example which doesn't require putting @ApiExtraModels()
on every controller (when using NestJS Swagger CLI Plugin):
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)}
}
}
}
]
}
})
);
};
from tnc-scully.
@hakimio ListResponse is missing to generate the API.
from tnc-scully.
@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?
from tnc-scully.
@hakimio No error, only the result model is generated without the wrapper class with count etc.
from tnc-scully.
@markus2610 Try the @ApiListResponse()
decorator I have posted. It works as intended.
from tnc-scully.
Also, for those having issue with the original solution, remember to add @ApiExtraModels(PaginatedDto, UserDto)
decorator to your controller class.
from tnc-scully.
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 @nestjs/swagger": "^5.0.9"
and "swagger-ui-express": "^4.1.6"
.
For example, a controller method returning a paginated list of User
entities, decorated with ApiPaginatedResponse(User)
, displayed the following error message at the top of the Swagger UI: Could not resolve reference: #/components/schemas/User. The response value for my endpoint (which should also include a count
property) was erroneously displayed as the following, and User should appear as an object not a string:
"data": [
"string"
]
My entities and DTO's are named per NestJS conventions as required by the default configuration of the nestjs cli plugin from @nestjs/swagger
.
Other features implemented by the plugin seem to work.
To fix/workaround, I simply added ApiExtraModels(model)
to the applyDecorators()
args in @hakimio's decorator implementation. Hopefully that helps anyone who also wants to avoid having to remember to decorate their controller classes or methods with @ApiExtraModels()
.
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 ApiExtraModels()
, only the entity class.
from tnc-scully.
Related Issues (11)
- blog/add-utterances-comments-scully HOT 4
- blog/angular-recaptcha-v2 HOT 3
- blog/customizing-angular-ui-library HOT 1
- blog/deploy-scully-netlify HOT 1
- reporting `could not parse static page content` errors in your hosted page https://nartc.me/blog HOT 1
- RSS Feed HOT 3
- blog/typed-mat-cell-def HOT 2
- blog/nestjs-typegoose HOT 3
- blog/macos-dev-setup HOT 1
- blog/automapper-nestjs HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tnc-scully.