springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.26k stars 493 forks source link

Display nullable request body with map type #2727

Closed granddaifuku closed 1 week ago

granddaifuku commented 1 week ago

Fixes #2703

Problem Overview

@RestController
public class GreetController {
    @PostMapping("/greet")
    @Operation(summary = "Greet")
    public String greet(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Some description", required = false)
            @RequestBody(required = false) Map<String, String> body) {
        return body.getOrDefault("greet", "Hello");
    }
}

In the example above, the request body is not displayed because it is marked as a parameter to ignore. This behavior stems from the fact that the request body is both optional and of type java.util.Map. The relevant code can be found here: https://github.com/springdoc/springdoc-openapi/blob/03349cff6a2480555c746cf560f7d14272c93331/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/AbstractRequestService.java#L451-L457

https://github.com/springdoc/springdoc-openapi/blob/03349cff6a2480555c746cf560f7d14272c93331/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/AbstractRequestService.java#L141

Although this logic makes sense in some cases, I believe that using a Map as a request body is fairly common and should be supported, even when the request body is not required. As such, I’ve made an adjustment that ensures the request body will still be displayed when its type is Map.

I would love to hear your thoughts or feedback on this approach and whether you think it aligns with the project’s goals.