Skip to content

Commit 3c34e5f

Browse files
authored
Merge pull request #2184 from uc4w6c/fix_DefaultFlatParamObject_v2
fixed DefaultFlatParamObject to work with annotated parameters. Fixes…
2 parents 8779681 + 2260b43 commit 3c34e5f

File tree

8 files changed

+263
-16
lines changed

8 files changed

+263
-16
lines changed

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,12 @@ public static MethodParameter[] customize(String[] pNames, MethodParameter[] par
128128
explodedParameters.add(methodParameter);
129129
});
130130
}
131-
else if (defaultFlatParamObject) {
132-
boolean isSimpleType = MethodParameterPojoExtractor.isSimpleType(paramClass);
133-
boolean hasAnnotation = p.hasParameterAnnotations();
134-
boolean shouldFlat = !isSimpleType && !hasAnnotation;
135-
if (shouldFlat && !AbstractRequestService.isRequestTypeToIgnore(paramClass)) {
136-
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
137-
optionalDelegatingMethodParameterCustomizer
138-
.ifPresent(customizer -> customizer.customize(p, methodParameter));
139-
explodedParameters.add(methodParameter);
140-
});
141-
}
142-
else {
143-
String name = pNames != null ? pNames[i] : p.getParameterName();
144-
explodedParameters.add(new DelegatingMethodParameter(p, name, null, false, false));
145-
}
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+
});
146137
}
147138
else {
148139
String name = pNames != null ? pNames[i] : p.getParameterName();
@@ -295,4 +286,4 @@ public boolean isParameterObject() {
295286
return isParameterObject;
296287
}
297288

298-
}
289+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.v30.app204;
2+
3+
import org.springframework.validation.annotation.Validated;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class DefaultFlatParamObjectController {
10+
@GetMapping("/test1")
11+
public String test1(@RequestParam String email, @Validated Person person) {
12+
return null;
13+
}
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package test.org.springdoc.api.v30.app204;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import jakarta.validation.constraints.Size;
5+
6+
public class Person {
7+
private long id;
8+
9+
private String firstName;
10+
11+
@NotBlank
12+
@Size(max = 10)
13+
private String lastName;
14+
15+
public Person(long id, String firstName, String lastName) {
16+
this.id = id;
17+
this.firstName = firstName;
18+
this.lastName = lastName;
19+
}
20+
21+
public long getId() {
22+
return id;
23+
}
24+
25+
public String getFirstName() {
26+
return firstName;
27+
}
28+
29+
public String getLastName() {
30+
return lastName;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 test.org.springdoc.api.v30.AbstractSpringDocV30Test;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
import org.springframework.test.context.TestPropertySource;
29+
30+
@TestPropertySource(properties = { "springdoc.default-flat-param-object=true" })
31+
public class SpringDocApp204Test extends AbstractSpringDocV30Test {
32+
@SpringBootApplication
33+
static class SpringDocTestApp {}
34+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
"responses": {
58+
"200": {
59+
"description": "OK",
60+
"content": {
61+
"*/*": {
62+
"schema": {
63+
"type": "string"
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
},
72+
"components": {}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.app38;
2+
3+
import org.springframework.data.domain.Sort;
4+
import org.springframework.data.web.SortDefault;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class DefaultFlatParamObjectController {
10+
@GetMapping("/test1")
11+
public String test1(@SortDefault("name") Sort sort) {
12+
return null;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* * Copyright 2019-2023 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.app38;
20+
21+
import test.org.springdoc.api.AbstractSpringDocTest;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
import org.springframework.test.context.TestPropertySource;
25+
26+
@TestPropertySource(properties = { "springdoc.default-flat-param-object=true" })
27+
public class SpringDocApp38Test extends AbstractSpringDocTest {
28+
29+
@SpringBootApplication
30+
static class SpringDocTestApp {
31+
32+
}
33+
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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": "sort",
23+
"in": "query",
24+
"description": "Sorting criteria in the format: property,(asc|desc). Default sort order is ascending. Multiple sort criteria are supported.",
25+
"required": false,
26+
"schema": {
27+
"type": "array",
28+
"items": {
29+
"type": "string"
30+
},
31+
"default": [
32+
"name,ASC"
33+
]
34+
}
35+
}
36+
],
37+
"responses": {
38+
"200": {
39+
"description": "OK",
40+
"content": {
41+
"application/hal+json": {
42+
"schema": {
43+
"type": "string"
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
},
52+
"components":{
53+
"schemas":{}
54+
}
55+
}

0 commit comments

Comments
 (0)