Sahbaz
Sahbaz

Reputation: 1272

Swagger 2 UI How to show models that are not explicitly returned by RestController

I'm having following issue, on swagger under Models, i see just abstract Base class that is extended by 3 other classes. My current end point returns Base type of class, because i can have 3 different types returned on one end point.

So basically i have something like this

@MappedSuperclass
@ApiModel(description = "Base Details.")
abstract class BaseClass(
    open var id: String? = null,
    var prop1: String? = null,
    var prop2: String? = null,
    var prop3: String? = null,
    var prop4: String? = null
)

@ApiModel(description = "Some Specific Details that contains all base properties.")
data class AnotherClass(
        val prop4: String,
        val prop5: String,
        val prop6: Set<Amount>,
        val prop7: Set<Amount>,
        val prop8: String
) : BaseClass()


@ApiModel(description = "Some more Specific Details that contains all base properties.")
data class OneMoreClass(
        val prop4: String,
        val prop5: String
) : BaseClass()


And in RestController i have this

    @GetMapping
    @ApiOperation(value = "End point description", notes = "Notes notes notes.")
    fun getSomethingFromDatabase(): List<BaseClass> {
        return someService.getData();
    }

So issue that i have is on swagger UI, under Models section i see just BaseClass and no other classes at all...

I tried this, because somewhere i seen this example:

@ApiModel(description = "Base Details.", subTypes = {AnotherClass.class}) 
BaseClass 

but this way i have "kotlin" issue, that is saying "name is missing", also i can not do AnotherClass::class...

Upvotes: 0

Views: 2390

Answers (1)

Amith Kumar
Amith Kumar

Reputation: 4902

You will have to add those in the config as below:

    return new Docket(DocumentationType.SWAGGER_2)
            .additionalModels(typeResolver.resolve(AnotherClass.class), typeResolver.resolve(OneMoreClass.class))
.....

subTypes is still not completely supported in Swagger 2, still has an open ticket

For your Kotlin config, this is how it should look like:

subTypes = [AnotherClass::class, OneMoreClass::class]

I have just added a sample Kotlin controller for you to refer in my github project. Look for AnimalController.kt & SwaggerConfig for required setup.

Upvotes: 2

Related Questions