question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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;
  }

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:7

github_iconTop GitHub Comments

3reactions
tommyb82commented, Jul 22, 2021

We have just encountered this issue (or a very closely related one) via the Maven plugin, openapi-generator-maven-plugin v5.1.1 & 5.2.0 - Spring generator. We are missing the @Pattern annotation (and example attribute in @ApiModelProperty) on the agreementID property in our generated AgreementDetails class, where pattern is specified on the referenced base simple type AgreementId, e.g.

    AgreementId:
      type: string
      pattern: RM[0-9]{4}(.[0-9]{1,2})?
      example: RM1234
      readOnly: true

    AgreementDetails:
      type: object
      properties:
        agreementID:
          allOf:
            - $ref: '#/components/schemas/AgreementId'
          readOnly: false
1reaction
robertc-datcommented, Jul 16, 2021

Still seeing this issue with OpenAPI Generator 5.2.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Annotations from javax.validation.constraints not working
In my case, I was missing @Valid in the API that receives the Object (a POJO in my case) that was annotated with...
Read more >
Java Bean Validation Basics - Baeldung
Learn the basics of Java Bean validation, the common annotations and how to trigger the validation process.
Read more >
Better error messages with Bean Validation 1.1 in Spring MVC ...
Bean Validation 1.1, among many new features, introduced error message interpolation using Unified Expression Language (EL) expressions.
Read more >
Generate Server Code Using OpenAPI Generator
In this blog, you will learn how to generate server code using the OpenAPI Generator based on an OpenAPI specification.
Read more >
Validation Loop error occurred when allOf used with ...
I send an invalid basket with an orange ( originCountry is missing), I do not get a proper validation error as I do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found