简体   繁体   中英

Springfox swagger inheritance support

Is there any way to expose inheritance/ polymorphism in springfox swagger (2.7.0)? I know that swagger specification supports allOf. Is springfox support this? Below is sample domain model.

@ApiModel
public abstract class Animal{
    private String name;
}

@ApiModel(parent=Animal.class)
public class Dog extends Animal{
    ...
}

@ApiModel(parent=Animal.class)
public class Cat extends Animal{
    ...
}

If controller returns Animal, swagger contract doesn't expose Cat or Dog. It only returns Animal with it's properties.

Spring-fox hadn't added support to polymorphism at the time you posted it. However, the 2.9.0 release seems to add it. Check this out

Support to polymorphism is still not available (Using 2.9.2). What we did in our project to have the models documented is just simply add them manually..

In your swaggerConfig:

@Bean
public Docket apiDocumentation() {
    TypeResolver typeResolver = new TypeResolver();
    return new Docket(DocumentationType.SWAGGER_2)
                .additionalModels(
                    typeResolver.resolve(type1.class),
                    typeResolver.resolve(type2.class),
                    typeResolver.resolve(typeX.class));
}

This should make the models available in swagger-ui

@ApiModel(subTypes = {
Dog.class,
Cat.class},
discriminator = "type")
public abstract class Animal{
    private String name;
    public abstract AnimalType getType();
}

@ApiModel(parent=Animal.class)
public class Dog extends Animal{
    ...
}

@ApiModel(parent=Animal.class)
public class Cat extends Animal{
    ...
}

public enum AnimalType{
CAT,
DOG;
}

Solutions like above working for me, but documents only parent class with all options (types).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM