OpenAPITools / OpenAPITools/openapi-generator

[BUG] Missing Java Bean Validation annotations when using `allOf` instead of $ref directly

Open
#7,342 7 comments 10 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 search 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 using:

components:
  schemas:
    Name:
      type: string
      maxLength: 50
      minLength: 1
      description: "A name"
    Cat:
      type: object
      description: "A cat with a name"
      properties:
        name:
          allOf:
            - $ref: '#/components/schemas/Name'
          description: "Name of this cat"
          example: "Kitty"

The generated code is:

@ApiModelProperty(example = "Kitty", value = "Name of this cat") 
public String getName() {
   return name;
}

Instead of:

@ApiModelProperty(example = "Kitty", value = "Name of this cat")
@Size(min=1,max=50) 
public String getName() {
   return name;
}
openapi-generator version

OpenAPI Generator 4.3.1 and 5.0.0-alpha.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: My animal API
  version: "1.0"
servers:
  - url: https://api.example.com
paths:
  /cats/:
    post:
      operationId: createCat
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Cat'
      responses:
        201:
          description: ""
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Cat'
  /dogs/:
    post:
      operationId: createDog
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Dog'
      responses:
        201:
          description: ""
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Dog'
components:
  schemas:
    Name:
      type: string
      maxLength: 50
      minLength: 1
      description: "A name"
    Cat:
      type: object
      description: "A cat with a name"
      properties:
        name:
          allOf:
            - $ref: '#/components/schemas/Name'
          description: "Name of this cat"
          example: "Kitty"
    Dog:
      type: object
      description: "A dog with a name"
      properties:
        name:
          allOf:
            - $ref: '#/components/schemas/Name'
          description: "Name of this dog"
          example: "Fido"

image

Generation Details

Plugin configuration:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>4.3.1</version>
    <executions>
        <execution>
            <id>code-from-openapi-document</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/spec.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <library>spring-boot</library>
                <apiPackage>com.acme.animals.controller</apiPackage>
                <modelPackage>com.acme.animals.dto</modelPackage>
                <generateApis>true</generateApis>
                <configOptions>
                    <interfaceOnly>true</interfaceOnly>
                    <dateLibrary>java8</dateLibrary>
                    <enableBuilderSupport>true</enableBuilderSupport>
                    <skipDefaultInterface>true</skipDefaultInterface>
                    <performBeanValidation>true</performBeanValidation>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
Steps to reproduce
  1. Run mvn clean generate-sources
  2. The generated classes contain:
  @ApiModelProperty(example = "Kitty", value = "Name of this cat")
  public String getName() {
    return name;
  }

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

Run mvn clean generate-sources with the supplied OpenAPI document and Spring generator configuration, then compare the generated getName() methods with the expected annotations. Trace how allOf and referenced schema constraints are handled when performBeanValidation is enabled. Done means the generated Java getters include the referenced @Size(min=1,max=50) constraint for both Cat and Dog.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.