Skip to content

SpringBoot native fails /v3/api-docs when using a Map as an http entity field #2941

Closed
@KlausCGM

Description

@KlausCGM

/v3/api-docs fails at runtime in native mode, omitting the entity, when a http entity with a Map is used

/swagger-ui/index.html, or, better say /v3/api-docs generates an incomplete open-api definition due to an exception.

I am using Spring boot 3.4.3 and springdoc 2.8.5.

To Reproduce

The entity:

DifficultClass.kt.txt

data class DifficultClass(
    val name: String,
    /**
     * properties can be anything mapping a string to Any
     */
    @Schema(example = """
        {"sequence": 123}
        """)
    val properties: Map<String, Any> = emptyMap()
)

The controller:

DifficultController.kt.txt

@RestController
@RequestMapping("/")
// the below register is a fix for swagger ui for native image. We could not find out the reason,
// but the class will cause a "Type not found" and leave the swagger screen in a half-ready state.
//@RegisterReflectionForBinding(java.lang.invoke.MethodHandles.Lookup::class)
class DifficultController {
    @PostMapping("a")
    @Operation(summary = "reproduce the problem")
    fun someFunction(@RequestBody problems: List<DifficultClass>) : DifficultClass? {
        return if (problems.isEmpty())
            null
        else
            problems.first()
    }
}

Resulting exception (only native)

nativeException.txt

Here is a project including test to reproduce:
swaggerNativeBugDemo.zip

use
./gradlew nativeTest
to reproduce.

The workaround is to register the java class for reflection as follows
@RegisterReflectionForBinding(java.lang.invoke.MethodHandles.Lookup::class)

Expected behavior

  • The swagger should come up with a field for the request body the one controller method /a (which it does in non-native mode)
  • No exception should be thrown

Screenshots
This is the screen that comes up in the case of native image without the reflection hint
Image

Additional context: Workaround
The workaround, see above, is to register java.lang.invoke.MethodHandles.Lookup for the native image, but we don't know what is the underlying reason. I suspect it should be added to the springdoc provided spring-configuration-meadata.json in springdoc-openapi-starter-common-2.8.5.jar.

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