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

Display nullable request body with map type #2727

Conversation

granddaifuku
Copy link
Contributor

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:

public boolean isParamToIgnore(MethodParameter parameter) {
if (SpringDocAnnotationsUtils.isAnnotationToIgnore(parameter))
return true;
if (isRequiredAnnotation(parameter))
return false;
return isRequestTypeToIgnore(parameter.getParameterType());
}

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.

@granddaifuku granddaifuku marked this pull request as ready for review September 27, 2024 07:56
@bnasslahsen bnasslahsen merged commit ad86c0a into springdoc:main Sep 28, 2024
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

Successfully merging this pull request may close these issues.

Request body description not shown
2 participants