Skip to content

Commit 3bd2693

Browse files
committed
Custom Requestmapping consumes responses. Fixes #1546
1 parent f39b640 commit 3bd2693

File tree

14 files changed

+1448
-145
lines changed

14 files changed

+1448
-145
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ protected void calculatePath(RouterOperation routerOperation, Locale locale) {
609609
* @param locale the locale
610610
*/
611611
protected void calculatePath(HandlerMethod handlerMethod, String operationPath,
612-
Set<RequestMethod> requestMethods, Locale locale) {
613-
this.calculatePath(handlerMethod, new RouterOperation(operationPath, requestMethods.toArray(new RequestMethod[requestMethods.size()])), locale);
612+
Set<RequestMethod> requestMethods,String[] consumes, String[] produces, String[] headers, Locale locale) {
613+
this.calculatePath(handlerMethod, new RouterOperation(operationPath, requestMethods.toArray(new RequestMethod[requestMethods.size()]), consumes, produces, headers), locale);
614614
}
615615

616616
/**

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.commons.lang3.ArrayUtils;
3232

3333
import org.springframework.core.annotation.AnnotatedElementUtils;
34+
import org.springframework.util.CollectionUtils;
3435
import org.springframework.web.bind.annotation.DeleteMapping;
3536
import org.springframework.web.bind.annotation.GetMapping;
3637
import org.springframework.web.bind.annotation.PostMapping;
@@ -269,21 +270,26 @@ else if (reqMappingClass != null) {
269270
* @param headers the headers
270271
*/
271272
private void fillMethods(String[] produces, String[] consumes, String[] headers) {
272-
if (ArrayUtils.isNotEmpty(produces))
273-
methodProduces = produces;
274-
else if (ArrayUtils.isNotEmpty(classProduces))
275-
methodProduces = classProduces;
276-
else
277-
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+
}
278281

279-
if (ArrayUtils.isNotEmpty(consumes))
280-
methodConsumes = consumes;
281-
else if (ArrayUtils.isNotEmpty(classConsumes))
282-
methodConsumes = classConsumes;
283-
else
284-
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+
}
285290

286-
setHeaders(headers);
291+
if (CollectionUtils.isEmpty(this.headers))
292+
setHeaders(headers);
287293
}
288294

289295
/**

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,12 @@ public RouterOperation(org.springdoc.core.annotations.RouterOperation routerOper
141141
* @param path the path
142142
* @param methods the methods
143143
*/
144-
public RouterOperation(String path, RequestMethod[] methods) {
144+
public RouterOperation(String path, RequestMethod[] methods,String[] consumes, String[] produces, String[] headers) {
145145
this.path = path;
146146
this.methods = methods;
147+
this.consumes=consumes;
148+
this.produces=produces;
149+
this.headers=headers;
147150
}
148151

149152
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ else if (ControllerType.PROPERTY.equals(controllerType))
280280
MethodResourceMapping methodResourceMapping, HandlerMethod handlerMethod,
281281
RequestMethod requestMethod, ResourceMetadata resourceMetadata, String
282282
operationPath, ControllerType controllerType) {
283-
RouterOperation routerOperation = new RouterOperation(operationPath, new RequestMethod[] { requestMethod });
283+
RouterOperation routerOperation = new RouterOperation(operationPath, new RequestMethod[] { requestMethod }, null, null, null);
284284
MethodAttributes methodAttributes = new MethodAttributes(springDocConfigProperties.getDefaultConsumesMediaType(), springDocConfigProperties.getDefaultProducesMediaType(), dataRestRepository.getLocale());
285285
methodAttributes.calculateConsumesProduces(handlerMethod.getMethod());
286286
routerOperation.setConsumes(methodAttributes.getMethodConsumes());

springdoc-openapi-data-rest/src/test/resources/results/app22.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@
324324
"schema": {
325325
"$ref": "#/components/schemas/Person"
326326
}
327+
},
328+
"application/json": {
329+
"schema": {
330+
"$ref": "#/components/schemas/Person"
331+
}
332+
},
333+
"application/prs.hal-forms+json": {
334+
"schema": {
335+
"$ref": "#/components/schemas/Person"
336+
}
327337
}
328338
}
329339
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ && isFilterCondition(handlerMethod, operationPath, produces, consumes, headers))
197197
// default allowed requestmethods
198198
if (requestMethods.isEmpty())
199199
requestMethods = this.getDefaultAllowedHttpMethods();
200-
calculatePath(handlerMethod, operationPath, requestMethods, locale);
200+
calculatePath(handlerMethod, operationPath, requestMethods, consumes, produces, headers, locale);
201201
}
202202
}
203203
}

0 commit comments

Comments
 (0)