Skip to content

Commit d6da952

Browse files
author
jsieczczynski
committed
Correctly calculate consumes and produces
1 parent 8367ba9 commit d6da952

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

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

+35-21
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@
2525
package org.springdoc.core.models;
2626

2727
import java.lang.reflect.Method;
28+
import java.util.Arrays;
29+
import java.util.HashSet;
2830
import java.util.LinkedHashMap;
2931
import java.util.Locale;
3032
import java.util.Map;
3133

3234
import com.fasterxml.jackson.annotation.JsonView;
3335
import io.swagger.v3.oas.models.responses.ApiResponse;
3436
import io.swagger.v3.oas.models.responses.ApiResponses;
37+
import java.util.Set;
38+
import java.util.stream.Collectors;
3539
import org.apache.commons.lang3.ArrayUtils;
3640
import org.apache.commons.lang3.StringUtils;
3741

42+
import org.jetbrains.annotations.Nullable;
3843
import org.springframework.core.annotation.AnnotatedElementUtils;
39-
import org.springframework.util.CollectionUtils;
4044
import org.springframework.web.bind.annotation.DeleteMapping;
4145
import org.springframework.web.bind.annotation.GetMapping;
4246
import org.springframework.web.bind.annotation.PostMapping;
@@ -275,28 +279,38 @@ else if (reqMappingClass != null) {
275279
* @param headers the headers
276280
*/
277281
private void fillMethods(String[] produces, String[] consumes, String[] headers) {
278-
if (ArrayUtils.isEmpty(methodProduces)) {
279-
if (ArrayUtils.isNotEmpty(produces))
280-
methodProduces = produces;
281-
else if (ArrayUtils.isNotEmpty(classProduces))
282-
methodProduces = classProduces;
283-
else
284-
methodProduces = new String[] { defaultProducesMediaType };
285-
}
286-
287-
if (ArrayUtils.isEmpty(methodConsumes)) {
288-
if (ArrayUtils.isNotEmpty(consumes))
289-
methodConsumes = consumes;
290-
else if (ArrayUtils.isNotEmpty(classConsumes))
291-
methodConsumes = classConsumes;
292-
else
293-
methodConsumes = new String[] { defaultConsumesMediaType };
294-
}
295-
296-
if (CollectionUtils.isEmpty(this.headers))
297-
setHeaders(headers);
282+
if (ArrayUtils.isNotEmpty(produces)) {
283+
methodProduces = mergeArrays(methodProduces, produces);
284+
} else if (ArrayUtils.isNotEmpty(classProduces)) {
285+
methodProduces = mergeArrays(methodProduces, classProduces);
286+
} else if (ArrayUtils.isEmpty(methodProduces)) {
287+
methodProduces = new String[] {defaultProducesMediaType};
288+
}
289+
290+
if (ArrayUtils.isNotEmpty(consumes)) {
291+
methodConsumes = mergeArrays(methodConsumes, consumes);
292+
} else if (ArrayUtils.isNotEmpty(classConsumes)) {
293+
methodConsumes = mergeArrays(methodConsumes, classConsumes);
294+
} else if (ArrayUtils.isEmpty(methodConsumes)) {
295+
methodConsumes = new String[] {defaultConsumesMediaType};
296+
}
297+
298+
setHeaders(headers);
298299
}
299300

301+
/**
302+
* Merge string arrays into one array with unique values
303+
*
304+
* @param array1 the array1
305+
* @param array2 the array2
306+
* @return the string [ ]
307+
*/
308+
private String[] mergeArrays(@Nullable String[] array1, String[] array2) {
309+
Set<String> uniqueValues = array1 == null ? new HashSet<>() : Arrays.stream(array1).collect(Collectors.toSet());
310+
uniqueValues.addAll(Arrays.asList(array2));
311+
return uniqueValues.toArray(new String[0]);
312+
}
313+
300314
/**
301315
* Is method overloaded boolean.
302316
*

0 commit comments

Comments
 (0)