|
25 | 25 | package org.springdoc.core.models; |
26 | 26 |
|
27 | 27 | import java.lang.reflect.Method; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.HashSet; |
28 | 30 | import java.util.LinkedHashMap; |
29 | 31 | import java.util.Locale; |
30 | 32 | import java.util.Map; |
31 | 33 |
|
32 | 34 | import com.fasterxml.jackson.annotation.JsonView; |
33 | 35 | import io.swagger.v3.oas.models.responses.ApiResponse; |
34 | 36 | import io.swagger.v3.oas.models.responses.ApiResponses; |
| 37 | +import java.util.Set; |
| 38 | +import java.util.stream.Collectors; |
35 | 39 | import org.apache.commons.lang3.ArrayUtils; |
36 | 40 | import org.apache.commons.lang3.StringUtils; |
37 | 41 |
|
| 42 | +import org.jetbrains.annotations.Nullable; |
38 | 43 | import org.springframework.core.annotation.AnnotatedElementUtils; |
39 | | -import org.springframework.util.CollectionUtils; |
40 | 44 | import org.springframework.web.bind.annotation.DeleteMapping; |
41 | 45 | import org.springframework.web.bind.annotation.GetMapping; |
42 | 46 | import org.springframework.web.bind.annotation.PostMapping; |
@@ -275,28 +279,38 @@ else if (reqMappingClass != null) { |
275 | 279 | * @param headers the headers |
276 | 280 | */ |
277 | 281 | 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); |
298 | 299 | } |
299 | 300 |
|
| 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 | + |
300 | 314 | /** |
301 | 315 | * Is method overloaded boolean. |
302 | 316 | * |
|
0 commit comments