Skip to content

Commit 2ad1ce0

Browse files
committed
Ensure default media type is prioritized in SpringDoc configuration (Fixes #2671)
1 parent 71a0684 commit 2ad1ce0

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
import java.lang.reflect.Method;
2828
import java.util.Arrays;
29-
import java.util.HashSet;
3029
import java.util.LinkedHashMap;
30+
import java.util.LinkedHashSet;
3131
import java.util.Locale;
3232
import java.util.Map;
3333
import java.util.Set;
@@ -305,11 +305,11 @@ private void fillMethods(String[] produces, String[] consumes, String[] headers)
305305
* @param array2 the array2
306306
* @return the string [ ]
307307
*/
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-
}
308+
public String[] mergeArrays(@Nullable String[] array1, String[] array2) {
309+
Set<String> uniqueValues = array1 == null ? new LinkedHashSet<>() : Arrays.stream(array1).collect(Collectors.toCollection(LinkedHashSet::new));
310+
uniqueValues.addAll(Arrays.asList(array2));
311+
return uniqueValues.toArray(new String[0]);
312+
}
313313

314314
/**
315315
* Is method overloaded boolean.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.springdoc.core.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springdoc.core.models.MethodAttributes;
5+
6+
import java.lang.reflect.Method;
7+
import java.util.Locale;
8+
9+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
10+
11+
public class MethodAttributesTest {
12+
13+
@Test
14+
public void testMergeArrays() {
15+
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH);
16+
17+
String[] array1 = {"application/json", "application/xml"};
18+
String[] array2 = {"application/xml", "application/yaml"};
19+
20+
String[] expected = {"application/json", "application/xml", "application/yaml"};
21+
String[] result = methodAttributes.mergeArrays(array1, array2);
22+
23+
assertArrayEquals(expected, result);
24+
}
25+
26+
@Test
27+
public void testMergeArraysWithNullArray1() {
28+
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH);
29+
30+
String[] array1 = null;
31+
String[] array2 = {"application/xml", "application/yaml"};
32+
33+
String[] expected = {"application/xml", "application/yaml"};
34+
String[] result = methodAttributes.mergeArrays(array1, array2);
35+
36+
assertArrayEquals(expected, result);
37+
}
38+
39+
@Test
40+
public void testDefaultProducesMediaType() {
41+
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH);
42+
43+
Method method = this.getClass().getDeclaredMethods()[0];
44+
methodAttributes.calculateConsumesProduces(method);
45+
46+
String[] expectedProduces = {"application/xml"};
47+
String[] resultProduces = methodAttributes.getMethodProduces();
48+
49+
assertArrayEquals(expectedProduces, resultProduces);
50+
}
51+
52+
@Test
53+
public void testDefaultConsumesMediaType() {
54+
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH);
55+
56+
Method method = this.getClass().getDeclaredMethods()[0];
57+
methodAttributes.calculateConsumesProduces(method);
58+
59+
String[] expectedConsumes = {"application/json"};
60+
String[] resultConsumes = methodAttributes.getMethodConsumes();
61+
62+
assertArrayEquals(expectedConsumes, resultConsumes);
63+
}
64+
}

0 commit comments

Comments
 (0)