Skip to content

Commit a9e5672

Browse files
author
bnasslahsen
committed
Discovery of MediaType producers inconsistent with Spring MVC behaviour. Fixes #426
1 parent db60468 commit a9e5672

File tree

5 files changed

+43
-64
lines changed

5 files changed

+43
-64
lines changed

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseBuilder.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@ private void buildContentFromDoc(Components components, ApiResponses apiResponse
151151
io.swagger.v3.oas.annotations.responses.ApiResponse apiResponseAnnotations, ApiResponse apiResponse,
152152
io.swagger.v3.oas.annotations.media.Content[] contentdoc) {
153153
Optional<Content> optionalContent = SpringDocAnnotationsUtils.getContent(contentdoc, new String[0],
154-
methodAttributes.getAllProduces(), null, components, methodAttributes.getJsonViewAnnotation());
154+
methodAttributes.getMethodProduces(), null, components, methodAttributes.getJsonViewAnnotation());
155155
if (apiResponsesOp.containsKey(apiResponseAnnotations.responseCode())) {
156156
// Merge with the existing content
157157
Content existingContent = apiResponsesOp.get(apiResponseAnnotations.responseCode()).getContent();
158158
if (optionalContent.isPresent() && existingContent != null) {
159159
Content newContent = optionalContent.get();
160160
if (methodAttributes.isMethodOverloaded()) {
161-
Arrays.stream(methodAttributes.getAllProduces()).filter(mediaTypeStr -> (newContent.get(mediaTypeStr) != null)).forEach(mediaTypeStr -> mergeSchema(existingContent, newContent.get(mediaTypeStr).getSchema(), mediaTypeStr));
161+
Arrays.stream(methodAttributes.getMethodProduces()).filter(mediaTypeStr -> (newContent.get(mediaTypeStr) != null)).forEach(mediaTypeStr -> mergeSchema(existingContent, newContent.get(mediaTypeStr).getSchema(), mediaTypeStr));
162162
apiResponse.content(existingContent);
163163
}
164164
else
@@ -273,7 +273,7 @@ private void buildApiResponses(Components components, Method method, ApiResponse
273273
// No documentation
274274
if (StringUtils.isBlank(apiResponse.get$ref())) {
275275
if (apiResponse.getContent() == null) {
276-
Content content = buildContent(components, method, methodAttributes.getAllProduces(),
276+
Content content = buildContent(components, method, methodAttributes.getMethodProduces(),
277277
methodAttributes.getJsonViewAnnotation());
278278
apiResponse.setContent(content);
279279
}
@@ -290,8 +290,8 @@ else if (CollectionUtils.isEmpty(apiResponse.getContent())) {
290290
Content existingContent = apiResponse.getContent();
291291
Schema<?> schemaN = calculateSchema(components, method.getGenericReturnType(),
292292
methodAttributes.getJsonViewAnnotation());
293-
if (schemaN != null && ArrayUtils.isNotEmpty(methodAttributes.getAllProduces())) {
294-
Arrays.stream(methodAttributes.getAllProduces()).forEach(mediaTypeStr -> mergeSchema(existingContent, schemaN, mediaTypeStr));
293+
if (schemaN != null && ArrayUtils.isNotEmpty(methodAttributes.getMethodProduces())) {
294+
Arrays.stream(methodAttributes.getMethodProduces()).forEach(mediaTypeStr -> mergeSchema(existingContent, schemaN, mediaTypeStr));
295295
}
296296
}
297297
apiResponsesOp.addApiResponse(httpCode, apiResponse);

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,20 @@ else if (reqMappingClass != null) {
117117
}
118118

119119
private void fillMethods(String[] produces, String[] consumes) {
120-
methodProduces = ArrayUtils.isNotEmpty(produces) ? produces : new String[] { MediaType.ALL_VALUE };
121-
methodConsumes = ArrayUtils.isNotEmpty(consumes) ? consumes : new String[] { MediaType.APPLICATION_JSON_VALUE };
122-
}
123-
124-
public String[] getAllConsumes() {
125-
return ArrayUtils.addAll(methodConsumes, classConsumes);
126-
}
120+
if (ArrayUtils.isNotEmpty(produces))
121+
methodProduces = produces;
122+
else if (ArrayUtils.isNotEmpty(classProduces))
123+
methodProduces = classProduces;
124+
else
125+
methodProduces = new String[] { MediaType.ALL_VALUE };
126+
127+
if (ArrayUtils.isNotEmpty(consumes))
128+
methodConsumes = consumes;
129+
else if (ArrayUtils.isNotEmpty(classConsumes))
130+
methodConsumes = classConsumes;
131+
else
132+
methodConsumes = new String[] { MediaType.APPLICATION_JSON_VALUE };
127133

128-
public String[] getAllProduces() {
129-
return ArrayUtils.addAll(methodProduces, classProduces);
130134
}
131135

132136
public boolean isMethodOverloaded() {

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/core/RequestBodyBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void buildContent(RequestBody requestBody, MethodAttributes methodAttrib
147147
}
148148

149149
private void buildContent(RequestBody requestBody, MethodAttributes methodAttributes, Schema<?> schema, Content content) {
150-
for (String value : methodAttributes.getAllConsumes()) {
150+
for (String value : methodAttributes.getMethodConsumes()) {
151151
io.swagger.v3.oas.models.media.MediaType mediaTypeObject = new io.swagger.v3.oas.models.media.MediaType();
152152
mediaTypeObject.setSchema(schema);
153153
if (content.get(value) != null)

Diff for: springdoc-openapi-data-rest/src/test/resources/results/app3.json

-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@
5252
"200": {
5353
"description": "default response",
5454
"content": {
55-
"*/*": {
56-
"schema": {
57-
"type": "string"
58-
}
59-
},
6055
"text/plain": {
6156
"schema": {
6257
"type": "string"

Diff for: springdoc-openapi-webmvc-core/src/test/resources/results/app67.json

+24-44
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
}
1212
],
1313
"paths": {
14-
"/demo/operation4": {
14+
"/demo/operation2": {
1515
"get": {
1616
"tags": [
1717
"hello-controller"
1818
],
19-
"summary": "Operation 4 (expected result - 3 parameters)",
20-
"operationId": "operation4",
19+
"summary": "Operation 2 (expected result - 3 parameters)",
20+
"operationId": "operation2",
2121
"parameters": [
2222
{
2323
"name": "pageNumber",
@@ -48,37 +48,6 @@
4848
"200": {
4949
"description": "default response",
5050
"content": {
51-
"*/*": {
52-
"schema": {
53-
"type": "string"
54-
}
55-
},
56-
"text/plain": {
57-
"schema": {
58-
"type": "string"
59-
}
60-
}
61-
}
62-
}
63-
}
64-
}
65-
},
66-
"/demo/operation1": {
67-
"get": {
68-
"tags": [
69-
"hello-controller"
70-
],
71-
"summary": "Operation 1 (expected result - no parameters)",
72-
"operationId": "operation1",
73-
"responses": {
74-
"200": {
75-
"description": "default response",
76-
"content": {
77-
"*/*": {
78-
"schema": {
79-
"type": "string"
80-
}
81-
},
8251
"text/plain": {
8352
"schema": {
8453
"type": "string"
@@ -126,11 +95,27 @@
12695
"200": {
12796
"description": "default response",
12897
"content": {
129-
"*/*": {
98+
"text/plain": {
13099
"schema": {
131100
"type": "string"
132101
}
133-
},
102+
}
103+
}
104+
}
105+
}
106+
}
107+
},
108+
"/demo/operation1": {
109+
"get": {
110+
"tags": [
111+
"hello-controller"
112+
],
113+
"summary": "Operation 1 (expected result - no parameters)",
114+
"operationId": "operation1",
115+
"responses": {
116+
"200": {
117+
"description": "default response",
118+
"content": {
134119
"text/plain": {
135120
"schema": {
136121
"type": "string"
@@ -141,13 +126,13 @@
141126
}
142127
}
143128
},
144-
"/demo/operation2": {
129+
"/demo/operation4": {
145130
"get": {
146131
"tags": [
147132
"hello-controller"
148133
],
149-
"summary": "Operation 2 (expected result - 3 parameters)",
150-
"operationId": "operation2",
134+
"summary": "Operation 4 (expected result - 3 parameters)",
135+
"operationId": "operation4",
151136
"parameters": [
152137
{
153138
"name": "pageNumber",
@@ -178,11 +163,6 @@
178163
"200": {
179164
"description": "default response",
180165
"content": {
181-
"*/*": {
182-
"schema": {
183-
"type": "string"
184-
}
185-
},
186166
"text/plain": {
187167
"schema": {
188168
"type": "string"

0 commit comments

Comments
 (0)