-
-
Notifications
You must be signed in to change notification settings - Fork 532
Fix unexpected merging of media types #3026
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
Open
Mattias-Sehlstedt
wants to merge
1
commit into
springdoc:main
Choose a base branch
from
Mattias-Sehlstedt:method-media-type-override-class-media-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
266 changes: 212 additions & 54 deletions
266
...c-openapi-starter-common/src/test/java/org/springdoc/core/model/MethodAttributesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,228 @@ | ||
package org.springdoc.core.model; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Locale; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import org.springdoc.core.models.MethodAttributes; | ||
import org.springframework.core.annotation.AnnotatedElementUtils; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Locale; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
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); | ||
private static final String APPLICATION_JSON = "application/json"; | ||
private static final String APPLICATION_XML = "application/xml"; | ||
private static final String APPLICATION_YAML = "application/yaml"; | ||
|
||
String[] expectedProduces = { "application/xml" }; | ||
String[] resultProduces = methodAttributes.getMethodProduces(); | ||
@Test | ||
void testMergeArrays() throws Exception { | ||
MethodAttributes methodAttributes = new MethodAttributes(APPLICATION_JSON, APPLICATION_XML, Locale.ENGLISH); | ||
|
||
assertArrayEquals(expectedProduces, resultProduces); | ||
} | ||
String[] array1 = {APPLICATION_JSON, APPLICATION_XML}; | ||
String[] array2 = {APPLICATION_XML, APPLICATION_YAML}; | ||
|
||
@Test | ||
public void testDefaultConsumesMediaType() { | ||
MethodAttributes methodAttributes = new MethodAttributes("application/json", "application/xml", Locale.ENGLISH); | ||
String[] expected = {APPLICATION_JSON, APPLICATION_XML, APPLICATION_YAML}; | ||
|
||
Method method = this.getClass().getDeclaredMethods()[0]; | ||
methodAttributes.calculateConsumesProduces(method); | ||
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 | ||
void testMergeArraysWithNullArray1() throws Exception { | ||
MethodAttributes methodAttributes = new MethodAttributes(APPLICATION_JSON, APPLICATION_XML, Locale.ENGLISH); | ||
|
||
String[] expectedConsumes = { "application/json" }; | ||
String[] resultConsumes = methodAttributes.getMethodConsumes(); | ||
String[] array1 = null; | ||
String[] array2 = {APPLICATION_XML, APPLICATION_YAML}; | ||
|
||
String[] expected = {APPLICATION_XML, APPLICATION_YAML}; | ||
|
||
assertArrayEquals(expectedConsumes, resultConsumes); | ||
} | ||
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 | ||
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 | ||
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); | ||
} | ||
|
||
@Test | ||
void methodConsumesOverridesClassConsumes() { | ||
MethodAttributes methodAttributes = new MethodAttributes(APPLICATION_JSON, APPLICATION_XML, Locale.ENGLISH); | ||
RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations( | ||
new String[]{APPLICATION_JSON, APPLICATION_XML}, | ||
new String[]{APPLICATION_JSON, APPLICATION_XML} | ||
); | ||
Method method = this.getClass().getDeclaredMethods()[0]; | ||
try (MockedStatic<AnnotatedElementUtils> annotatedElementUtils = Mockito.mockStatic(AnnotatedElementUtils.class)) { | ||
annotatedElementUtils.when(() -> AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class)) | ||
.thenReturn(requestMapping); | ||
|
||
methodAttributes.setClassConsumes(new String[]{APPLICATION_YAML}); | ||
methodAttributes.calculateConsumesProduces(method); | ||
|
||
String[] expectedConsumes = {APPLICATION_JSON, APPLICATION_XML}; | ||
String[] resultConsumes = methodAttributes.getMethodConsumes(); | ||
|
||
assertArrayEquals(expectedConsumes, resultConsumes); | ||
} | ||
} | ||
|
||
@Test | ||
void methodProducesOverridesClassProduces() { | ||
MethodAttributes methodAttributes = new MethodAttributes(APPLICATION_JSON, APPLICATION_XML, Locale.ENGLISH); | ||
RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations( | ||
new String[]{APPLICATION_JSON, APPLICATION_XML}, | ||
new String[]{APPLICATION_JSON, APPLICATION_XML} | ||
); | ||
Method method = this.getClass().getDeclaredMethods()[0]; | ||
try (MockedStatic<AnnotatedElementUtils> annotatedElementUtils = Mockito.mockStatic(AnnotatedElementUtils.class)) { | ||
annotatedElementUtils.when(() -> AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class)) | ||
.thenReturn(requestMapping); | ||
|
||
methodAttributes.setClassProduces(new String[]{APPLICATION_YAML}); | ||
methodAttributes.calculateConsumesProduces(method); | ||
|
||
String[] expectedProduces = {APPLICATION_JSON, APPLICATION_XML}; | ||
String[] resultProduces = methodAttributes.getMethodProduces(); | ||
|
||
assertArrayEquals(expectedProduces, resultProduces); | ||
} | ||
} | ||
|
||
@Test | ||
void methodConsumesIsSetToClassConsumesIfNoMethodConsumesIsDefined() { | ||
MethodAttributes methodAttributes = new MethodAttributes(APPLICATION_JSON, APPLICATION_XML, Locale.ENGLISH); | ||
RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations( | ||
new String[]{APPLICATION_JSON, APPLICATION_XML}, | ||
new String[]{} | ||
); | ||
Method method = this.getClass().getDeclaredMethods()[0]; | ||
try (MockedStatic<AnnotatedElementUtils> annotatedElementUtils = Mockito.mockStatic(AnnotatedElementUtils.class)) { | ||
annotatedElementUtils.when(() -> AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class)) | ||
.thenReturn(requestMapping); | ||
|
||
String[] classConsumes = new String[]{APPLICATION_YAML}; | ||
methodAttributes.setClassConsumes(classConsumes); | ||
methodAttributes.calculateConsumesProduces(method); | ||
|
||
String[] resultConsumes = methodAttributes.getMethodConsumes(); | ||
|
||
assertArrayEquals(classConsumes, resultConsumes); | ||
} | ||
} | ||
|
||
@Test | ||
void methodProducesIsSetToClassProducesIfNoMethodProducesIsDefined() { | ||
MethodAttributes methodAttributes = new MethodAttributes(APPLICATION_JSON, APPLICATION_XML, Locale.ENGLISH); | ||
RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations( | ||
new String[]{}, | ||
new String[]{APPLICATION_JSON, APPLICATION_XML} | ||
); | ||
Method method = this.getClass().getDeclaredMethods()[0]; | ||
try (MockedStatic<AnnotatedElementUtils> annotatedElementUtils = Mockito.mockStatic(AnnotatedElementUtils.class)) { | ||
annotatedElementUtils.when(() -> AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class)) | ||
.thenReturn(requestMapping); | ||
|
||
String[] classProduces = new String[]{APPLICATION_YAML}; | ||
methodAttributes.setClassProduces(classProduces); | ||
methodAttributes.calculateConsumesProduces(method); | ||
|
||
String[] resultProduces = methodAttributes.getMethodProduces(); | ||
|
||
assertArrayEquals(classProduces, resultProduces); | ||
} | ||
} | ||
|
||
@Test | ||
void methodConsumesIsSetToClassConsumesIfNoMethodConsumesIsDefinedAndClassConsumesNotSet() { | ||
MethodAttributes methodAttributes = new MethodAttributes(APPLICATION_JSON, APPLICATION_XML, Locale.ENGLISH); | ||
String[] classConsumes = new String[]{APPLICATION_YAML}; | ||
RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations( | ||
new String[]{APPLICATION_JSON, APPLICATION_XML}, | ||
new String[]{} | ||
); | ||
RequestMapping classMapping = givenAnnotationHasMediaTypeAnnotations( | ||
new String[]{}, | ||
classConsumes | ||
); | ||
Method method = this.getClass().getDeclaredMethods()[0]; | ||
try (MockedStatic<AnnotatedElementUtils> annotatedElementUtils = Mockito.mockStatic(AnnotatedElementUtils.class)) { | ||
annotatedElementUtils.when(() -> AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class)) | ||
.thenReturn(requestMapping); | ||
annotatedElementUtils.when(() -> AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), RequestMapping.class)) | ||
.thenReturn(classMapping); | ||
|
||
methodAttributes.calculateConsumesProduces(method); | ||
|
||
String[] resultConsumes = methodAttributes.getMethodConsumes(); | ||
|
||
assertArrayEquals(classConsumes, resultConsumes); | ||
} | ||
} | ||
|
||
@Test | ||
void methodProducesIsSetToClassProducesIfNoMethodProducesIsDefinedAndClassProducesNotSet() { | ||
MethodAttributes methodAttributes = new MethodAttributes(APPLICATION_JSON, APPLICATION_XML, Locale.ENGLISH); | ||
String[] classProduces = new String[]{APPLICATION_YAML}; | ||
RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations( | ||
new String[]{}, | ||
new String[]{APPLICATION_JSON, APPLICATION_XML} | ||
); | ||
RequestMapping classMapping = givenAnnotationHasMediaTypeAnnotations( | ||
classProduces, | ||
new String[]{} | ||
); | ||
Method method = this.getClass().getDeclaredMethods()[0]; | ||
try (MockedStatic<AnnotatedElementUtils> annotatedElementUtils = Mockito.mockStatic(AnnotatedElementUtils.class)) { | ||
annotatedElementUtils.when(() -> AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class)) | ||
.thenReturn(requestMapping); | ||
annotatedElementUtils.when(() -> AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), RequestMapping.class)) | ||
.thenReturn(classMapping); | ||
|
||
methodAttributes.calculateConsumesProduces(method); | ||
|
||
String[] resultProduces = methodAttributes.getMethodProduces(); | ||
|
||
assertArrayEquals(classProduces, resultProduces); | ||
} | ||
} | ||
|
||
private RequestMapping givenAnnotationHasMediaTypeAnnotations(String[] produces, String[] consumes) { | ||
RequestMapping requestMapping = Mockito.mock(RequestMapping.class); | ||
given(requestMapping.produces()).willReturn(produces); | ||
given(requestMapping.consumes()).willReturn(consumes); | ||
return requestMapping; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
fillMethods
triggered a media type recalculation that made the types become incorrectly assigned as the class media types. Given the name of the method, I removed the call tofillMethods
and instead simply invokedsetHeaders
which is invoked lastly in thefillMethods
method.