Skip to content
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

Kotlin enums are always marked as required if used in Java controllers #2622

Closed
bcmedeiros opened this issue Jun 13, 2024 · 3 comments
Closed

Comments

@bcmedeiros
Copy link

Describe the bug

If a Kotlin enum is used as a parameter in a request method of a controller written in Java, the parameter is always marked as required.

To Reproduce
Steps to reproduce the behavior:

Add the following classes to a Spring Boot project:
enum (kotlin):

enum class MyEnum {
    A, B;
}

controller (java):

@RestController
public class EnumController {
    @GetMapping("/test-enum-2")
    String testEnum2(@Parameter(required = false) @Nullable MyEnum e) {
        return "";
    }
}

This will generate the following spec, which is not consistent:

  ...
  "/test-enum-2": {
    "get": {
      "tags": [
        "enum-controller"
      ],
      "operationId": "testEnum2",
      "parameters": [
        {
          "name": "e",
          "in": "query",
          "required": true,
          "schema": {
            "type": "string",
            "enum": [
              "A",
              "B"
            ]
          }
        }
      ],
      "responses": {
        "200": {
          "description": "OK",
          "content": {
            "*/*": {
              "schema": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
  ...

Expected behavior

required should be equals false, since even though the enum is written in Kotlin, it's being used on a Java method and all parameters in a Java method are effective nullable.

@Parameter(required = false) and @Nullable annotations were added to the parameter in a tentative to override the current behaviour, but without success.

If we convert the exact same controller to Kotlin, then the required property of the parameter follows the nullability flag of the method parameter (MyEnum vs MyEnum?), which is the correct thing to do. We unfortunately cannot convert the controller right now (see context below).

Screenshots
(none)

Additional context
I know this is an unusual combination of Kotlin and Java classes, but we are gradually migrating our Java classes to Kotlin and that's our current reality, some of the business class are already Kotlin but we still have some Java controllers that will take some time to migrate.

  • What version of spring-boot you are using?
    3.2.4
  • What modules and versions of springdoc-openapi are you using?
    2.5.0
@bnasslahsen
Copy link
Contributor

@bcmedeiros,

I am adding a fix for this non usual use case.
The workaround for mixing the two langages, will be to use @Nullable annotation.

If you need further customization, feel free to override nullableKotlinRequestParameterCustomizer

@bcmedeiros
Copy link
Author

Thank you, I really appreciate it!

@nealeu
Copy link

nealeu commented Jul 9, 2024

@bcmedeiros & @bnasslahsen Thanks for getting this fixed. There are many people that this will help as large projects converting to Kotlin will be doing so incrementally.

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

No branches or pull requests

3 participants