OpenAPITools / OpenAPITools/openapi-generator

[BUG] [kotlin-server] double used in constraint annotation for integer types

Open
#23,445 0 comments 0 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?
  • 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?
Description

When declaring a integer schema, and referencing it as property with addional validation annotations
the generated code attampts to insert doubles in place of integers / longs

This:

/**
 * 
 * @param validatedInteger 
 */
data class ValidatedObject(

    @get:Min(value=1)
    @get:Max(value=5000.0)
    @get:JsonProperty("validatedInteger") val validatedInteger: kotlin.Int? = null
) {

}

Instead of this:

/**
 * 
 * @param validatedInteger 
 */
data class ValidatedObject(

    @get:Min(value=1)
    @get:Max(value=5000)
    @get:JsonProperty("validatedInteger") val validatedInteger: kotlin.Int? = null
) {

}

##### openapi-generator version

7.21.0

##### OpenAPI declaration file content or url

```yaml
openapi: 3.1.2
info:
  description: API
  title: API
  version: v0
servers: []
paths:
  /example:
    get:
      operationId: get
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ValidatedObject'
          description: List of car models for a given make
components:
  schemas:
    validatedInteger:
      type: integer
      format: int32
      description: Some Description
      minimum: 1
    ValidatedObject:
      type: object
      properties:
        validatedInteger:
            $ref: '#/components/schemas/validatedInteger'
            maximum: 5000
Generation Details

I've been using the gradle plugin for this with following settings:

openApiGenerate {
    generatorName.set("kotlin-spring")
    inputSpec.set(rootProject.file("openapi.yaml").toString())
    outputDir.set(generatedOpenApiCodeDir.map { it.asFile.toString() })
    packageName.set("com.example")

    configOptions.put("annotationLibrary", "none")
    configOptions.put("basePackage", "com.example")
    configOptions.put("configPackage", "com.example.config")
    configOptions.put("documentationProvider", "none")
    configOptions.put("gradleBuildFile", "false")
    configOptions.put("includeHttpRequestContext", "true")
    configOptions.put("interfaceOnly", "true")
    configOptions.put("library", "spring-boot")
    configOptions.put("requestMappingMode", "api_interface")
    configOptions.put("useSpringBoot4", "true")
    configOptions.put("useJackson3", "true")
}

So in config based settings it should be

generatorName: kotlin-spring
outputDir: samples/server/petstore/kotlin-springboot-4
library: spring-boot
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-spring
additionalProperties:
  documentationProvider: none
  annotationLibrary: none
  basePackage: "com.example"
  configPackage: "com.example.config"
  gradleBuildFile: "false"
  includeHttpRequestContext: "true"
  useSpringBoot4: "true"
  useJackson3: "true"
  requestMappingMode: api_interface
  interfaceOnly: "true"
Steps to reproduce
  1. Create an integer or long schema, and reference it as a property adding minimum or maximum annotations,
  2. attempt to generate classes
  3. classes are unable to compile due to type mismatch on the annotation.
Related issues/PRs
Suggest a fix

The simplest fix would seem to be to use the DecimalMax and DecimalMin variants always, as it seems like they should be able to support integer types too.

But otherwise I'm not sure what makes it start interpreting the integer as a double.

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

Reproduce the issue with the supplied OpenAPI declaration and the kotlin-spring configuration. Start by inspecting modules/openapi-generator/src/main/resources/kotlin-spring and the annotation generation path to trace why integer limits become doubles. Done means generated Kotlin classes use compatible integer annotation values and compile successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi, spring
Domain
backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.