Skip to content

Commit d992477

Browse files
author
bnasslahsen
committed
Improve Management of multiple params in requestBody
1 parent 9e46c38 commit d992477

File tree

8 files changed

+242
-53
lines changed

8 files changed

+242
-53
lines changed

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

+15-13
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,13 @@ Schema calculateSchema(Components components, java.lang.reflect.Parameter parame
214214
else {
215215
schemaN = SpringDocAnnotationsUtils.resolveSchemaFromType(schemaImplementation, components, jsonView);
216216
}
217-
if (isRequestBodySchema(requestBodyInfo)) {
218-
requestBodyInfo.getMergedSchema().addProperties(paramName, schemaN);
219-
schemaN = requestBodyInfo.getMergedSchema();
217+
if (requestBodyInfo != null) {
218+
if (requestBodyInfo.getMergedSchema() != null) {
219+
requestBodyInfo.getMergedSchema().addProperties(paramName, schemaN);
220+
schemaN = requestBodyInfo.getMergedSchema();
221+
}
222+
else
223+
requestBodyInfo.addProperties(paramName, schemaN);
220224
}
221225

222226
return schemaN;
@@ -238,32 +242,30 @@ private Schema extractFileSchema(String paramName, RequestBodyInfo requestBodyIn
238242

239243
private Schema getFileSchema(RequestBodyInfo requestBodyInfo) {
240244
Schema schemaN;
241-
if (isRequestBodySchema(requestBodyInfo))
245+
if (requestBodyInfo.getMergedSchema() != null)
242246
schemaN = requestBodyInfo.getMergedSchema();
243-
else
247+
else {
244248
schemaN = new ObjectSchema();
249+
requestBodyInfo.setMergedSchema(schemaN);
250+
}
245251
return schemaN;
246252
}
247253

248-
private boolean isRequestBodySchema(RequestBodyInfo requestBodyInfo) {
249-
return requestBodyInfo != null && requestBodyInfo.getMergedSchema() != null;
250-
}
251-
252254
protected abstract boolean isFile(ParameterizedType parameterizedType);
253255

254256
protected abstract boolean isFile(JavaType ct);
255257

256258
public boolean isFile(java.lang.reflect.Parameter parameter) {
257-
boolean result =false;
258-
Type type = parameter.getParameterizedType();
259+
boolean result = false;
260+
Type type = parameter.getParameterizedType();
259261
JavaType javaType = constructType(parameter.getType());
260262
if (isFile(javaType)) {
261-
result =true;
263+
result = true;
262264
}
263265
else if (type instanceof ParameterizedType) {
264266
ParameterizedType parameterizedType = (ParameterizedType) type;
265267
if (isFile(parameterizedType)) {
266-
result =true;
268+
result = true;
267269
}
268270
}
269271
return result;

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public Operation build(Components components, HandlerMethod handlerMethod, Reque
145145
if (pNames == null) {
146146
pNames = reflectionParametersNames;
147147
}
148-
RequestBodyInfo requestBodyInfo = new RequestBodyInfo(methodAttributes);
148+
RequestBodyInfo requestBodyInfo = new RequestBodyInfo();
149149
List<Parameter> operationParameters = (operation.getParameters() != null) ? operation.getParameters()
150150
: new ArrayList<>();
151151
Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap = getApiParameters(handlerMethod.getMethod());
@@ -178,7 +178,6 @@ public Operation build(Components components, HandlerMethod handlerMethod, Reque
178178
applyBeanValidatorAnnotations(parameter, Arrays.asList(parameters[i].getAnnotations()));
179179
}
180180
else if (!RequestMethod.GET.equals(requestMethod)) {
181-
requestBodyInfo.incrementNbParam();
182181
requestBodyInfo.setRequestBody(operation.getRequestBody());
183182
requestBodyBuilder.calculateRequestBodyInfo(components, handlerMethod, methodAttributes, i,
184183
parameterInfo, requestBodyInfo);

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

+3-31
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,17 @@
1818

1919
package org.springdoc.core;
2020

21-
import java.util.Arrays;
22-
2321
import io.swagger.v3.oas.models.media.ObjectSchema;
2422
import io.swagger.v3.oas.models.media.Schema;
2523
import io.swagger.v3.oas.models.parameters.RequestBody;
2624

27-
import org.springframework.http.MediaType;
28-
2925
@SuppressWarnings("rawtypes")
3026
class RequestBodyInfo {
3127

3228
private RequestBody requestBody;
3329

3430
private Schema mergedSchema;
3531

36-
private int nbParams;
37-
38-
public RequestBodyInfo(MethodAttributes methodAttributes) {
39-
String[] allConsumes = methodAttributes.getAllConsumes();
40-
if (Arrays.asList(allConsumes).contains(MediaType.MULTIPART_FORM_DATA_VALUE)) {
41-
this.initMergedSchema();
42-
}
43-
}
44-
4532
public RequestBody getRequestBody() {
4633
return requestBody;
4734
}
@@ -50,33 +37,18 @@ public void setRequestBody(RequestBody requestBody) {
5037
this.requestBody = requestBody;
5138
}
5239

53-
public void incrementNbParam() {
54-
nbParams++;
55-
}
56-
5740
public Schema getMergedSchema() {
58-
if (mergedSchema == null && nbParams > 1) {
59-
mergedSchema = new ObjectSchema();
60-
}
6141
return mergedSchema;
6242
}
6343

6444
public void setMergedSchema(Schema mergedSchema) {
6545
this.mergedSchema = mergedSchema;
6646
}
6747

68-
private void initMergedSchema() {
69-
if (mergedSchema == null) {
48+
public void addProperties(String paramName, Schema schemaN) {
49+
if (mergedSchema == null)
7050
mergedSchema = new ObjectSchema();
71-
}
72-
}
73-
74-
public int getNbParams() {
75-
return nbParams;
76-
}
77-
78-
public void setNbParams(int nbParams) {
79-
this.nbParams = nbParams;
51+
mergedSchema.addProperties(paramName, schemaN);
8052
}
8153

8254
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.api.app82;
20+
21+
import org.springframework.http.ResponseEntity;
22+
import org.springframework.web.bind.annotation.PutMapping;
23+
import org.springframework.web.bind.annotation.RequestMapping;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
@RestController
27+
public class HelloController {
28+
29+
30+
@PutMapping(value = "/test")
31+
public ResponseEntity<?> put(
32+
String configuration,
33+
String second, PersonDTO personDTO ) {
34+
return null;
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.api.app82;
20+
21+
public class PersonDTO {
22+
private String email;
23+
24+
private String firstName;
25+
26+
private String lastName;
27+
28+
public PersonDTO() {
29+
}
30+
31+
public PersonDTO(final String email, final String firstName, final String lastName) {
32+
this.email = email;
33+
this.firstName = firstName;
34+
this.lastName = lastName;
35+
}
36+
37+
public String getEmail() {
38+
return email;
39+
}
40+
41+
public void setEmail(final String email) {
42+
this.email = email;
43+
}
44+
45+
public String getFirstName() {
46+
return firstName;
47+
}
48+
49+
public void setFirstName(final String firstName) {
50+
this.firstName = firstName;
51+
}
52+
53+
public String getLastName() {
54+
return lastName;
55+
}
56+
57+
public void setLastName(final String lastName) {
58+
this.lastName = lastName;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.api.app82;
20+
21+
import test.org.springdoc.api.AbstractSpringDocTest;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
25+
public class SpringDocApp82Test extends AbstractSpringDocTest {
26+
27+
@SpringBootApplication
28+
static class SpringDocTestApp {}
29+
30+
}

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

+43-7
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,39 @@
1111
}
1212
],
1313
"paths": {
14-
"/myapi": {
15-
"get": {
14+
"/test": {
15+
"put": {
1616
"tags": [
17-
"my-api-controller"
17+
"hello-controller"
1818
],
19-
"description": "Annotations from interfaces test",
20-
"operationId": "get",
19+
"operationId": "put",
20+
"requestBody": {
21+
"content": {
22+
"application/json": {
23+
"schema": {
24+
"type": "object",
25+
"properties": {
26+
"configuration": {
27+
"type": "string"
28+
},
29+
"second": {
30+
"type": "string"
31+
},
32+
"personDTO": {
33+
"$ref": "#/components/schemas/PersonDTO"
34+
}
35+
}
36+
}
37+
}
38+
}
39+
},
2140
"responses": {
2241
"200": {
2342
"description": "default response",
2443
"content": {
2544
"*/*": {
2645
"schema": {
27-
"type": "string"
46+
"type": "object"
2847
}
2948
}
3049
}
@@ -33,5 +52,22 @@
3352
}
3453
}
3554
},
36-
"components": {}
55+
"components": {
56+
"schemas": {
57+
"PersonDTO": {
58+
"type": "object",
59+
"properties": {
60+
"email": {
61+
"type": "string"
62+
},
63+
"firstName": {
64+
"type": "string"
65+
},
66+
"lastName": {
67+
"type": "string"
68+
}
69+
}
70+
}
71+
}
72+
}
3773
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
"/test": {
15+
"put": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "put",
20+
"requestBody": {
21+
"content": {
22+
"application/json": {
23+
"schema": {
24+
"type": "object",
25+
"properties": {
26+
"configuration": {
27+
"type": "string"
28+
},
29+
"second": {
30+
"type": "string"
31+
}
32+
}
33+
}
34+
}
35+
}
36+
},
37+
"responses": {
38+
"200": {
39+
"description": "default response",
40+
"content": {
41+
"*/*": {
42+
"schema": {
43+
"type": "object"
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
},
52+
"components": {}
53+
}

0 commit comments

Comments
 (0)