Skip to content

[BUG][kotlin-spring] String to Enum conversion fails for enum query parameters with non default enumPropertyNaming #21564

@drnta

Description

@drnta

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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When the enumPropertyNaming config option value differs from original (e.g. UPPERCASE) kotlin-spring generator produces

enum class TestEnumSpec(@get:JsonValue val value: kotlin.String) {

    ABC("abc"),
    DEF("def"),
    GHK("ghk");

    companion object {
        @JvmStatic
        @JsonCreator
        fun forValue(value: kotlin.String): TestEnumSpec {
                return values().first{it -> it.value == value}
        }
    }
}

for the mentioned spec.
However forValue is not used by Spring by default to convert String input into the TestEnumSpec query parameter and the exception is thrown

Caused by: java.lang.IllegalArgumentException: No enum constant my.package.TestEnumSpec.abc
	at java.base/java.lang.Enum.valueOf(Enum.java:293) ~[?:?]
	at org.springframework.core.convert.support.StringToEnumConverterFactory$StringToEnum.convert(StringToEnumConverterFactory.java:54) ~[spring-core-6.2.7.jar:6.2.7]
	at org.springframework.core.convert.support.StringToEnumConverterFactory$StringToEnum.convert(StringToEnumConverterFactory.java:39) ~[spring-core-6.2.7.jar:6.2.7]
	at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:415) ~[spring-core-6.2.7.jar:6.2.7]
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) ~[spring-core-6.2.7.jar:6.2.7]
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:182) ~[spring-core-6.2.7.jar:6.2.7]

Java Spring generator seem to generate EnumConverterConfiguration to provide custom converters for the generated enums and force using forValue for conversion from String

openapi-generator version

7.12.0

OpenAPI declaration file content or url

schema

    TestEnumSpec:
      enum:
        - abc
        - def
        - ghk
      default: abc

then user in parameters as

        - name: test
          in: query
          schema:
            $ref: "#/components/schemas/TestEnumSpec"
Steps to reproduce

Create enum schema, use it as query parameter.
Use enumPropertyNaming=UPPERCASE config option for generation.

Related issues/PRs

The #12874 seems to be the similar issue for Java Spring generator

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