Giter Club home page Giter Club logo

Comments (9)

andrew-berezhnoy93 avatar andrew-berezhnoy93 commented on September 19, 2024

Hey! Thanks a lot for that explanation, it really helped me!

from tnc-scully.

TEHbKA-dev avatar TEHbKA-dev commented on September 19, 2024

Not working for me

from tnc-scully.

hakimio avatar hakimio commented on September 19, 2024

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.

markus2610 avatar markus2610 commented on September 19, 2024

@hakimio ListResponse is missing to generate the API.

from tnc-scully.

hakimio avatar hakimio commented on September 19, 2024

@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.

markus2610 avatar markus2610 commented on September 19, 2024

@hakimio No error, only the result model is generated without the wrapper class with count etc.

from tnc-scully.

hakimio avatar hakimio commented on September 19, 2024

@markus2610 Try the @ApiListResponse() decorator I have posted. It works as intended.

from tnc-scully.

hakimio avatar hakimio commented on September 19, 2024

Also, for those having issue with the original solution, remember to add @ApiExtraModels(PaginatedDto, UserDto) decorator to your controller class.

from tnc-scully.

firxworx avatar firxworx commented on September 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.