Skip to content

[Bug]: Primitive boxed type schemas used in @ApiResponse content schema implementations are incorrectly cast to string #4963

@OllieKosh

Description

@OllieKosh

Description of the problem/issue

When generating the specs for OpenAPI 3.1 for a JAX-RS resource, there is an issue with @ApiResponse object that uses primitive boxed types for the content schema implementation. When resolving the schema, although the types field in the schema object (io.swagger.v3.oas.models.media.Schema) is correct, type field is always string.

Affected Version

2.2.31 and above

Earliest version the bug appears in:
2.2.31 (introduced in PR 4868)

Steps to Reproduce

@Test
public void testApiResponses31() {
    SwaggerConfiguration config = new SwaggerConfiguration().openAPI31(true);
    Reader reader = new Reader(config);
    OpenAPI openAPI = reader.read(ApiResponsesResource.class);

    Operation postOperation = openAPI.getPaths().get("/mypath").getPost();
    Schema responseSchema = postOperation.getResponses().get("200").getContent().get("*/*").getSchema();

    assertEquals(responseSchema.getType(), "string"); // Incorrect type
    assertEquals(responseSchema.getTypes().iterator().next(), "boolean"); // Actual type
}

with ApiResponsesResource class being:

@Path("mypath")
public class ApiResponsesResource {

    @POST
    @ApiResponses({
            @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = Boolean.class)))
    })
    public void myMethod() { }
}

Expected Behavior

The type value should either be null or it should match the value in types.

Actual Behavior

type is always set to string (in AnnotationsUtils#947).

Proposed solution

The if condition surrounding the setting of the type to string in AnnotationsUtils#947 should also take types into consideration, so something like:

        if (StringUtils.isBlank(existingSchemaObject.get$ref()) && StringUtils.isBlank(existingSchemaObject.getType()) && 
                (existingSchemaObject.getTypes() == null || existingSchemaObject.getTypes().isEmpty())) {

Checklist

  • I have searched the existing issues and this is not a duplicate.
  • I have provided sufficient information for maintainers to reproduce the issue.

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