Skip to content

Commit ae5ab58

Browse files
committed
Merge branch 'fix-default-media-type' of https://github.com/limehee/springdoc-openapi into limehee-fix-default-media-type
2 parents 1208e38 + ca438ab commit ae5ab58

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

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

+5-5
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;
@@ -306,10 +306,10 @@ private void fillMethods(String[] produces, String[] consumes, String[] headers)
306306
* @return the string [ ]
307307
*/
308308
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-
}
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,70 @@
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() throws Exception {
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+
22+
Method mergeArraysMethod = MethodAttributes.class.getDeclaredMethod("mergeArrays", String[].class, String[].class);
23+
mergeArraysMethod.setAccessible(true);
24+
String[] result = (String[]) mergeArraysMethod.invoke(methodAttributes, (Object) array1, (Object) array2);
25+
26+
assertArrayEquals(expected, result);
27+
}
28+
29+
@Test
30+
public void testMergeArraysWithNullArray1() throws Exception {
31+
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH);
32+
33+
String[] array1 = null;
34+
String[] array2 = {"application/xml", "application/yaml"};
35+
36+
String[] expected = {"application/xml", "application/yaml"};
37+
38+
Method mergeArraysMethod = MethodAttributes.class.getDeclaredMethod("mergeArrays", String[].class, String[].class);
39+
mergeArraysMethod.setAccessible(true);
40+
String[] result = (String[]) mergeArraysMethod.invoke(methodAttributes, (Object) array1, (Object) array2);
41+
42+
assertArrayEquals(expected, result);
43+
}
44+
45+
@Test
46+
public void testDefaultProducesMediaType() {
47+
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH);
48+
49+
Method method = this.getClass().getDeclaredMethods()[0];
50+
methodAttributes.calculateConsumesProduces(method);
51+
52+
String[] expectedProduces = {"application/xml"};
53+
String[] resultProduces = methodAttributes.getMethodProduces();
54+
55+
assertArrayEquals(expectedProduces, resultProduces);
56+
}
57+
58+
@Test
59+
public void testDefaultConsumesMediaType() {
60+
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH);
61+
62+
Method method = this.getClass().getDeclaredMethods()[0];
63+
methodAttributes.calculateConsumesProduces(method);
64+
65+
String[] expectedConsumes = {"application/json"};
66+
String[] resultConsumes = methodAttributes.getMethodConsumes();
67+
68+
assertArrayEquals(expectedConsumes, resultConsumes);
69+
}
70+
}

0 commit comments

Comments
 (0)