Skip to content

Commit 70943e0

Browse files
committed
Merge branch 'NaccOll-fix-2282'
2 parents 8fd5685 + a0ed6ef commit 70943e0

File tree

5 files changed

+183
-7
lines changed

5 files changed

+183
-7
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/extractor/DelegatingMethodParameter.java

+21-7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import org.springframework.core.annotation.AnnotatedElementUtils;
5252
import org.springframework.lang.NonNull;
5353
import org.springframework.lang.Nullable;
54+
import org.springframework.web.bind.annotation.RequestBody;
55+
import org.springframework.web.bind.annotation.RequestPart;
5456

5557
/**
5658
* The type Delegating method parameter.
@@ -128,12 +130,24 @@ public static MethodParameter[] customize(String[] pNames, MethodParameter[] par
128130
explodedParameters.add(methodParameter);
129131
});
130132
}
131-
else if (defaultFlatParamObject && !MethodParameterPojoExtractor.isSimpleType(paramClass) && !AbstractRequestService.isRequestTypeToIgnore(paramClass)) {
132-
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
133-
optionalDelegatingMethodParameterCustomizer
134-
.ifPresent(customizer -> customizer.customize(p, methodParameter));
135-
explodedParameters.add(methodParameter);
136-
});
133+
else if (defaultFlatParamObject) {
134+
boolean isSimpleType = MethodParameterPojoExtractor.isSimpleType(paramClass);
135+
List<Annotation> annotations = Arrays.stream(p.getParameterAnnotations())
136+
.filter(annotation -> Arrays.asList(RequestBody.class, RequestPart.class).contains(annotation.annotationType()))
137+
.toList();
138+
boolean hasAnnotation = !annotations.isEmpty();
139+
boolean shouldFlat = !isSimpleType && !hasAnnotation;
140+
if (shouldFlat && !AbstractRequestService.isRequestTypeToIgnore(paramClass)) {
141+
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
142+
optionalDelegatingMethodParameterCustomizer
143+
.ifPresent(customizer -> customizer.customize(p, methodParameter));
144+
explodedParameters.add(methodParameter);
145+
});
146+
}
147+
else {
148+
String name = pNames != null ? pNames[i] : p.getParameterName();
149+
explodedParameters.add(new DelegatingMethodParameter(p, name, null, false, false));
150+
}
137151
}
138152
else {
139153
String name = pNames != null ? pNames[i] : p.getParameterName();
@@ -286,4 +300,4 @@ public boolean isParameterObject() {
286300
return isParameterObject;
287301
}
288302

289-
}
303+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package test.org.springdoc.api.v30.app208;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestBody;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* @author bnasslahsen
11+
*/
12+
13+
@RestController
14+
@RequestMapping("/test1")
15+
public class HelloController {
16+
17+
@GetMapping
18+
public String test1(RequestObject object) {
19+
return null;
20+
}
21+
22+
@PostMapping
23+
public String test2(@RequestBody RequestObject obj) {
24+
return null;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package test.org.springdoc.api.v30.app208;
2+
3+
/**
4+
* @author bnasslahsen
5+
*/
6+
public class RequestObject {
7+
private String id;
8+
9+
private String name;
10+
11+
public String getId() {
12+
return id;
13+
}
14+
15+
public void setId(String id) {
16+
this.id = id;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.v30.app208;
2+
3+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
4+
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.test.context.TestPropertySource;
7+
8+
@TestPropertySource(properties = { "springdoc.default-flat-param-object=true" })
9+
public class SpringdocApp208Test extends AbstractSpringDocV30Test {
10+
11+
@SpringBootApplication
12+
static class SpringDocTestApp {}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"openapi": "3.0.1",
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+
"/test1": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "test1",
20+
"parameters": [
21+
{
22+
"name": "id",
23+
"in": "query",
24+
"required": false,
25+
"schema": {
26+
"type": "string"
27+
}
28+
},
29+
{
30+
"name": "name",
31+
"in": "query",
32+
"required": false,
33+
"schema": {
34+
"type": "string"
35+
}
36+
}
37+
],
38+
"responses": {
39+
"200": {
40+
"description": "OK",
41+
"content": {
42+
"*/*": {
43+
"schema": {
44+
"type": "string"
45+
}
46+
}
47+
}
48+
}
49+
}
50+
},
51+
"post": {
52+
"tags": [
53+
"hello-controller"
54+
],
55+
"operationId": "test2",
56+
"requestBody": {
57+
"content": {
58+
"application/json": {
59+
"schema": {
60+
"$ref": "#/components/schemas/RequestObject"
61+
}
62+
}
63+
},
64+
"required": true
65+
},
66+
"responses": {
67+
"200": {
68+
"description": "OK",
69+
"content": {
70+
"*/*": {
71+
"schema": {
72+
"type": "string"
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
},
81+
"components": {
82+
"schemas": {
83+
"RequestObject": {
84+
"type": "object",
85+
"properties": {
86+
"id": {
87+
"type": "string"
88+
},
89+
"name": {
90+
"type": "string"
91+
}
92+
}
93+
}
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)