Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [Java/Spring] Openapi Generator 7.4.0 generates Integer Min and Max Values for int64 array instead of Long Values causing compile errors #18082

Closed
5 of 6 tasks
svboettger opened this issue Mar 12, 2024 · 9 comments · Fixed by #18379

Comments

@svboettger
Copy link

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

Since version 7.4.0 arrays of int64 items with defined min and max values are generated with @min and @max annotations.
The values are generated as Integer instead of Long. which results in "integer number too large" errors.

Generated code:

  @Valid
  private List<@Min(1000000000000) @Max(1999999999999)Long> ids;
openapi-generator version

Since 7.4.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: test
  description: Test API
  version: 1.0.1

paths:
  /test:
    get:
      responses:
        200:
          description: Valid response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SomeLongIdArrayObject'

components:
  schemas:
    SomeLongIdArrayObject:
      type: object
      properties:
        ids:
          type: array
          items:
            type: integer
            format: int64
            minimum: 1000000000000
            maximum: 1999999999999
Generation Details

pom - Plugin

<plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.4.0</version>
                <executions>

                    <execution>
                        <id>test-v1</id>

                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/swagger/example.yaml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <generateApiTests>false</generateApiTests>
                            <generateApiDocumentation>false</generateApiDocumentation>
                            <generateModelTests>false</generateModelTests>
                            <apiPackage>de.test.v1.controller</apiPackage>
                            <modelPackage>de.test.v1.dto</modelPackage>
                            <ignoreFileOverride>${project.basedir}/.openapi-generator-ignore</ignoreFileOverride>
                            <skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                                <annotationLibrary>swagger2</annotationLibrary>
                                <useSpringBoot3>true</useSpringBoot3>
                                <skipDefaultInterface>true</skipDefaultInterface>
                            </configOptions>
                            <output>${project.basedir}/target/generated-sources</output>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <!-- https://mvnrepository.com/artifact/org.codehaus.plexus/plexus-utils -->
                    <dependency>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-utils</artifactId>
                        <version>${plexus.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            
Steps to reproduce

Generate via maven plugin results in

package de.test.v1.dto;

import java.net.URI;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.openapitools.jackson.nullable.JsonNullable;
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import io.swagger.v3.oas.annotations.media.Schema;


import java.util.*;
import jakarta.annotation.Generated;

/**
 * SomeLongIdArrayObject
 */

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.4.0")
public class SomeLongIdArrayObject {

  @Valid
  private List<@Min(1000000000000) @Max(1999999999999)Long> ids;

  public SomeLongIdArrayObject ids(List<@Min(1000000000000) @Max(1999999999999)Long> ids) {
    this.ids = ids;
    return this;
  }

  public SomeLongIdArrayObject addIdsItem(Long idsItem) {
    if (this.ids == null) {
      this.ids = new ArrayList<>();
    }
    this.ids.add(idsItem);
    return this;
  }

  /**
   * Get ids
   * @return ids
  */
  
  @Schema(name = "ids", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("ids")
  public List<@Min(1000000000000) @Max(1999999999999)Long> getIds() {
    return ids;
  }

  public void setIds(List<@Min(1000000000000) @Max(1999999999999)Long> ids) {
    this.ids = ids;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    SomeLongIdArrayObject someLongIdArrayObject = (SomeLongIdArrayObject) o;
    return Objects.equals(this.ids, someLongIdArrayObject.ids);
  }

  @Override
  public int hashCode() {
    return Objects.hash(ids);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class SomeLongIdArrayObject {\n");
    sb.append("    ids: ").append(toIndentedString(ids)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}
Suggest a fix

Generate Min and Max Value as Long values instead, as for normal int64 fields.

Expected Output:

package de.test.v1.dto;

import java.net.URI;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.openapitools.jackson.nullable.JsonNullable;
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import io.swagger.v3.oas.annotations.media.Schema;


import java.util.*;
import jakarta.annotation.Generated;

/**
 * SomeLongIdArrayObject
 */

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.4.0")
public class SomeLongIdArrayObject {

  @Valid
  private List<@Min(1000000000000L) @Max(1999999999999L)Long> ids;

  public SomeLongIdArrayObject ids(List<@Min(1000000000000L) @Max(1999999999999L)Long> ids) {
    this.ids = ids;
    return this;
  }

  public SomeLongIdArrayObject addIdsItem(Long idsItem) {
    if (this.ids == null) {
      this.ids = new ArrayList<>();
    }
    this.ids.add(idsItem);
    return this;
  }

  /**
   * Get ids
   * @return ids
  */
  
  @Schema(name = "ids", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("ids")
  public List<@Min(1000000000000L) @Max(1999999999999L)Long> getIds() {
    return ids;
  }

  public void setIds(List<@Min(1000000000000L) @Max(1999999999999L)Long> ids) {
    this.ids = ids;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    SomeLongIdArrayObject someLongIdArrayObject = (SomeLongIdArrayObject) o;
    return Objects.equals(this.ids, someLongIdArrayObject.ids);
  }

  @Override
  public int hashCode() {
    return Objects.hash(ids);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class SomeLongIdArrayObject {\n");
    sb.append("    ids: ").append(toIndentedString(ids)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}
@ZBSTooling
Copy link

I have the bug too and can confirm that the character 'L' is missing for the constants.

@fanqiewanzi
Copy link
Contributor

Is there any solution for this bug? Or we have to handle this manually. Is it a bug introduced by the template file?

@wing328
Copy link
Member

wing328 commented Apr 2, 2024

can you try openapi generator v7.3.0 as a workaround to see if it works for you?

@fanqiewanzi
Copy link
Contributor

can you try openapi generator v7.3.0 as a workaround to see if it works for you?

It's not work for me sadly, the type is Long but the bean validator @Max constant is normal integer.

This seems to be related to the nesting defined in yaml. The first one takes effect but the second one does not.

the one that works:

properties:
  vm_id:
    type: integer
    format: int64
    minimum: 100
    maximum:99999999999999
@javax.annotation.Nonnull
@NotNull
@Min(100L) @Max(9999999999999L)
public Long getVmId() {
    return vmId;
}

the one that doesn't works

properties:
  vm_ids:
    type:array
    items:
      type: integer
      format: int64
      minimum: 100
      maximum:9999999999999
@SerializedName(SERIALIZED_NAME_VM_IDS)
private List<@Min(100) @Max(9999999999999)Long> vmIds;

@svboettger
Copy link
Author

svboettger commented Apr 2, 2024

can you try openapi generator v7.3.0 as a workaround to see if it works for you?

Hi. Works fine with 7.3.0,as the min and max values are not generated for arrays there with the spring generator.

@jorgerod
Copy link
Contributor

can you try openapi generator v7.3.0 as a workaround to see if it works for you?

It's not work for me sadly, the type is Long but the bean validator @Max constant is normal integer.

This seems to be related to the nesting defined in yaml. The first one takes effect but the second one does not.

the one that works:

properties:
  vm_id:
    type: integer
    format: int64
    minimum: 100
    maximum:99999999999999
@javax.annotation.Nonnull
@NotNull
@Min(100L) @Max(9999999999999L)
public Long getVmId() {
    return vmId;
}

the one that doesn't works

properties:
  vm_ids:
    type:array
    items:
      type: integer
      format: int64
      minimum: 100
      maximum:9999999999999
@SerializedName(SERIALIZED_NAME_VM_IDS)
private List<@Min(100) @Max(9999999999999)Long> vmIds;

Same case, for arrays it does not work in 7.3.0, for objects it does.
The difference is that for objects it adds the L suffix while for arrays it does not add it.

@Max(9999999999999) vs @Max(9999999999999L)

@wing328
Copy link
Member

wing328 commented Apr 12, 2024

if anyone would like to contribute or sponsor a fix, please reply to let us know. thank you.

@fanqiewanzi
Copy link
Contributor

if anyone would like to contribute or sponsor a fix, please reply to let us know. thank you.

It seems easy to resolve this bug. I'm already found the code causes this. I will try to fix this today.

@wing328
Copy link
Member

wing328 commented Apr 16, 2024

thanks @fanqiewanzi for the fix, which has been merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants