Skip to content
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 @@ -93,6 +93,7 @@
* @author Sam Kruglov
* @author Tang Xiong
* @author Juhyeong An
* @author Olof Segergren
*/
public class SpringMvcContract extends Contract.BaseContract implements ResourceLoaderAware {

Expand Down Expand Up @@ -417,9 +418,11 @@ private boolean queryMapParamPresent(MethodMetadata data) {
}

private void parseProduces(MethodMetadata md, RequestMapping annotation) {
String[] serverProduces = annotation.produces();
String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]);
if (clientAccepts != null) {
String[] clientAccepts = Arrays.stream(annotation.produces())
.map(s -> emptyToNull(s))
.filter(Objects::nonNull)
.toArray(String[]::new);
if (clientAccepts.length > 0) {
md.template().header(ACCEPT, clientAccepts);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,38 @@ void testAddingTemplatedParameterWithTheSameKey() throws NoSuchMethodException {
assertThat(data.template().headers().get("Accept")).contains("application/json", "{Accept}");
}

@Test
void testMultipleProducesValues() throws NoSuchMethodException {
Method method = TestTemplate_MultipleProduces.class.getDeclaredMethod("multipleProduces");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().headers().get("Accept")).containsExactly("application/jose", "application/json");
}

@Test
void testSingleProducesValueUnchanged() throws NoSuchMethodException {
Method method = TestTemplate_MultipleProduces.class.getDeclaredMethod("singleProduces");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().headers().get("Accept")).containsExactly(MediaType.APPLICATION_JSON_VALUE);
}

@Test
void testEmptyProducesNoAcceptHeader() throws NoSuchMethodException {
Method method = TestTemplate_MultipleProduces.class.getDeclaredMethod("noProduces");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().headers().get("Accept")).isNull();
}

@Test
void testProducesWithBlankEntryIgnored() throws NoSuchMethodException {
Method method = TestTemplate_MultipleProduces.class.getDeclaredMethod("producesWithBlankEntry");
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

assertThat(data.template().headers().get("Accept")).containsExactly("application/jose", "application/json");
}

@Test
void testMultipleRequestPartAnnotations() throws NoSuchMethodException {
Method method = TestTemplate_RequestPart.class.getDeclaredMethod("requestWithMultipleParts",
Expand Down Expand Up @@ -1141,4 +1173,20 @@ public String toString() {

}

public interface TestTemplate_MultipleProduces {

@GetMapping(value = "/test", produces = { "application/jose", "application/json" })
ResponseEntity<TestObject> multipleProduces();

@GetMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<TestObject> singleProduces();

@GetMapping("/test")
ResponseEntity<TestObject> noProduces();

@GetMapping(value = "/test", produces = { "application/jose", "", "application/json" })
ResponseEntity<TestObject> producesWithBlankEntry();

}

}