OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Kotlin] JsonProperty invalid for enum with non-string constructor value

Open
#15,255 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 (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

The @JsonProperty annotation for the generated enum values has the constructor value type and is not a string, which is invalid.

The yaml below results in:

enum class TestEnum(val value: kotlin.Float) {

        @JsonProperty(0f) _0(0f),
        @JsonProperty(1.5f) _1Period5(1.5f),
        @JsonProperty(2f) _2(2f)
 }
openapi-generator version

6.5.0 (doesn't seem to be a regression)

OpenAPI declaration file content or url
openapi: '3.0.3'

info:
  title: Test
  description: Test API
  version: '1.0'

paths:
  /test:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                testEnum:
                  type: number
                  minimum: -3.4028235E38
                  maximum: 3.4028235E38
                  enum:
                    - 0
                    - 1.5
                    - 2
      responses:
        '200':
          description: Success
Generation Details

Here is the config:

project.task<GenerateTask>("openApiGenerate$capitalizedName") {
            inputSpec.set("pathToSpec")
            outputDir.set("outputDir")
            generatorName.set("kotlin-spring")
            configOptions.put("enumPropertyNaming", "original")
            // The following prevents value type from being BigDecimal
            importMappings.put("FloatDto", "kotlin.Float")
            importMappings.put("DoubleDto", "kotlin.Double")
            typeMappings.put("DoubleDto", "kotlin.Double")
            typeMappings.put("FloatDto", "kotlin.Float")
            typeMappings.put("number+double", "Double")
            typeMappings.put("number", "Float")
}
Steps to reproduce

Generate kotlin with above OpenApi spec and config

Related issues/PRs

None that I'm aware of

Suggest a fix

No suggestions

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

Start by generating the kotlin-spring output from the supplied OpenAPI declaration and configuration, then inspect how the enum values produce their @JsonProperty annotations. Compare the generated annotations with valid Kotlin/Jackson string values; done means numeric enum constructor values still generate valid annotations.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.