OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Missing Validation Annotations of Array items in generated Classes

Open
#14,269 3 comments 0 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating schemas from am a yaml-file, that contain arrays with items that have validation properties (e.g. maxLength: 4), it appears that the generated classes are missing the associated Annotations (e.g. @Size(max=4)).

Using this specification as an example:

components:
  schemas:
    BrokenValidation:
      properties:
        stringWithValidation:
          description: 'String validation should look like this'
          type: string
          maxLength: 4
        brokenArray:
          description: 'Why no validation'
          type: array
          items:
            type: string
            maxLength: 4

We get a correct Annotation for stringWithValidation:

/**
   * String validation should look like this
   * @return stringWithValidation
  */
  @Size(max = 4) 
  @Schema(name = "stringWithValidation", description = "String validation should look like this", required = false)
  public String getStringWithValidation() {
    return stringWithValidation;
  }

But for the brokenArray this Annotation is missing:

/**
   * Why no validation
   * @return brokenArray
  */
  
  @Schema(name = "brokenArray", description = "Why no validation", required = false)
  public List<String> getBrokenArray() {
    return brokenArray;
  }

The correct generation would be:

/**
   * Why no validation
   * @return brokenArray
  */
  
  @Schema(name = "brokenArray", description = "Why no validation", required = false)
  public List<@Size(max=4) String> getBrokenArray() {
    return brokenArray;
  }
openapi-generator version

6.0.0

OpenAPI declaration file content or url
components:
  schemas:
    BrokenValidation:
      properties:
        stringWithValidation:
          description: 'String validation should look like this'
          type: string
          maxLength: 4
        brokenArray:
          description: 'Why no validation'
          type: array
          items:
            type: string
            maxLength: 4
Steps to reproduce

Copy paste the given extract (specs) in a YAML file and use it as input.

Suggest a fix

Since adding the necessary Annotations to the generated classes is possible, I suggest the code-generator should be adjusted to account for these types of properties.

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

Reproduce the issue with the supplied YAML schema and the Java generator, then trace how validation annotations are emitted for the ordinary string property versus the array's item schema. Done means the generated Java model preserves the item's maxLength as an appropriate @Size annotation, with coverage for the provided example.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.