Skip to content
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

ApiExtraModels does not add additional models #738

Closed
cosinus84 opened this issue May 12, 2020 · 2 comments
Closed

ApiExtraModels does not add additional models #738

cosinus84 opened this issue May 12, 2020 · 2 comments

Comments

@cosinus84
Copy link

cosinus84 commented May 12, 2020

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

@kamilmysliwiec
Copy link
Member

Please provide a minimum reproduction repository.

@cosinus84
Copy link
Author

cosinus84 commented May 13, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants