Skip to content

Commit

Permalink
Fix produces content type
Browse files Browse the repository at this point in the history
Fixes #105
  • Loading branch information
neiser committed Sep 5, 2022
1 parent 46702e8 commit 39ecced
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.qaware.openapigeneratorforspring.test.app55;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
class App55 {
public static void main(String[] args) {
SpringApplication.run(App55.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.qaware.openapigeneratorforspring.test.app55;

import lombok.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class App55Controller {

@GetMapping
public SomeDto getJson() {
return new SomeDto("some-value");
}

@Value
private static class SomeDto {
String someProperty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.qaware.openapigeneratorforspring.test.app55;

import de.qaware.openapigeneratorforspring.test.AbstractOpenApiGeneratorWebMvcIntTest;

class App55Test extends AbstractOpenApiGeneratorWebMvcIntTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"200": {
"description": "Default response",
"content": {
"*/*": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/App34Controller.SomeListDto"
}
Expand All @@ -29,7 +29,7 @@
"200": {
"description": "Default response",
"content": {
"*/*": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/App34Controller.SomeDto"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"200": {
"description": "Default response",
"content": {
"*/*": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/App42Controller.SomeType1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"200": {
"description": "Default response",
"content": {
"*/*": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/array_string_1"
}
Expand Down Expand Up @@ -69,7 +69,7 @@
"200": {
"description": "Default response",
"content": {
"*/*": {
"application/json": {
"schema": {
"type": "array",
"description": "Description for array",
Expand All @@ -91,7 +91,7 @@
"200": {
"description": "Default response",
"content": {
"*/*": {
"application/json": {
"schema": {
"maxItems": 5,
"minItems": 2,
Expand All @@ -116,7 +116,7 @@
"200": {
"description": "Default response",
"content": {
"*/*": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/array_string_0"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"openapi": "3.0.1",
"info": {
"title": "API for App55",
"version": "unknown"
},
"paths": {
"/": {
"get": {
"operationId": "getJson",
"responses": {
"200": {
"description": "Default response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/App55Controller.SomeDto"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"App55Controller.SomeDto": {
"type": "object",
"properties": {
"someProperty": {
"type": "string"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ paths:
"200":
description: Default response
content:
'*/*':
application/json:
schema:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> findConsumesContentTypes(SpringWebHandlerMethod handlerMethod) {
return fromRequestMappingAnnotation(handlerMethod, RequestMapping::consumes);
return fromRequestMappingAnnotation(handlerMethod, RequestMapping::consumes, DEFAULT_CONSUMES_CONTENT_TYPE);
}

public Set<String> findProducesContentTypes(SpringWebHandlerMethod handlerMethod) {
return fromRequestMappingAnnotation(handlerMethod, RequestMapping::produces);
return fromRequestMappingAnnotation(handlerMethod, RequestMapping::produces, DEFAULT_PRODUCES_CONTENT_TYPE);
}

private static Set<String> fromRequestMappingAnnotation(HandlerMethod handlerMethod, Function<RequestMapping, String[]> annotationMapper) {
private static Set<String> fromRequestMappingAnnotation(HandlerMethod handlerMethod, Function<RequestMapping, String[]> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -57,9 +58,9 @@ public List<HandlerMethod.RequestBody> mergeRequestBodies(List<SpringWebHandlerM
.map(handlerMethod -> Pair.of(contentTypesMapper.findConsumesContentTypes(handlerMethod), requestBodyParameterMapper.findRequestBodyParameter(handlerMethod)))
.collect(groupingByPairKeyAndCollectingValuesToList());

List<Optional<RequestBodyParameter>> allValueRequestBodyParameters = groupedRequestBodyParameters.get(SINGLE_ALL_VALUE);
List<Optional<RequestBodyParameter>> 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();
}
Expand Down

0 comments on commit 39ecced

Please sign in to comment.