Skip to content

Commit 1732dba

Browse files
committed
Changes report: Custom Requestmapping consumes responses. Fixes #1546
1 parent 95f1b91 commit 1732dba

File tree

14 files changed

+1459
-158
lines changed

14 files changed

+1459
-158
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,8 @@ protected void calculatePath(RouterOperation routerOperation, Locale locale) {
611611
* @param locale the locale
612612
*/
613613
protected void calculatePath(HandlerMethod handlerMethod, String operationPath,
614-
Set<RequestMethod> requestMethods, Locale locale) {
615-
this.calculatePath(handlerMethod, new RouterOperation(operationPath, requestMethods.toArray(new RequestMethod[requestMethods.size()])), locale);
614+
Set<RequestMethod> requestMethods,String[] consumes, String[] produces, String[] headers, Locale locale) {
615+
this.calculatePath(handlerMethod, new RouterOperation(operationPath, requestMethods.toArray(new RequestMethod[requestMethods.size()]), consumes, produces, headers), locale);
616616
}
617617

618618
/**

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/data/DataRestRouterOperationService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ else if (ControllerType.PROPERTY.equals(controllerType))
279279
MethodResourceMapping methodResourceMapping, HandlerMethod handlerMethod,
280280
RequestMethod requestMethod, ResourceMetadata resourceMetadata, String
281281
operationPath, ControllerType controllerType) {
282-
RouterOperation routerOperation = new RouterOperation(operationPath, new RequestMethod[] { requestMethod });
282+
RouterOperation routerOperation = new RouterOperation(operationPath, new RequestMethod[] { requestMethod }, null, null, null);
283283
MethodAttributes methodAttributes = new MethodAttributes(springDocConfigProperties.getDefaultConsumesMediaType(), springDocConfigProperties.getDefaultProducesMediaType(), dataRestRepository.getLocale());
284284
methodAttributes.calculateConsumesProduces(handlerMethod.getMethod());
285285
routerOperation.setConsumes(methodAttributes.getMethodConsumes());

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/fn/RouterOperation.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ public RouterOperation(org.springdoc.core.annotations.RouterOperation routerOper
143143
* @param path the path
144144
* @param methods the methods
145145
*/
146-
public RouterOperation(String path, RequestMethod[] methods) {
146+
public RouterOperation(String path, RequestMethod[] methods,String[] consumes, String[] produces, String[] headers) {
147147
this.path = path;
148148
this.methods = methods;
149+
this.consumes=consumes;
150+
this.produces=produces;
151+
this.headers=headers;
149152
}
150153

151154
/**

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/models/MethodAttributes.java

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
/*
22
*
33
* *
4+
* * * Copyright 2019-2020 the original author or authors.
45
* * *
5-
* * * * Copyright 2019-2022 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.
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
189
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
1917
* *
2018
*
2119
*/
@@ -33,6 +31,7 @@
3331
import org.apache.commons.lang3.ArrayUtils;
3432

3533
import org.springframework.core.annotation.AnnotatedElementUtils;
34+
import org.springframework.util.CollectionUtils;
3635
import org.springframework.web.bind.annotation.DeleteMapping;
3736
import org.springframework.web.bind.annotation.GetMapping;
3837
import org.springframework.web.bind.annotation.PostMapping;
@@ -271,21 +270,26 @@ else if (reqMappingClass != null) {
271270
* @param headers the headers
272271
*/
273272
private void fillMethods(String[] produces, String[] consumes, String[] headers) {
274-
if (ArrayUtils.isNotEmpty(produces))
275-
methodProduces = produces;
276-
else if (ArrayUtils.isNotEmpty(classProduces))
277-
methodProduces = classProduces;
278-
else
279-
methodProduces = new String[] { defaultProducesMediaType };
273+
if (ArrayUtils.isEmpty(methodProduces)) {
274+
if (ArrayUtils.isNotEmpty(produces))
275+
methodProduces = produces;
276+
else if (ArrayUtils.isNotEmpty(classProduces))
277+
methodProduces = classProduces;
278+
else
279+
methodProduces = new String[] { defaultProducesMediaType };
280+
}
280281

281-
if (ArrayUtils.isNotEmpty(consumes))
282-
methodConsumes = consumes;
283-
else if (ArrayUtils.isNotEmpty(classConsumes))
284-
methodConsumes = classConsumes;
285-
else
286-
methodConsumes = new String[] { defaultConsumesMediaType };
282+
if (ArrayUtils.isEmpty(methodConsumes)) {
283+
if (ArrayUtils.isNotEmpty(consumes))
284+
methodConsumes = consumes;
285+
else if (ArrayUtils.isNotEmpty(classConsumes))
286+
methodConsumes = classConsumes;
287+
else
288+
methodConsumes = new String[] { defaultConsumesMediaType };
289+
}
287290

288-
setHeaders(headers);
291+
if (CollectionUtils.isEmpty(this.headers))
292+
setHeaders(headers);
289293
}
290294

291295
/**

springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/api/OpenApiResource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ && isFilterCondition(handlerMethod, operationPath, produces, consumes, headers))
199199
// default allowed requestmethods
200200
if (requestMethods.isEmpty())
201201
requestMethods = this.getDefaultAllowedHttpMethods();
202-
calculatePath(handlerMethod, operationPath, requestMethods, locale);
202+
calculatePath(handlerMethod, operationPath, requestMethods, consumes, produces , headers, locale);
203203
}
204204
}
205205
}

springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/api/OpenApiResource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ && isFilterCondition(handlerMethod, operationPath, produces, consumes, headers))
223223
// default allowed requestmethods
224224
if (requestMethods.isEmpty())
225225
requestMethods = this.getDefaultAllowedHttpMethods();
226-
calculatePath(handlerMethod, operationPath, requestMethods, locale);
226+
calculatePath(handlerMethod, operationPath, requestMethods, consumes, produces, headers, locale);
227227
}
228228
}
229229
}

springdoc-openapi-starter-webmvc-api/src/test/resources/results/app143.json

+45-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,29 @@
3232
"200": {
3333
"description": "OK",
3434
"content": {
35-
"*/*": {
35+
"application/vnd.spring-boot.actuator.v3+json": {
36+
"schema": {
37+
"type": "object",
38+
"additionalProperties": {
39+
"type": "object",
40+
"additionalProperties": {
41+
"$ref": "#/components/schemas/Link"
42+
}
43+
}
44+
}
45+
},
46+
"application/vnd.spring-boot.actuator.v2+json": {
47+
"schema": {
48+
"type": "object",
49+
"additionalProperties": {
50+
"type": "object",
51+
"additionalProperties": {
52+
"$ref": "#/components/schemas/Link"
53+
}
54+
}
55+
}
56+
},
57+
"application/json": {
3658
"schema": {
3759
"type": "object",
3860
"additionalProperties": {
@@ -59,7 +81,17 @@
5981
"200": {
6082
"description": "OK",
6183
"content": {
62-
"*/*": {
84+
"application/vnd.spring-boot.actuator.v3+json": {
85+
"schema": {
86+
"type": "object"
87+
}
88+
},
89+
"application/vnd.spring-boot.actuator.v2+json": {
90+
"schema": {
91+
"type": "object"
92+
}
93+
},
94+
"application/json": {
6395
"schema": {
6496
"type": "object"
6597
}
@@ -80,7 +112,17 @@
80112
"200": {
81113
"description": "OK",
82114
"content": {
83-
"*/*": {
115+
"application/vnd.spring-boot.actuator.v3+json": {
116+
"schema": {
117+
"type": "object"
118+
}
119+
},
120+
"application/vnd.spring-boot.actuator.v2+json": {
121+
"schema": {
122+
"type": "object"
123+
}
124+
},
125+
"application/json": {
84126
"schema": {
85127
"type": "object"
86128
}

0 commit comments

Comments
 (0)