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

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

Closed
KlausCGM opened this issue Mar 17, 2025 · 1 comment
Closed

Comments

@KlausCGM
Copy link

/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.

@KlausCGM
Copy link
Author

KlausCGM commented Apr 1, 2025

Thank you!

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

1 participant