Skip to content

Commit 4d06552

Browse files
authored
Merge pull request #2183 from uc4w6c/fix_DefaultFlatParamObject
fixed DefaultFlatParamObject to work with annotated parameters. Fixes…
2 parents a70f518 + 34862fa commit 4d06552

File tree

5 files changed

+190
-15
lines changed

5 files changed

+190
-15
lines changed

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

+6-15
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,12 @@ public static MethodParameter[] customize(String[] pNames, MethodParameter[] par
125125
explodedParameters.add(methodParameter);
126126
});
127127
}
128-
else if (defaultFlatParamObject) {
129-
boolean isSimpleType = MethodParameterPojoExtractor.isSimpleType(paramClass);
130-
boolean hasAnnotation = p.hasParameterAnnotations();
131-
boolean shouldFlat = !isSimpleType && !hasAnnotation;
132-
if (shouldFlat && !AbstractRequestService.isRequestTypeToIgnore(paramClass)) {
133-
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
134-
optionalDelegatingMethodParameterCustomizer
135-
.ifPresent(customizer -> customizer.customize(p, methodParameter));
136-
explodedParameters.add(methodParameter);
137-
});
138-
}
139-
else {
140-
String name = pNames != null ? pNames[i] : p.getParameterName();
141-
explodedParameters.add(new DelegatingMethodParameter(p, name, null, false, false));
142-
}
128+
else if (defaultFlatParamObject && !MethodParameterPojoExtractor.isSimpleType(paramClass) && !AbstractRequestService.isRequestTypeToIgnore(paramClass)) {
129+
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
130+
optionalDelegatingMethodParameterCustomizer
131+
.ifPresent(customizer -> customizer.customize(p, methodParameter));
132+
explodedParameters.add(methodParameter);
133+
});
143134
}
144135
else {
145136
String name = pNames != null ? pNames[i] : p.getParameterName();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.org.springdoc.api.v30.app204;
2+
3+
import org.springdoc.api.annotations.ParameterObject;
4+
5+
import org.springframework.data.domain.Sort;
6+
import org.springframework.data.domain.Sort.Direction;
7+
import org.springframework.data.web.SortDefault;
8+
import org.springframework.validation.annotation.Validated;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
@RestController
15+
public class DefaultFlatParamObjectController {
16+
@GetMapping("/test1")
17+
public String test1(@RequestParam String email, @Validated Person person, @SortDefault("name") @ParameterObject Sort sort) {
18+
return null;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package test.org.springdoc.api.v30.app204;
2+
3+
import javax.validation.constraints.NotBlank;
4+
import javax.validation.constraints.NotNull;
5+
import javax.validation.constraints.Size;
6+
7+
public class Person {
8+
private long id;
9+
10+
private String firstName;
11+
12+
@NotBlank
13+
@Size(max = 10)
14+
private String lastName;
15+
16+
public Person(long id, String firstName, String lastName) {
17+
this.id = id;
18+
this.firstName = firstName;
19+
this.lastName = lastName;
20+
}
21+
22+
public long getId() {
23+
return id;
24+
}
25+
26+
public String getFirstName() {
27+
return firstName;
28+
}
29+
30+
public String getLastName() {
31+
return lastName;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2023 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.v30.app204;
24+
25+
import org.junit.jupiter.api.Test;
26+
import org.springdoc.core.Constants;
27+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
28+
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
import org.springframework.context.annotation.Import;
31+
import org.springframework.test.context.TestPropertySource;
32+
33+
import static org.hamcrest.Matchers.is;
34+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
35+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
36+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
37+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
38+
39+
@TestPropertySource(properties = { "springdoc.default-flat-param-object=true" })
40+
public class SpringDocApp204Test extends AbstractSpringDocV30Test {
41+
@SpringBootApplication
42+
static class SpringDocTestApp {}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
"default-flat-param-object-controller"
18+
],
19+
"operationId": "test1",
20+
"parameters": [
21+
{
22+
"name": "email",
23+
"in": "query",
24+
"required": true,
25+
"schema": {
26+
"type": "string"
27+
}
28+
},
29+
{
30+
"name": "id",
31+
"in": "query",
32+
"required": false,
33+
"schema": {
34+
"type": "integer",
35+
"format": "int64"
36+
}
37+
},
38+
{
39+
"name": "firstName",
40+
"in": "query",
41+
"required": false,
42+
"schema": {
43+
"type": "string"
44+
}
45+
},
46+
{
47+
"name": "lastName",
48+
"in": "query",
49+
"required": true,
50+
"schema": {
51+
"maxLength": 10,
52+
"minLength": 0,
53+
"type": "string"
54+
}
55+
},
56+
{
57+
"name": "sort",
58+
"in": "query",
59+
"description": "Sorting criteria in the format: property,(asc|desc). Default sort order is ascending. Multiple sort criteria are supported.",
60+
"required": false,
61+
"schema": {
62+
"type": "array",
63+
"items": {
64+
"type": "string"
65+
},
66+
"default": [
67+
"name,ASC"
68+
]
69+
}
70+
}
71+
],
72+
"responses": {
73+
"200": {
74+
"description": "OK",
75+
"content": {
76+
"*/*": {
77+
"schema": {
78+
"type": "string"
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}
85+
}
86+
},
87+
"components": {}
88+
}

0 commit comments

Comments
 (0)