Skip to content

[BUG] kotlin-spring generator produces non-compiling Kotlin code for enum type integer #19244

@ulrichw-svg

Description

@ulrichw-svg

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 kotlin-spring generator produces Kotlin code that does not compile when the API specification contains an enum of type integer.

openapi-generator version

7.7.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: My API
  version: ${project.version}
paths:
  /healthcheck:
    get:
      summary: Health check endpoint.
      operationId: healthcheck
      responses:
        204:
          description: Successful health check
        default:
          description: Unexpected error
          content:
            'application/json':
              schema:
                $ref: '#/components/schemas/ApiError'

components:
  schemas:
    ApiError:
      required:
        - errorCode
      type: object
      properties:
        errorCode:
          type: integer
          enum:
            - 0
            - 100
Generation Details

Maven plugin configuration:

              <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.7.0</version>
                <executions>
                    <execution>
                        <id>generated-api-server</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                            <generatorName>kotlin-spring</generatorName>
                            <generateApiTests>false</generateApiTests>
                            <generateModelTests>false</generateModelTests>
                            <apiPackage>test.openapigeneratortest.api</apiPackage>
                            <modelPackage>test.openapigeneratortest.api.model</modelPackage>
                            <generateSupportingFiles>false</generateSupportingFiles>
                            <output>${project.build.directory}/generated-sources/api/server</output>
                            <additionalProperties>removeEnumValuePrefix=false</additionalProperties>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <skipDefaultInterface>true</skipDefaultInterface>
                                <useTags>true</useTags>
                                <useSpringBoot3>true</useSpringBoot3>
                                <annotationLibrary>none</annotationLibrary>
                                <documentationProvider>none</documentationProvider>
                                <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Generated code:

/**
 * 
 * @param errorCode 
 */
data class ApiError(

    @get:JsonProperty("errorCode", required = true) val errorCode: ApiError.ErrorCode
) {

    /**
    * 
    * Values: _0,_100
    */
    enum class ErrorCode(val value: kotlin.Int) {

        @JsonProperty(0) _0(0),
        @JsonProperty(100) _100(100)
    }

}
Steps to reproduce
  1. Generate the code using the openApi specification and the openapi-generator-maven-plugin from above.
  2. Try to compile the code that is generated (see above)

Expected result:
Kotlin compiler is able to compile the generated code

Actual result:
Compilation fails with error "Argument type mismatch: actual type is 'kotlin.Int', but 'kotlin.String' was expected.". This is because of the integer used as parameter of the JsonProperty annotation.

Related issues/PRs
Suggest a fix

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