Forum Discussion

EddyKr's avatar
EddyKr
New Contributor
2 years ago

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 ...
  • EddyKr's avatar
    2 years ago

    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());
    }
    });
    }