Skip to content

Commit 94ac7ed

Browse files
committed
Prevent bad usage of @ParameterObject for simpleTypes. Fixes #1142
1 parent 9edfea6 commit 94ac7ed

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public static MethodParameter[] customize(String[] pNames, MethodParameter[] par
9797
for (int i = 0; i < parameters.length; ++i) {
9898
MethodParameter p = parameters[i];
9999
Class<?> paramClass = AdditionalModelsConverter.getReplacement(p.getParameterType());
100-
if (p.hasParameterAnnotation(ParameterObject.class) || AnnotatedElementUtils.isAnnotated(paramClass, ParameterObject.class)) {
100+
101+
if (!MethodParameterPojoExtractor.isSimpleType(paramClass) && (p.hasParameterAnnotation(ParameterObject.class) || AnnotatedElementUtils.isAnnotated(paramClass, ParameterObject.class))) {
101102
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
102103
optionalDelegatingMethodParameterCustomizer.ifPresent(customizer -> customizer.customize(p, methodParameter));
103104
explodedParameters.add(methodParameter);

springdoc-openapi-common/src/main/java/org/springdoc/core/MethodParameterPojoExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private static List<Field> allFieldsOf(Class<?> clazz) {
220220
* @param clazz the clazz
221221
* @return the boolean
222222
*/
223-
private static boolean isSimpleType(Class<?> clazz) {
223+
static boolean isSimpleType(Class<?> clazz) {
224224
return SIMPLE_TYPE_PREDICATES.stream().anyMatch(p -> p.test(clazz)) ||
225225
SIMPLE_TYPES.stream().anyMatch(c -> c.isAssignableFrom(clazz));
226226
}

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app154/HelloController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
import java.time.Instant;
2222

23+
import org.springdoc.api.annotations.ParameterObject;
24+
2325
import org.springframework.web.bind.annotation.GetMapping;
26+
import org.springframework.web.bind.annotation.PostMapping;
27+
import org.springframework.web.bind.annotation.RequestBody;
2428
import org.springframework.web.bind.annotation.RestController;
2529

2630
@RestController
@@ -31,4 +35,8 @@ public String hello() {
3135
return "Hello world at " + Instant.now().toString();
3236
}
3337

38+
@PostMapping(value = "/persons")
39+
public void create(@ParameterObject Long id, @RequestBody Object o){
40+
41+
}
3442
}

springdoc-openapi-webmvc-core/src/test/resources/results/app154.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@
1919
}
2020
],
2121
"paths": {
22+
"/persons": {
23+
"post": {
24+
"tags": [
25+
"hello-controller"
26+
],
27+
"operationId": "create",
28+
"requestBody": {
29+
"content": {
30+
"application/json": {
31+
"schema": {
32+
"type": "object",
33+
"properties": {
34+
"id": {
35+
"type": "integer",
36+
"format": "int64"
37+
},
38+
"o": {
39+
"type": "object"
40+
}
41+
}
42+
}
43+
}
44+
},
45+
"required": true
46+
},
47+
"responses": {
48+
"200": {
49+
"description": "OK"
50+
}
51+
}
52+
}
53+
},
2254
"/": {
2355
"get": {
2456
"tags": [

0 commit comments

Comments
 (0)