Skip to content

Commit 0287e83

Browse files
committed
Merge branch 'granddaifuku-fix/2703-display-request-body-with-nullable-map-type'
2 parents 03349cf + 3237787 commit 0287e83

File tree

6 files changed

+232
-3
lines changed

6 files changed

+232
-3
lines changed

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

Lines changed: 18 additions & 3 deletions
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 Map.class.isAssignableFrom(parameterType);
487+
}
488+
474489
/**
475490
* Sets params.
476491
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 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.ui.app32;
20+
21+
import jakarta.annotation.PostConstruct;
22+
import org.junit.jupiter.api.Test;
23+
import reactor.core.publisher.Mono;
24+
import test.org.springdoc.ui.AbstractCommonTest;
25+
import test.org.springdoc.ui.AbstractSpringDocTest;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
30+
import org.springframework.boot.test.web.server.LocalServerPort;
31+
import org.springframework.context.annotation.Import;
32+
import org.springframework.http.HttpStatus;
33+
import org.springframework.http.HttpStatusCode;
34+
import org.springframework.test.context.TestPropertySource;
35+
import org.springframework.web.reactive.function.client.WebClient;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
39+
40+
41+
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT,
42+
properties = { "spring.webflux.base-path=/test",
43+
"server.forward-headers-strategy=framework",
44+
"server.port=9318",
45+
"springdoc.swagger-ui.path=/documentation/swagger-ui.html",
46+
"springdoc.api-docs.path=/documentation/v3/api-docs",
47+
"springdoc.webjars.prefix= /webjars-pref" })
48+
49+
@Import(SpringDocConfig.class)
50+
public class SpringDocBehindProxyBasePathTest extends AbstractCommonTest {
51+
52+
private static final String X_FORWARD_PREFIX = "/path/prefix";
53+
54+
@LocalServerPort
55+
private int port;
56+
57+
private WebClient webClient;
58+
59+
@PostConstruct
60+
void init() {
61+
webClient = WebClient.builder().baseUrl("http://localhost:" + port)
62+
.build();
63+
}
64+
65+
@Test
66+
public void testIndex() throws Exception {
67+
HttpStatusCode httpStatusMono = webClient.get().uri("/test/documentation/swagger-ui.html")
68+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
69+
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
70+
assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);
71+
72+
httpStatusMono = webClient.get().uri("/test/documentation/webjars-pref/swagger-ui/index.html")
73+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
74+
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
75+
assertThat(httpStatusMono).isEqualTo(HttpStatus.OK);
76+
77+
String contentAsString = webClient.get().uri("/test/documentation/v3/api-docs/swagger-config")
78+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
79+
.retrieve()
80+
.bodyToMono(String.class).block();
81+
String expected = getContent("results/app32-1.json");
82+
assertEquals(expected, contentAsString, true);
83+
}
84+
85+
86+
87+
@SpringBootApplication
88+
static class SpringDocTestApp {}
89+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"configUrl": "/path/prefix/documentation/v3/api-docs/swagger-config",
3+
"oauth2RedirectUrl": "http://localhost:9318/path/prefix/documentation/webjars-pref/swagger-ui/oauth2-redirect.html",
4+
"url": "/path/prefix/documentation/v3/api-docs",
5+
"validatorUrl": ""
6+
}
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+
}
Lines changed: 52 additions & 0 deletions
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)