Skip to content

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

Closed
@bcmedeiros

Description

@bcmedeiros

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions