OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] @Size not generated when request body is a list of primitive type elements

Open
#4,122 5 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

I'm trying to generate some javax.validation annotations in my generated Spring API files. Most of the cases are working fine, but when I try to add a size validation on a request body which is a list of primitive type elements, by adding minItems or maxItems for example, the generated method does not contain the expected javax.validation @Size annotation. Plus, I would also like to validate the length of each element inside my list

Actual:

ResponseEntity<Void> syncCars(@ApiParam(value = ""  )
                              @Valid
                              @RequestBody List<String> requestBody);

Expected:

ResponseEntity<Void> syncCars(@Size(min=1,max=100)
                              @ApiParam(value = ""  )
                              @Valid
                              @RequestBody List<@Size(min=5,max=100) String> requestBody);

or at least

ResponseEntity<Void> syncCars(@Size(min=1,max=100)
                              @ApiParam(value = ""  )
                              @Valid
                              @RequestBody List<String> requestBody);

When I try this by manually modifying the generated interface, the validation works great and throw errors when I send an empty list or a list with string which is too short.
Additionally, I would like to know if OAS uniqueItems property could, or should, generate anything else?

openapi-generator version

3.3.4 (maven plugin). I also tried the latest version, the result is the same

OpenAPI declaration file content or url
openapi: 3.0.0
servers:
  - url: 'http://localhost:8080/'
info:
  version: '1.0'
  title: My Project
paths:
  /cars/sync:
    post:
      tags:
        - Cars
      summary: test
      operationId: syncCars
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                type: string
                minLength: 5
                maxLength: 100
              minItems: 1
              maxItems: 100
              uniqueItems: true
      responses:
        '200':
          description: success
Command line used for generation
<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>3.3.4</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
        <generatorName>spring</generatorName>
        <output>target/generated-sources</output>

        <modelNameSuffix>Dto</modelNameSuffix>
        <generateSupportingFiles>false</generateSupportingFiles>
        <apiPackage>my.api</apiPackage>
        <modelPackage>my.api.dto</modelPackage>

        <configOptions>
          <sourceFolder>src/main/java/</sourceFolder>
          <java8>false</java8>
          <interfaceOnly>true</interfaceOnly>
          <dateLibrary>java8-localdatetime</dateLibrary>
        </configOptions>

      </configuration>
    </execution>
  </executions>
</plugin>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the supplied OpenAPI YAML and Maven plugin configuration to reproduce the generated Spring interface. Start by tracing how the Spring generator maps request-body array constraints and item constraints to Java validation annotations. Done means the generated signature reflects the supported minItems, maxItems, and item length behavior, with the uniqueItems expectation clarified.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.