Skip to content

Ensure enum classes toString() returns kotlin.String#16247

Merged
wing328 merged 1 commit intoOpenAPITools:masterfrom
androa:feature/support-string-in-enum
Aug 9, 2023
Merged

Ensure enum classes toString() returns kotlin.String#16247
wing328 merged 1 commit intoOpenAPITools:masterfrom
androa:feature/support-string-in-enum

Conversation

@androa
Copy link
Copy Markdown
Contributor

@androa androa commented Aug 3, 2023

When creating an enum where a valid value is String, the override of toString() will be the wrong type and compilation fails with:

Return type of 'toString' is not a subtype of the return type of the overridden member 'public open fun toString(): kotlin.String defined in kotlin.Enum'

Using kotlin.String instead of just String as the return value will allow using String as a value in an enum.

@jimschubert

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 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    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*.
    For Windows users, please run the script in Git BASH.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.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.

When creating an enum where a valid value is `String`, the override of toString() will be the wrong type.

Simply using `*kotlin*.String` will allow using `String` as a value in an enum.
androa added a commit to navikt/dp-saksbehandling that referenced this pull request Aug 4, 2023
@wing328
Copy link
Copy Markdown
Member

wing328 commented Aug 6, 2023

@androa thanks for the PR. Can you please share a spec to reproduce the issue?

@androa
Copy link
Copy Markdown
Contributor Author

androa commented Aug 7, 2023

Sure, this will reproduce the issue.

openapi: 3.0.3
info:
  title: Answer Type POC
  version: 1.0.0
paths:
  /answer-type:
    get:
      summary: Get Answer Type
      responses:
        '200':
          description: Successful response with Answer Type
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AnswerType'
components:
  schemas:
    AnswerType:
      type: string
      enum:
        - String
        - LocalDate
        - Int
        - Boolean
        - Double

What happens is the generated code will look like this:

enum class SvartypeDTO(val value: kotlin.String) {
    @JsonProperty(value = "String")
    String("String"),
    // ...
    
    override fun toString(): String = value

Since String in the return type of toString() is not prefixed with kotlin. it ends up referring to the String defined inside the enum.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Aug 9, 2023

@androa thanks for the explanation. The fix looks good.

@wing328 wing328 merged commit 75b04bb into OpenAPITools:master Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants