Springfox swagger inheritance support

余生颓废 提交于 2019-12-07 13:20:33

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/46443817/springfox-swagger-inheritance-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!