Example for using inheritance with swagger-springmvc

2,631 views
Skip to first unread message

Daniel

unread,
Sep 5, 2014, 8:04:29 AM9/5/14
to swagger-sw...@googlegroups.com
Hi,

we want to start using Swagger (swagger-springmvc v0.8.5) in our project.

The initial setup worked fine and the web application generates the REST documentation.

But currently we are facing problems, when using Java object which are using inheritance as Request Payload for an Api Operation.

The generated documentation shows only the properties of the abstract base class. The definitions of the derived classes respectively the resources are omitted. 

I used following annotations.

In the abstract base class:

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "someEnum")
@JsonSubTypes({ @JsonSubTypes.Type(value = ClassB.class, name = "CLASS_B"),
   @JsonSubTypes.Type(value = ClassC.class, name = "CLASS_C"),
   @JsonSubTypes.Type(value = ClassD.class, name = "CLASS_D"),
   @JsonSubTypes.Type(value = ClassE.class, name = "CLASS_E") })
@ApiModel(value = "my base model", subTypes = { ClassB.class,
   ClassC.class, ClassD.class, ClassE.class })
public abstract class ClassA
{
  private SomeEnum someEnum;

    public void setSomeEnum SomeEnum someEnum)
   {
      this.someEnumsomeEnum;
   }
...
}

In the derived ClassB:
@ApiModel(value = "CLASS_B", parent = ClassA.class)
public class ClassB extends ClassA
{
 private AdditionalProperty addProp;
 
  public ClassB ()
  {
     this.setSomeEnum SomeEnum.CLASS_B );
  }
}

The other derived classes are specified accordingly to the ClassB.

Generated Model Schema:
{
  "someEnum": ""
}

Generated Model:
ClassA 
{
   someEnum(string, optional)=['CLASS_B' or 'CLASS_C' or 'CLASS_D' or 'CLASS_E']
}

I searched the Swagger documentation and the Swagger Group for a solution / example. I also read the Swagger Documentation regarding to the Model Object.

Does anyone has some glue what I am doing wrong here?

Thanks,
Daniel

Dilip Krishnan

unread,
Sep 6, 2014, 10:21:55 AM9/6/14
to swagger-sw...@googlegroups.com
Daniel,

Subclass arent as well supported. This is intentional, from a RESTful service standpoint, your domain models should just be messages and not an inheritance heirarchy, which is internal to the service. 

Having said that, swagger 2.0 has better support around defining those types of relationships, and we're looking to support that in the near future. Feel free to create an issue for it @ https://github.com/martypitt/swagger-springmvc

Thanks
Dilip

Daniel

unread,
Sep 8, 2014, 9:53:25 AM9/8/14
to swagger-sw...@googlegroups.com
Hi Dilip,

thank you for the clarification. I know that this is not the best approach, but this is a different story ;-).

Regarding to my problem I will try the newest swagger release.

Regards,
Daniel
Reply all
Reply to author
Forward
0 new messages