OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][spring] Invalid annotations produced for collections with qualified types when useBeanValidation set

Open
#15,186 11 comments 5 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 BeanValidation is used, the annotation @Valid is inserted in a way that produces invalid syntax when there are collections of a fully qualified type.

openapi-generator version

This issue is a regression and was first seen on 6.5.0. It is also present on 6.6.0-20230411.095613-12.

OpenAPI declaration file content or url
openapi: 3.0.2
info: { title: Sample, version: "1.0" }
components:
  schemas:
    Observation:
      type: object
      properties:
        key: { type: string }
        value: { type: integer }
      required: [key, value]
    Report:
      type: object
      properties:
        observations:
          type: array
          items: { $ref: '#/components/schemas/Observation' }
      required: [observations]
paths:
  /reports:
    post:
      operationId: addReport
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Report' }
      responses:
        "204": { description: OK }

Generation Details

java -jar openapi-generator-cli.jar generate -i api.yaml -g spring --schema-mappings Observation=example.ObservationDTO

Steps to reproduce

See the generated src/main/java/org/openapitools/model/Report.java file.

Expected declaration:
List<example.@Valid ObservationDTO> observations

Actual declaration:
List<@Valid example.ObservationDTO> observations

Related issues/PRs

Introduced in #14736.

Suggest a fix

Instead of a simple replace operation, use a regular expression in SpringCodegen::replaceBeanValidationCollectionType

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

Inspect SpringCodegen::replaceBeanValidationCollectionType and the generation path for the generated Report.java file. Reproduce the issue with the supplied OpenAPI declaration and schema mapping. Done when qualified collection element types receive valid @Valid annotation syntax without breaking ordinary collection types.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.