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"
}
]
}