Skip to content

[Bug]: SpecFilter skips the ApiResponse#$ref field #4960

@vprudnikov

Description

@vprudnikov

Description of the problem/issue

When Spring's OpenApiHateoasLinksCustomizer calls the SpecFilter#removeBrokenReferenceDefinitions method, SpecFilter doesn't take into account the "$ref" field of the ApiResponse class:

addPathItemSchemaRef: 
...
                for (String keyResponses : op.getResponses().keySet()) {
                    ApiResponse response = op.getResponses().get(keyResponses);
                    if (response.getHeaders() != null) {
                        for (String keyHeaders : response.getHeaders().keySet()) {
                            Header header = response.getHeaders().get(keyHeaders);
                            addSchemaRef(header.getSchema(), referencedDefinitions);
                            addContentSchemaRef(header.getContent(), referencedDefinitions);
                        }
                    }
                    addContentSchemaRef(response.getContent(), referencedDefinitions);
                }
...
    private void addContentSchemaRef(Content content, Set<String> referencedDefinitions) {
        if (content != null) {
            for (MediaType mediaType : content.values()) {
                addSchemaRef(mediaType.getSchema(), referencedDefinitions);
            }
        }
    }

Affected Version

2.2.22

Earliest version the bug appears in (if known):
2.2.22

Steps to Reproduce

Define a controller with a reference to a custom schema like so:

    @Operation(summary = "Finds one sample", responses = {
            @ApiResponse(responseCode = "404", ref = "MyCustomSchema"),
    })

Add your custom schema and response using OpenApiCustomizer like so:

    protected static final String REF_MY_CUSTOM_SCHEMA = constructRef("MyCustomSchema");

    @Bean
    OpenApiCustomizer openApiCustomizer() {
        return openApi -> {
        var components = ofNullable(openApi.getComponents()).orElseGet(Components::new);
        commonResponses().forEach(components::addResponses);
        commonSchemas().forEach(components::addSchemas);

        openApi.setComponents(components);
        };
    }

    protected Map<String, Schema> commonSchemas() {
        return ModelConverters.getInstance().read(MyCustomSchema.class);
    }

    protected Map<String, ApiResponse> commonResponses() {
        return Map.of(
                "MyCustomSchema", new ApiResponse()
                        .description("A custom response")
                        .content(new Content().addMediaType(APPLICATION_JSON_VALUE, mediaTypeOf(REF_MY_CUSTOM_SCHEMA)))
        );
    }

    private static MediaType mediaTypeOf(String simpleRef) {
        return new MediaType().schema(new Schema<>().$ref(simpleRef));
    }

Expected Behavior

The schema is presented in the resulting JSON

Actual Behavior

The schema gets filtered by the SpecFilter

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions