Repository: springdoc/springdoc-openapi (java)

Issue:
Default Media Type application/xml is selected by default
**Describe the bug**
I wrote an interface,produces = {"application/json", "application/xml"}, consumes = {"application/json", "application/xml"}
![image](https://github.com/user-attachments/assets/702119fc-02d3-40e2-9d03-23a223409702)
When the springdoc version was 2.5.0, application/json was selected by default
![image](https://github.com/user-attachments/assets/f9542eb2-4823-4398-ba6d-4876a05073c0)
![image](https://github.com/user-attachments/assets/8770343c-68e6-4204-b9d0-de41042523d3)
But when I switched to 2.6.0, the default choice changed to application/xml
![image](https://github.com/user-attachments/assets/2284d012-11ec-4bc1-a1cf-2ec0d4db6028)
![image](https://github.com/user-attachments/assets/5b679fdb-9582-4294-852a-c416de8a6bf9)
I haven't changed anything except the version number of springdoc and I would like to know how to configure the Media type to return to the default application/json value

Pull request:
Ensure default media type order is preserved using LinkedHashSet in mergeArrays. Fixes #2671
#2671 - Ensure default media type order is preserved using LinkedHashSet in mergeArrays

### Description

This PR addresses issue #2671 by ensuring that the order of media types is preserved in the `mergeArrays` method of the `MethodAttributes` class. Previously, `HashSet` was used, which did not guarantee the order of media types, causing the default media type not to be prioritized. This has been fixed by using `LinkedHashSet` to maintain the insertion order.

### Changes

- **MethodAttributes.java**:
  - Modified `mergeArrays` method to use `LinkedHashSet` instead of `HashSet` to preserve the order of media types.

- **MethodAttributesTest.java**:
  - Added unit tests to verify that the `mergeArrays` method preserves the order of media types and that the default media type is prioritized.
  - Tests include scenarios with both non-null and null arrays.

### Testing

Added the following tests to `MethodAttributesTest` to ensure the functionality:
- `testMergeArrays`: Verifies that the order of media types is preserved when merging two arrays.
- `testMergeArraysWithNullArray1`: Verifies that the method handles `null` as the first array correctly.
- `testDefaultProducesMediaType`: Verifies that the default `produces` media type is correctly set.
- `testDefaultConsumesMediaType`: Verifies that the default `consumes` media type is correctly set.

Please review the changes and let me know if any further adjustments are needed.

Thank you!


Patch:
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 @@ package org.springdoc.core.models;
 
 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 @@ public class MethodAttributes {
 	 * @return the string [ ]
 	 */
     private String[] mergeArrays(@Nullable String[] array1, String[] array2) {
-        Set<String> uniqueValues = array1 == null ? new HashSet<>() : Arrays.stream(array1).collect(Collectors.toSet());
-        uniqueValues.addAll(Arrays.asList(array2));
-        return uniqueValues.toArray(new String[0]);
-    }
+		Set<String> 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.


Test patch:
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