Florian Cousin
Florian Cousin

Reputation: 65

How to force a DTO class to be in Swagger models

I have a SpringBoot application with Swagger 3.17.1.

I have one abstract class AbstractDtoClass and one DTO class DtoClass that extends the first one.

I have several REST request definitions, and all returns a AbstractDtoClass object, none returns DtoClass.

As a result, the Swagger models, i.e. the DTO models I can find in "definitions" from /v2/api-docs, contains AbstractDtoClass but not DtoClass.

I would like DtoClass to be in the Swagger models too. How can I do that?

I have tried to put @SwaggerDefinition on the DtoClass definition.
I have tried to put @ApiModel(parent = AbstractDtoClass.class) on the DtoClass definition.
I have tried to put @ApiModel(subTypes = {DeclarationDto.class}, discriminator = "DeclarationDto") on the AbstractDtoClass definition even though I am not sure I am using discriminator properly.

Nothing have worked.

Can anyone help me please?

Upvotes: 2

Views: 4925

Answers (1)

Ramachandran Murugaian
Ramachandran Murugaian

Reputation: 661

You can make use of following method to add additional models that are not part of any annotation or are perhaps implicit.

springfox.documentation.spring.web.plugins.Docket#additionalModels(ResolvedType first, ResolvedType... remaining)

Below is the sample DocketConfig,

@Configuration
@EnableSwagger2
public class SwaggerConfig {

private final TypeResolver typeResolver;


public SwaggerConfig(final TypeResolver typeResolver) {
    this.typeResolver = typeResolver;
}

@Bean
public Docket docketApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .useDefaultResponseMessages(false)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.rmurugaian.service.pricing.server"))
        .build()
        .additionalModels(typeResolver.resolve(DummyDTO.class));
}

Upvotes: 1

Related Questions