Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger doesnt show inherited objects

I have a class "SubModel" which inherits from class "SuperModel". My REST request returns an object of one of these classes. Now i want to document that in my Swagger UI that either an object of SubModel OR an object of SuperModel will be returned. I googled for this use case and found the @ApiModel annotations but they dont work, does anybody have an idea what i´m doing wrong here?

@ApiModel(value = "SuperModel", discriminator = "foo", subTypes = { 
SubModel.class })
public class SuperModel
{
    @ApiModelProperty(required = true)
    private String foo;

    public String getFoo() {
        return this.foo;
    }

    public void setFoo( String foo ) {
        this.foo = foo;
    }
}  

@ApiModel(value = "SubModel")
public class SubModel extends SuperModel
{
    private int    number;

    public int getNumber() {
        return this.number;
    }

    public void setNumber( int number ) {
        this.number = number;
    }
}

What i see in Swagger UI is only:

SuperModel {
  foo (string)
}

I found the example at this site: https://github.com/swagger-api/swagger-core/wiki/Annotations#apiresponses-apiresponse but it doesnt work :-(

Any ideas? Thanks a lot!

like image 844
Thomas W Avatar asked Mar 17 '17 13:03

Thomas W


People also ask

What is Swagger inheritance?

Swagger Inheritance. It is a set of files (often just one) written in JSON or yaml that defines the routes, parameters, responses, and metadata of an API. After a spec has been written it can be used to generate clients, create human-friendly documentation, automate testing, or even scaffold code for the application code itself.

Does Swagger UI see inherited classes in model's?

Swagger UI see's my inherited class in the Model's according: What I would actually like to see in Swagger UI is this: (This image was taken from a YAML file created by me for another API that we will create, hence the different properties - What I like achieve does not change though)

What would you like to see in Swagger UI?

Image of partial Model Example: Swagger UI see's my inherited class in the Model's according: What I would actually like to see in Swagger UI is this: (This image was taken from a YAML file created by me for another API that we will create, hence the different properties - What I like achieve does not change though)

Is it possible to get Swagger from parent in Visual Studio?

Visual Studio shows this properly, but output swagger is generated without information from parent. yeah, nothing changed! Sorry, something went wrong. Just ran into this myself - not actually sure if this is possible, as the generated XML doc doesn't seem to contain any information relating to the inheritance chain.


1 Answers

This is not supported in springfox yet. See https://github.com/springfox/springfox/issues/868

Yes, it's confusing, since they do have annotations with attributes like "discriminator" and "subTypes", but they don't work.

UPDATE: the issue was closed in the meantime (in March 2018), and it should work in Springfox >= 2.9.0 (haven't tested it myself)

like image 153
Vlad Dinulescu Avatar answered Oct 08 '22 23:10

Vlad Dinulescu