Skip to content

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

@ThisUsernameCouldHaveBeenAnEmail

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions