Exclude unwanted "xml" tags from schema.
Hi everyone.
My team is working on introducing new REST API version. Our current version supports XML as well but going forward we would like to drop the support for it. We use Java for development and have encountered a situation that we can't seem to find solution for.
We rename our POJO/Bean classes by using @XmlRootElement.
@XmlRootElement(name = "simpleDomain")
When OAPI spec is generated the schema for this class will look something like
"simpleDomain" : { "type" : "object", "properties" : { "id" : { "type" : "string" }, "name" : { "type" : "string" } }, "xml" : { "name" : "simpleDomain" } },
We would like to omit the xml part
"xml" : { "name" : "simpleDomain" }
The only solution we have found that does that is to remove @XmlRootElement but if we do that then we break backwards compatibility. Have gone through docs on github and found https://github.com/OAI/OpenAPI-Specification/blob/3.0.1/versions/3.0.1.md#xmlObject but this doesn't allow hiding of the say element.
We are running 2.2.7 of swagger-jaxrs2 of swager.core.v3
If there is any information missing, please let me know and I will add.
We managed to solve this ourselves. We're processing the OpenApi object once it's finished generating, through this object we have access to schemas and their corresponding objects
private OpenAPI getOpenAPISpecification(final OpenApiContext openApiContext, final boolean isTenantIndependent) {
final OpenAPI openApiSpecification = getOpenApiSpecification();
// This will remove any tags from schema components and their children related to XML.
openApiSpecification.getComponents().getSchemas().forEach((k, schema) -> {
if (schema.getXml() != null) {
schema.setXml(null);
}
final Map<String, Schema> childSchemas = schema.getProperties();
if (childSchemas != null) {
removeXmlTagsFromSchemas(childSchemas.values());
}
});
return openApiSpecification;
}
private static void removeXmlTagsFromSchemas(final Collection<Schema> schemas) {
schemas.forEach(schema -> {
if (schema == null) {
return;
}
if (schema.getXml() != null) {
schema.setXml(null);
}
final Schema<?> item = schema.getItems();
if (item != null && item.getXml() != null) {
item.setXml(null);
}
final Map<String, Schema> childSchemas = schema.getProperties();
if (childSchemas != null && !childSchemas.isEmpty()) {
// Recursive call to remove tags from deeply nested schemas
removeXmlTagsFromSchemas(childSchemas.values());
}
});
}