Swagger: How can I tell Swagger to include extended or specific models?

Created on 30 Apr 2019  路  6Comments  路  Source: nestjs/swagger

I'm submitting a...


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

Current behavior


This might be similar to https://github.com/nestjs/swagger/issues/223. I have a web method something like doWork( expression: AbstractExpression). Swagger includes AbstractExpression in Models. I have several subclasses which extend AbstractExpression, like PropertyExpression, StringExpression, etc, just subclasses. When I call my web method I'm wanting to receive a subclass expression like StringExpression.

The problem is that swagger is not including the extended subclasses in the list of Models because they are not directly referenced. How can I tell nestjs/swagger to explicitly include these extra models?

Expected behavior


I would like to be able to explicitly tag a model or tell swagger to include it. Maybe a class decorator for @ApiModel added to the class could tell swagger to include the model.

Minimal reproduction of the problem with instructions

web method doWork( expression: AbstractExpression)

// included because its referenced
export class AbstractExpression {
    @ApiModelProperty({ description: 'An Abstract class never used direclty.'})
    public expressionType: ExpressionType;
}

@ApiModel()  // this could tell swagger to include it in Models
export class PropertyExpression extends AbstractExpression {
    @ApiModelProperty({ description: 'A subclass dto to pass web method.'})
    public property: string;
}

*** Swagger will not include PropertyExpression in models.  How can I manually add it?

What is the motivation / use case for changing the behavior?


I have an abstract class and send in subclasses to a method that takes a variety of subclasses.

Environment


Nest version: X.Y.Z

 "@nestjs/core": "6.0.5",
"@nestjs/swagger": "3.0.2",

For Tooling issues:
- Node version: XX  
- Platform:  
node 10, macos

Others:

PRs open enhancement

Most helpful comment

Correct @loban.

This is available in 4.0.0 https://docs.nestjs.com/recipes/swagger#extra-models

Instead of using @ApiExtraModels, you can also pass extraModels array as a part of SwaggerDocumentOptions

SwaggerModule.createDocument(app, config, {
   extraModels: [.......]
});

All 6 comments

+1

any progress on this maybe?

One way to work around this would be to use the OpenAPI specification >= v3.0.
There are the keywords oneOf, anyOf, allOf (see https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/).
The subclasses could be listed with oneOf.

But I Think currently nestjs/swagger only supports OpenAPI Specification v2 (fka Swagger).

I also have the exact same motivation as described, would love to see this implemented. This issue seems related to #43.

I believe this is now handled in the next version using ApiExtraModels()?

Correct @loban.

This is available in 4.0.0 https://docs.nestjs.com/recipes/swagger#extra-models

Instead of using @ApiExtraModels, you can also pass extraModels array as a part of SwaggerDocumentOptions

SwaggerModule.createDocument(app, config, {
   extraModels: [.......]
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

malbertSC picture malbertSC  路  5Comments

Diluka picture Diluka  路  4Comments

kalaivanan-muthusamy picture kalaivanan-muthusamy  路  4Comments

ivashog picture ivashog  路  3Comments

dennisameling picture dennisameling  路  4Comments