Springfox rule inheritance in swagger schema

时间:2019-03-19 14:49:25

标签: java spring swagger-2.0 springfox

I tell swagger that BaseClass should be presented as a String in swagger.

ChildClass extends BaseClass

//someObject
"com.SomeObject" : {
    "properties" : [
        "name" : {
            "type": [
                "null",
                "string"
            ],
            "$id": "name"
        }
        "baseClass" : {
            "type": [
                "null",
                "string"
            ],
            "$id": "baseClass"
        }
    ]
}

This is Docket configuration

final AlternateTypeRule baseClassRule = AlternateTypeRules.newRule(
            typeResolver.resolve(BaseClass.class),
            typeResolver.resolve(String.class));

new Docket(DocumentationType.SWAGGER_2)
            .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage()))
                .paths(PathSelectors.any())
                .build()
            .pathMapping("/")
            .ignoredParameterTypes(ClassToIgnore.class)
            .directModelSubstitute(ObjectId.class, String.class) // This works. ObjectId will be presented as a String in swagger schema.
            .alternateTypeRules(
                baseClassRule
            );

However, when another model someObject has field baseClass which class extends BaseClass, then this rule is not applied, and the schema for anotherObject is different.

//someObject
"com.SomeObject" : {
    "properties" : [
        "name" : {
            "type": [
                "null",
                "string"
            ],
            "$id" : "name"
        }
        "baseClass" : {
            "$id" : "baseClass",
            "$ref": "#/definitions/com.ChildClass"
        }
    ]
}

0 个答案:

没有答案