From 2ad1ce0cc60c14950eb33bfaab7d6e3b499499c2 Mon Sep 17 00:00:00 2001 From: limehee Date: Thu, 8 Aug 2024 12:00:02 +0900 Subject: [PATCH 1/2] Ensure default media type is prioritized in SpringDoc configuration (Fixes #2671) --- .../core/models/MethodAttributes.java | 12 ++-- .../core/model/MethodAttributesTest.java | 64 +++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 springdoc-openapi-starter-common/src/test/java/org/springdoc/core/model/MethodAttributesTest.java 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..8a90530f3 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; @@ -305,11 +305,11 @@ private void fillMethods(String[] produces, String[] consumes, String[] headers) * @param array2 the array2 * @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]); - } + public String[] mergeArrays(@Nullable String[] array1, String[] array2) { + 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..d002270e7 --- /dev/null +++ b/springdoc-openapi-starter-common/src/test/java/org/springdoc/core/model/MethodAttributesTest.java @@ -0,0 +1,64 @@ +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() { + 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"}; + String[] result = methodAttributes.mergeArrays(array1, array2); + + assertArrayEquals(expected, result); + } + + @Test + public void testMergeArraysWithNullArray1() { + 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"}; + String[] result = methodAttributes.mergeArrays(array1, 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); + } +} From ca438abb912eceee23645f7a5fe683e528c5458a Mon Sep 17 00:00:00 2001 From: limehee Date: Thu, 8 Aug 2024 12:56:22 +0900 Subject: [PATCH 2/2] test: Use Reflection to test private mergeArrays method in MethodAttributes --- .../springdoc/core/models/MethodAttributes.java | 2 +- .../core/model/MethodAttributesTest.java | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) 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 8a90530f3..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 @@ -305,7 +305,7 @@ private void fillMethods(String[] produces, String[] consumes, String[] headers) * @param array2 the array2 * @return the string [ ] */ - public String[] mergeArrays(@Nullable String[] array1, String[] array2) { + private String[] mergeArrays(@Nullable String[] array1, String[] array2) { 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]); 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 index d002270e7..bec69db22 100644 --- 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 @@ -11,27 +11,33 @@ public class MethodAttributesTest { @Test - public void testMergeArrays() { + 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"}; - String[] result = methodAttributes.mergeArrays(array1, array2); + + 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() { + 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"}; - String[] result = methodAttributes.mergeArrays(array1, array2); + + 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); } @@ -61,4 +67,4 @@ public void testDefaultConsumesMediaType() { assertArrayEquals(expectedConsumes, resultConsumes); } -} +} \ No newline at end of file