Skip to content

[kotlin-client] Add support for integer enums in serialization step #21248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

CHervaudBetclic
Copy link
Contributor

@CHervaudBetclic CHervaudBetclic commented May 9, 2025

Following this issue, when using integer enums with the kotlin-client generator, the generated enum will look like this:

@Serializable
enum class SomeCode(val value: kotlin.Int) {
    @SerialName(value = "0")
    _0(0)
}

You can see the values of the enum members are not integers, they are strings (value = "0").
Using integer enums, it's should look like this:

@Serializable
enum class SomeCode(val value: kotlin.Int) {
    @SerialName(value = 0)
    _0(0),
}

How to test

  1. Create a test.yml file with the following content:
openapi: 3.0.3
info:
  title: Demo
  version: v1

components:
  schemas:
    Code:
      type: integer
      enum:
        - 0
        - 1
    StringCode:
      type: string
      enum:
        - hello
        - world

paths:
  /do:
    get:
      summary: Do something
      responses:
        200:
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                properties:
                  code:
                    $ref: '#/components/schemas/Code'
  1. Create a config.json file to generate only relevant content with the following:
{
  "modelPackage": "com.test",
  "generatorName": "kotlin",
  "enumPropertyNaming": "UPPERCASE",
  "additionalProperties": {
    "serializableModel": true,
    "omitGradleWrapper": true,
    "omitGradlePluginVersions": true
  },
  "globalProperties": {
    "apis": false,
    "models": "",
    "apiDocs": false,
    "modelDocs": false,
    "apiTests": false,
    "modelTests": false
  }
}
  1. Build openapi on the head branch of this PR: ./mvnw clean install -DskipTests -Dmaven.javadoc.skip=true
  2. Run the builded version against the files you've previously created: java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i test.yml -g kotlin -o out/ -c config.json
  3. See the generated code handling string enums (StringCode.kt) and integer enums (Code.kt)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Hello all @dr4ke616 @karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier @e5l, could you take a look please? 😁

@4brunu
Copy link
Contributor

4brunu commented May 16, 2025

@CHervaudBetclic did you tested this locally?

@CHervaudBetclic
Copy link
Contributor Author

@4brunu Hello
Yes, following the How to test section of the PR description

@4brunu
Copy link
Contributor

4brunu commented May 19, 2025

Thanks, looks good to me 👍

@4brunu 4brunu merged commit 0aaeb45 into OpenAPITools:master May 19, 2025
14 checks passed
@CHervaudBetclic
Copy link
Contributor Author

@4brunu Do you have any idea about the next release date?
I don't know if you have planned releases are if they are just done on the fly

@4brunu
Copy link
Contributor

4brunu commented May 19, 2025

You can find the next planned release date on the readme.
https://github.com/OpenAPITools/openapi-generator?tab=readme-ov-file#11---compatibility

@wing328 wing328 added this to the 7.14.0 milestone Jun 19, 2025
@Him188
Copy link

Him188 commented Jun 28, 2025

This change is incorrect and breaks compilation.

SerialName only takes String arguments:

public annotation class SerialName(val value: String)

Passing an integer literal is a compilation error.

image

@hrach
Copy link
Contributor

hrach commented Jun 29, 2025

@wing328 @4brunu please, revert this; it was not tested as it doesn't compile at all.

4brunu added a commit to 4brunu/openapi-generator that referenced this pull request Jun 29, 2025
4brunu added a commit that referenced this pull request Jun 29, 2025
@4brunu
Copy link
Contributor

4brunu commented Jun 29, 2025

This PR was reverts #21491

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants