nestjs/swagger

ApiExtraModels does not add additional models

Closed this issue · 2 comments

I'm submitting a...

[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

How is this supposed to work (paste from documentation)
https://docs.nestjs.com/recipes/swagger#extra-models

Extra models#
In order to define additional models that should be inspected by Swagger module, use the @ApiExtraModels() decorator:

@ApiExtraModels(ExtraModel)
export class CreateCatDto {}

Is not working, and I also get some warning 'MyPet' was used before it was defined.eslint@typescript-eslint/no-use-before-define

Only way is working is defining in main.ts on
SwaggerModule.createDocument(app, options,{ extraModels: []});

What I want to have is an response formed by some metadata and entity

@ApiResponse({
    status: 200, description: 'Successfully', schema: {
      properties: {
        _metadata: {
          type: "object",
          $ref: getSchemaPath(PagingMetadata)
        },
        records: {
          type: "array",
          items: {
            $ref: getSchemaPath(Post) // typeorm entity
          }
        }
      }
    }
  })

Thanks

Please provide a minimum reproduction repository.

I realized that I must use @ApiExtraModels(Model) annotation on the method on not on top of the model class.

@ApiExtraModels(PagingMetadata)
@ApiResponse({
    status: 200, description: 'Successfully', schema: {
      properties: {
        _metadata: {
          type: "object",
          $ref: getSchemaPath(PagingMetadata)
        },
        records: {
          type: "array",
          items: {
            $ref: getSchemaPath(Post) // typeorm entity
          }
        }
      }
    }
  })
@Get()

I think what was confusing me is this line from the documentation
Both Cat and Dog must be defined as extra models using the @ApiExtraModels() decorator (at the class-level).