diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/models/MethodAttributes.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/models/MethodAttributes.java index 433df153a..ce26b40a0 100644 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/models/MethodAttributes.java +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/models/MethodAttributes.java @@ -26,8 +26,8 @@ import java.lang.reflect.Method; import java.util.Arrays; -import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Locale; import java.util.Map; import java.util.Set; @@ -306,10 +306,10 @@ private void fillMethods(String[] produces, String[] consumes, String[] headers) * @return the string [ ] */ private String[] mergeArrays(@Nullable String[] array1, String[] array2) { - Set uniqueValues = array1 == null ? new HashSet<>() : Arrays.stream(array1).collect(Collectors.toSet()); - uniqueValues.addAll(Arrays.asList(array2)); - return uniqueValues.toArray(new String[0]); - } + Set uniqueValues = array1 == null ? new LinkedHashSet<>() : Arrays.stream(array1).collect(Collectors.toCollection(LinkedHashSet::new)); + uniqueValues.addAll(Arrays.asList(array2)); + return uniqueValues.toArray(new String[0]); + } /** * Is method overloaded boolean. diff --git a/springdoc-openapi-starter-common/src/test/java/org/springdoc/core/model/MethodAttributesTest.java b/springdoc-openapi-starter-common/src/test/java/org/springdoc/core/model/MethodAttributesTest.java new file mode 100644 index 000000000..bec69db22 --- /dev/null +++ b/springdoc-openapi-starter-common/src/test/java/org/springdoc/core/model/MethodAttributesTest.java @@ -0,0 +1,70 @@ +package org.springdoc.core.model; + +import org.junit.jupiter.api.Test; +import org.springdoc.core.models.MethodAttributes; + +import java.lang.reflect.Method; +import java.util.Locale; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class MethodAttributesTest { + + @Test + public void testMergeArrays() throws Exception { + MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH); + + String[] array1 = {"application/json", "application/xml"}; + String[] array2 = {"application/xml", "application/yaml"}; + + String[] expected = {"application/json", "application/xml", "application/yaml"}; + + Method mergeArraysMethod = MethodAttributes.class.getDeclaredMethod("mergeArrays", String[].class, String[].class); + mergeArraysMethod.setAccessible(true); + String[] result = (String[]) mergeArraysMethod.invoke(methodAttributes, (Object) array1, (Object) array2); + + assertArrayEquals(expected, result); + } + + @Test + public void testMergeArraysWithNullArray1() throws Exception { + MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH); + + String[] array1 = null; + String[] array2 = {"application/xml", "application/yaml"}; + + String[] expected = {"application/xml", "application/yaml"}; + + Method mergeArraysMethod = MethodAttributes.class.getDeclaredMethod("mergeArrays", String[].class, String[].class); + mergeArraysMethod.setAccessible(true); + String[] result = (String[]) mergeArraysMethod.invoke(methodAttributes, (Object) array1, (Object) array2); + + assertArrayEquals(expected, result); + } + + @Test + public void testDefaultProducesMediaType() { + MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH); + + Method method = this.getClass().getDeclaredMethods()[0]; + methodAttributes.calculateConsumesProduces(method); + + String[] expectedProduces = {"application/xml"}; + String[] resultProduces = methodAttributes.getMethodProduces(); + + assertArrayEquals(expectedProduces, resultProduces); + } + + @Test + public void testDefaultConsumesMediaType() { + MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH); + + Method method = this.getClass().getDeclaredMethods()[0]; + methodAttributes.calculateConsumesProduces(method); + + String[] expectedConsumes = {"application/json"}; + String[] resultConsumes = methodAttributes.getMethodConsumes(); + + assertArrayEquals(expectedConsumes, resultConsumes); + } +} \ No newline at end of file