openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][JAVA][MAVEN] Lombok @Getter disables validation

Open Qmalamos1 opened this issue 2 months ago • 0 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?

ACTUAL: @lombok.Data @lombok.NoArgsConstructor @lombok.AllArgsConstructor

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2025-12-04T00:18:24.316599800+01:00[Europe/Madrid]", comments = "Generator version: 7.17.0") public class UserDto {

private Integer id;

private String username = "anonymous"; }

Expected something like this: @Data @NoArgsConstructor @AllArgsConstructor @Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2025-12-04T00:12:38.229297500+01:00[Europe/Madrid]", comments = "Generator version: 7.17.0") public class UserDto { @NotNull @Min(value = 1) @Max(value = 100) @Schema(name = "id", example = "1", requiredMode = Schema.RequiredMode.REQUIRED) @JsonProperty("id") private Integer id; @Size(min = 3, max = 20) @Schema(name = "username", example = "user", requiredMode = Schema.RequiredMode.NOT_REQUIRED) @JsonProperty("username") private String username = "anonymous"; }

Description

After adding <additionalModelTypeAnnotations>@lombok.Data; @lombok.NoArgsConstructor; @lombok.AllArgsConstructor;</additionalModelTypeAnnotations> (or /@Getter instead of Data) The getters get removed as expected, but since the validation is made on those, the @Min,@Max...etc, gets overriden.

openapi-generator version

1.17.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: TEST API
  version: 1.0.0
servers:
  - url: http://localhost
paths:
  /users:
    get:
      tags:
        - users
      summary: Obtener todos los usuarios
      responses:
        '200':
          description: Lista de usuarios
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/UserDto'
      operationId: getAllUsers
components:
  schemas:
    UserDto:
      type: object
      properties:
        id:
          type: integer
          example: 1
          minimum: 1
          maximum: 100
        username:
          type: string
          example: "user"
          default: "anonymous"
          minLength: 3
          maxLength: 20
      required:
        - id

Generation Details
Steps to reproduce

Simplified .pom with what i think is all the needed information.

      <properties>
          <springVersion>3.3.0</springVersion>
          <java.version>21</java.version>
          <openapi-generator.version>7.17.0</openapi-generator.version>
          <contract.package>com.example</contract.package>
          <openapi.contract>${project.basedir}/contract/main.yaml</openapi.contract>
          <swagger.apiPackage>domain.api</swagger.apiPackage>
          <swagger.modelPackage>domain.model</swagger.modelPackage>
      </properties>

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>${openapi-generator.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${openapi.contract}</inputSpec>
                        <generatorName>spring</generatorName>
                        <apiPackage>${contract.package}.${swagger.apiPackage}</apiPackage>
                        <modelPackage>${contract.package}.${swagger.modelPackage}</modelPackage>
                        <invokerPackage>com.urnhinkoo.domain.invoker</invokerPackage>
                        <output>${project.basedir}/</output>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <configOptions>
                            <sourceFolder>src/main/java</sourceFolder>
                            <useBeanValidation>true</useBeanValidation>
                            <useJakartaEe>true</useJakartaEe>
                            <openApiNullable>false</openApiNullable>
                            <skipDefaultInterface>true</skipDefaultInterface>
                            <interfaceOnly>true</interfaceOnly>
                            <!--@Getter can be used aswell to make this warning go away, but that would be annoying to use -->
                            <!--suppress UnresolvedMavenProperty -->
                            <additionalModelTypeAnnotations>@lombok.Data; @lombok.NoArgsConstructor; @lombok.AllArgsConstructor;</additionalModelTypeAnnotations>

                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Related issues/PRs
Suggest a fix

Validation should be on the field declarator, and not on the getMethod, so @Data works as expectted

Qmalamos1 avatar Dec 03 '25 23:12 Qmalamos1