From 38c47396e31b8a2d86c8eaf9c0acc51635601b08 Mon Sep 17 00:00:00 2001 From: Andreas Grub Date: Mon, 5 Sep 2022 09:01:21 +0200 Subject: [PATCH] Fix produces content type Fixes #105 --- .../src/test/resources/openApiJson/app34.json | 4 ++-- .../src/test/resources/openApiJson/app42.json | 2 +- .../src/test/resources/openApiJson/app47.json | 8 ++++---- .../src/test/resources/openApiYaml/app38.yaml | 2 +- .../SpringWebHandlerMethodContentTypesMapper.java | 15 ++++++++------- .../SpringWebHandlerMethodRequestBodyMerger.java | 7 ++++--- 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/openapi-generator-for-spring-test/src/test/resources/openApiJson/app34.json b/openapi-generator-for-spring-test/src/test/resources/openApiJson/app34.json index c32c749b..775185fc 100644 --- a/openapi-generator-for-spring-test/src/test/resources/openApiJson/app34.json +++ b/openapi-generator-for-spring-test/src/test/resources/openApiJson/app34.json @@ -12,7 +12,7 @@ "200": { "description": "Default response", "content": { - "*/*": { + "application/json": { "schema": { "$ref": "#/components/schemas/App34Controller.SomeListDto" } @@ -29,7 +29,7 @@ "200": { "description": "Default response", "content": { - "*/*": { + "application/json": { "schema": { "$ref": "#/components/schemas/App34Controller.SomeDto" } diff --git a/openapi-generator-for-spring-test/src/test/resources/openApiJson/app42.json b/openapi-generator-for-spring-test/src/test/resources/openApiJson/app42.json index cac369b8..dc8b48ce 100644 --- a/openapi-generator-for-spring-test/src/test/resources/openApiJson/app42.json +++ b/openapi-generator-for-spring-test/src/test/resources/openApiJson/app42.json @@ -12,7 +12,7 @@ "200": { "description": "Default response", "content": { - "*/*": { + "application/json": { "schema": { "$ref": "#/components/schemas/App42Controller.SomeType1" } diff --git a/openapi-generator-for-spring-test/src/test/resources/openApiJson/app47.json b/openapi-generator-for-spring-test/src/test/resources/openApiJson/app47.json index 23ac9a01..74fbef89 100644 --- a/openapi-generator-for-spring-test/src/test/resources/openApiJson/app47.json +++ b/openapi-generator-for-spring-test/src/test/resources/openApiJson/app47.json @@ -32,7 +32,7 @@ "200": { "description": "Default response", "content": { - "*/*": { + "application/json": { "schema": { "$ref": "#/components/schemas/array_string_1" } @@ -69,7 +69,7 @@ "200": { "description": "Default response", "content": { - "*/*": { + "application/json": { "schema": { "type": "array", "description": "Description for array", @@ -91,7 +91,7 @@ "200": { "description": "Default response", "content": { - "*/*": { + "application/json": { "schema": { "maxItems": 5, "minItems": 2, @@ -116,7 +116,7 @@ "200": { "description": "Default response", "content": { - "*/*": { + "application/json": { "schema": { "$ref": "#/components/schemas/array_string_0" } diff --git a/openapi-generator-for-spring-test/src/test/resources/openApiYaml/app38.yaml b/openapi-generator-for-spring-test/src/test/resources/openApiYaml/app38.yaml index a6b059ac..3b36dd06 100644 --- a/openapi-generator-for-spring-test/src/test/resources/openApiYaml/app38.yaml +++ b/openapi-generator-for-spring-test/src/test/resources/openApiYaml/app38.yaml @@ -22,6 +22,6 @@ paths: "200": description: Default response content: - '*/*': + application/json: schema: type: string \ No newline at end of file diff --git a/openapi-generator-for-spring-web/src/main/java/de/qaware/openapigeneratorforspring/common/paths/method/SpringWebHandlerMethodContentTypesMapper.java b/openapi-generator-for-spring-web/src/main/java/de/qaware/openapigeneratorforspring/common/paths/method/SpringWebHandlerMethodContentTypesMapper.java index dce13f1b..b01c98c2 100644 --- a/openapi-generator-for-spring-web/src/main/java/de/qaware/openapigeneratorforspring/common/paths/method/SpringWebHandlerMethodContentTypesMapper.java +++ b/openapi-generator-for-spring-web/src/main/java/de/qaware/openapigeneratorforspring/common/paths/method/SpringWebHandlerMethodContentTypesMapper.java @@ -30,29 +30,30 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static java.util.Collections.singleton; import static org.springframework.http.MediaType.ALL_VALUE; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; public class SpringWebHandlerMethodContentTypesMapper { - public static final Set SINGLE_ALL_VALUE = singleton(ALL_VALUE); + public static final String DEFAULT_CONSUMES_CONTENT_TYPE = ALL_VALUE; + public static final String DEFAULT_PRODUCES_CONTENT_TYPE = APPLICATION_JSON_VALUE; public Set findConsumesContentTypes(SpringWebHandlerMethod handlerMethod) { - return fromRequestMappingAnnotation(handlerMethod, RequestMapping::consumes); + return fromRequestMappingAnnotation(handlerMethod, RequestMapping::consumes, DEFAULT_CONSUMES_CONTENT_TYPE); } public Set findProducesContentTypes(SpringWebHandlerMethod handlerMethod) { - return fromRequestMappingAnnotation(handlerMethod, RequestMapping::produces); + return fromRequestMappingAnnotation(handlerMethod, RequestMapping::produces, DEFAULT_PRODUCES_CONTENT_TYPE); } - private static Set fromRequestMappingAnnotation(HandlerMethod handlerMethod, Function annotationMapper) { + private static Set fromRequestMappingAnnotation(HandlerMethod handlerMethod, Function annotationMapper, String defaultValue) { return handlerMethod.findAnnotations(RequestMapping.class) .map(annotationMapper) .filter(contentTypes -> !StringUtils.isAllBlank(contentTypes)) // Spring doc says the first one should win, - // ie. annotation on class level is overridden by method level + // i.e. annotation on class level is overridden by method level .findFirst() .map(Stream::of) - .orElse(Stream.of(ALL_VALUE)) + .orElse(Stream.of(defaultValue)) .collect(Collectors.toCollection(LinkedHashSet::new)); } } diff --git a/openapi-generator-for-spring-web/src/main/java/de/qaware/openapigeneratorforspring/common/paths/method/merger/SpringWebHandlerMethodRequestBodyMerger.java b/openapi-generator-for-spring-web/src/main/java/de/qaware/openapigeneratorforspring/common/paths/method/merger/SpringWebHandlerMethodRequestBodyMerger.java index 98f2594e..ecfdd7d0 100644 --- a/openapi-generator-for-spring-web/src/main/java/de/qaware/openapigeneratorforspring/common/paths/method/merger/SpringWebHandlerMethodRequestBodyMerger.java +++ b/openapi-generator-for-spring-web/src/main/java/de/qaware/openapigeneratorforspring/common/paths/method/merger/SpringWebHandlerMethodRequestBodyMerger.java @@ -39,9 +39,10 @@ import java.util.Set; import java.util.stream.Collectors; -import static de.qaware.openapigeneratorforspring.common.paths.method.SpringWebHandlerMethodContentTypesMapper.SINGLE_ALL_VALUE; +import static de.qaware.openapigeneratorforspring.common.paths.method.SpringWebHandlerMethodContentTypesMapper.DEFAULT_CONSUMES_CONTENT_TYPE; import static de.qaware.openapigeneratorforspring.common.util.OpenApiStreamUtils.groupingByPairKeyAndCollectingValuesToList; import static java.util.Collections.emptyList; +import static java.util.Collections.singleton; @RequiredArgsConstructor public class SpringWebHandlerMethodRequestBodyMerger { @@ -57,9 +58,9 @@ public List mergeRequestBodies(List Pair.of(contentTypesMapper.findConsumesContentTypes(handlerMethod), requestBodyParameterMapper.findRequestBodyParameter(handlerMethod))) .collect(groupingByPairKeyAndCollectingValuesToList()); - List> allValueRequestBodyParameters = groupedRequestBodyParameters.get(SINGLE_ALL_VALUE); + List> defaultValueRequestBodyParameters = groupedRequestBodyParameters.get(singleton(DEFAULT_CONSUMES_CONTENT_TYPE)); if (groupedRequestBodyParameters.size() == 1 - && allValueRequestBodyParameters != null && allValueRequestBodyParameters.stream().noneMatch(Optional::isPresent)) { + && defaultValueRequestBodyParameters != null && defaultValueRequestBodyParameters.stream().noneMatch(Optional::isPresent)) { // we can omit the request bodies entirely if there's only empty request bodies matching for ALL_VALUE return emptyList(); }