Skip to content

BUG @Size annotation is ignored for requestBody parameters of collection type  #4792

@OllieKosh

Description

@OllieKosh

Problem

After upgrading swagger-core from 2.2.22 to 2.2.25, the @Size constraint annotation is ignored for the request body parameters of collection type, while it was properly processed before. I am using OpenAPI 3.1.

Expected behavior

@Size#min is used to generate minItems and @Size#max is used to generate maxItems for the generated property schema

For example for the following endpoint

@GET
@Path("/test")
public void getTest(@Size(min = 1, max = 100) List<String> myList) {}

generated spec should have the following request body (working in 2.2.22)

requestBody:
  content:
    application/json:
      schema:
        maxItems: 100
        minItems: 1
        type: array
        items:
          type: string

but instead the generated requestBody removes the maxItems and minItems (in version 2.2.25)

requestBody:
  content:
    application/json:
      schema:
        type: array
        items:
          type: string

Reproducer

I am able to reproduce this by adding a test to ReaderTest class

    @Test(description = "@Size annotation is properly processed on request body parameter of collection type")
    public void test123(){
        SwaggerConfiguration config = new SwaggerConfiguration().openAPI31(true).openAPI(new OpenAPI());
        Reader reader = new Reader(config);
        OpenAPI openAPI = reader.read(ExampleResource.class);

        String yaml = "openapi: 3.1.0\n" +
                        "paths:\n" +
                        "  /test:\n" +
                        "    get:\n" +
                        "      operationId: getTest\n" +
                        "      requestBody:\n" +
                        "        content:\n" +
                        "          '*/*':\n" +
                        "            schema:\n" +
                        "              type: array\n" +
                        "              items:\n" +
                        "                type: string\n" +
                        "              maxItems: 100\n" +
                        "              minItems: 1\n" +
                        "      responses:\n" +
                        "        default:\n" +
                        "          description: default response\n" +
                        "          content:\n" +
                        "            '*/*': {}";

        SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
    }

with ExampleResource

public class ExampleResource {

    @GET
    @Path("/test")
    public void getTest(@Size(min = 1, max = 100) List<String> myList) {}
}

Investigation with proposed solution

In ParameterProcessor.applyAnnotations there is a check for the @Size annotation. Inside it checks whether parameter.getSchema is an instance of ArraySchema, but it is now of type JsonSchema, so the information from @Size is not propagated to the schema (minItems/maxItems properties) and is removed in the generated spec. Proposed solution would be the same as solution in this issue, specifically "instead of using (parameter.getSchema() instanceof ArraySchema) a check is performed on the type/types fields of property to ensure that the property is an array schema".

Temporary workaround

Adding @ArraySchema(minItems=1, maxItems=100) corrects the generated contract and adds the minItems/maxItems back to the spec.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions