Skip to content

Commit ad86c0a

Browse files
committed
Display nullable request body with map type. Fixes #2703
1 parent 03349cf commit ad86c0a

File tree

4 files changed

+137
-3
lines changed

4 files changed

+137
-3
lines changed

Diff for: springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/AbstractRequestService.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* *
44
* * *
55
* * * *
6-
* * * * * Copyright 2019-2022 the original author or authors.
6+
* * * * * Copyright 2019-2024 the original author or authors.
77
* * * * *
88
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
99
* * * * * you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@
2525
package org.springdoc.core.service;
2626

2727
import java.lang.annotation.Annotation;
28-
import java.lang.reflect.Field;
2928
import java.lang.reflect.Method;
3029
import java.math.BigDecimal;
3130
import java.util.ArrayList;
@@ -89,7 +88,6 @@
8988
import org.springframework.web.context.request.NativeWebRequest;
9089
import org.springframework.web.context.request.WebRequest;
9190
import org.springframework.web.method.HandlerMethod;
92-
import org.springframework.web.multipart.MultipartFile;
9391
import org.springframework.web.util.UriComponentsBuilder;
9492

9593
import static org.springdoc.core.converters.SchemaPropertyDeprecatingConverter.containsDeprecatedAnnotation;
@@ -453,6 +451,8 @@ public boolean isParamToIgnore(MethodParameter parameter) {
453451
return true;
454452
if (isRequiredAnnotation(parameter))
455453
return false;
454+
if (isRequestBodyWithMapType(parameter))
455+
return false;
456456
return isRequestTypeToIgnore(parameter.getParameterType());
457457
}
458458

@@ -471,6 +471,21 @@ private boolean isRequiredAnnotation(MethodParameter parameter) {
471471
|| (requestBody != null && requestBody.required());
472472
}
473473

474+
/**
475+
* Is request body with map type
476+
*
477+
* @param parameter the parameter
478+
* @return the boolean
479+
*/
480+
private boolean isRequestBodyWithMapType(MethodParameter parameter) {
481+
org.springframework.web.bind.annotation.RequestBody requestBody = parameter.getParameterAnnotation(org.springframework.web.bind.annotation.RequestBody.class);
482+
if (requestBody == null) {
483+
return false;
484+
}
485+
Class<?> parameterType = parameter.getParameterType();
486+
return parameterType == java.util.Map.class;
487+
}
488+
474489
/**
475490
* Sets params.
476491
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
*
3+
* * Copyright 2019-2024 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.v31.app10;
20+
21+
import io.swagger.v3.oas.annotations.Operation;
22+
import org.springframework.web.bind.annotation.PostMapping;
23+
import org.springframework.web.bind.annotation.RequestBody;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
import java.util.Map;
27+
28+
@RestController
29+
public class GreetController {
30+
@PostMapping("/greet")
31+
@Operation(summary = "Greet")
32+
public String greet(
33+
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Some description", required = false)
34+
@RequestBody(required = false) Map<String, String> body) {
35+
return body.getOrDefault("greet", "Hello");
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
*
3+
* * Copyright 2019-2024 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.v31.app10;
20+
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import test.org.springdoc.api.v31.AbstractSpringDocV31Test;
23+
24+
public class SpringDocApp10Test extends AbstractSpringDocV31Test {
25+
26+
@SpringBootApplication
27+
static class SpringDocTestApp {
28+
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/greet": {
15+
"post": {
16+
"tags": [
17+
"greet-controller"
18+
],
19+
"summary": "Greet",
20+
"operationId": "greet",
21+
"requestBody": {
22+
"description": "Some description",
23+
"content": {
24+
"application/json": {
25+
"schema": {
26+
"type": "object",
27+
"additionalProperties": {
28+
"type": "string"
29+
}
30+
}
31+
}
32+
}
33+
},
34+
"responses": {
35+
"200": {
36+
"description": "OK",
37+
"content": {
38+
"*/*": {
39+
"schema": {
40+
"type": "string"
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
},
49+
"components": {
50+
51+
}
52+
}

0 commit comments

Comments
 (0)