Skip to content

Ensure default media type order is preserved using LinkedHashSet in mergeArrays. Fixes #2671 #2672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}