swagger-api / swagger-api/swagger-codegen
[JAVA] Bug using useBeanValidation for int64 - Compilation failure with "integer number too large"
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Swagger Codegen is generating Model with invalid annotation when used with and as true for swagger type "integer" and format "int64".
Following is my element definition in swagger
components:
schemas:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
minimum: -9223372036854775808
maximum: 9223372036854775807
Generated code has following annotation
@NotNull
@Min(-9223372036854775808) @Max(9223372036854775807) @Schema(required = true, description = "")
public Long getId() {
return id;
}
Compiler is giving following error for above generated annotation -
integer number too large: -9223372036854775808
integer number too large: 9223372036854775807
Swagger-codegen version
io.swagger.codegen.v3
swagger-codegen-maven-plugin
3.0.27
Swagger declaration file content or url
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
responses:
200:
description: An paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
201:
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
200:
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
components:
schemas:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
minimum: -9223372036854775808
maximum: 9223372036854775807
name:
type: string
tag:
type: string
Pets:
type: array
items:
$ref: "#/components/schemas/Pet"
Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
Command line used for generation
Spring Boot: 2.3.5.RELEASE (It's reproducible in any spring boot)
Java: 8
Maven File Execution Phase Code -
<execution>
<id>sampleswagger</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/resources/swaggers/Sample-Swagger.yml</inputSpec>
<language>java</language>
<library>jersey2</library>
<generateApis>false</generateApis>
<generateModels>true</generateModels>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<generateSupportingFiles>false</generateSupportingFiles>
<modelPackage>com.mylearning.rinksp.model.sampleswagger</modelPackage>
<output>target/generated-sources</output>
<configOptions>
<dateLibrary>java8</dateLibrary>
<sourceFolder>java</sourceFolder>
<performBeanValidation>true</performBeanValidation>
<useBeanValidation>true</useBeanValidation>
</configOptions>
<typeMappings>
<typeMapping>Double=java.math.BigDecimal</typeMapping>
</typeMappings>
</configuration>
</execution>
Steps to reproduce
- Use provided swagger content
- Spring Boot Maven project with Java 8
- Add maven swagger codegen plugin using mentioned swagger codegen version and execution details
- Try to build project (mvn install ...)
- Build time error will be produce
Suggest a fix/enhancement
data type - integer with format "int64" is converted to "Long" data type in Java. So Min and Max annotation also must consider to add "L" in the end of values. After generating Model classes, If code is changed manually to add "L" as shown below then it's working alright.
Min(-9223372036854775808) REPLACED with Min(-9223372036854775808L)
Max(9223372036854775807) replaced with Max(9223372036854775807L)
@NotNull
@Min(-9223372036854775808L) @Max(9223372036854775807L) @Schema(required = true, description = "")
public Long getId() {
return id;
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Java model generation and the Bean Validation annotation rendering using the provided Swagger declaration and Maven execution configuration. Reproduce the Java 8 compilation failure, then verify that generated Long bounds compile successfully with the intended annotations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100