Skip to content

Commit fb142c8

Browse files
committed
Merge branch '4.3.x'
2 parents 74ffd3c + 72cc88d commit fb142c8

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringMvcContract.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
* @author Sam Kruglov
9494
* @author Tang Xiong
9595
* @author Juhyeong An
96+
* @author Olof Segergren
9697
*/
9798
public class SpringMvcContract extends Contract.BaseContract implements ResourceLoaderAware {
9899

@@ -382,9 +383,11 @@ private boolean queryMapParamPresent(MethodMetadata data) {
382383
}
383384

384385
private void parseProduces(MethodMetadata md, RequestMapping annotation) {
385-
String[] serverProduces = annotation.produces();
386-
String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]);
387-
if (clientAccepts != null) {
386+
String[] clientAccepts = Arrays.stream(annotation.produces())
387+
.map(s -> emptyToNull(s))
388+
.filter(Objects::nonNull)
389+
.toArray(String[]::new);
390+
if (clientAccepts.length > 0) {
388391
md.template().header(ACCEPT, clientAccepts);
389392
}
390393
}

spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/SpringMvcContractTests.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,38 @@ void testAddingTemplatedParameterWithTheSameKey() throws NoSuchMethodException {
780780
assertThat(data.template().headers().get("Accept")).contains("application/json", "{Accept}");
781781
}
782782

783+
@Test
784+
void testMultipleProducesValues() throws NoSuchMethodException {
785+
Method method = TestTemplate_MultipleProduces.class.getDeclaredMethod("multipleProduces");
786+
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);
787+
788+
assertThat(data.template().headers().get("Accept")).containsExactly("application/jose", "application/json");
789+
}
790+
791+
@Test
792+
void testSingleProducesValueUnchanged() throws NoSuchMethodException {
793+
Method method = TestTemplate_MultipleProduces.class.getDeclaredMethod("singleProduces");
794+
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);
795+
796+
assertThat(data.template().headers().get("Accept")).containsExactly(MediaType.APPLICATION_JSON_VALUE);
797+
}
798+
799+
@Test
800+
void testEmptyProducesNoAcceptHeader() throws NoSuchMethodException {
801+
Method method = TestTemplate_MultipleProduces.class.getDeclaredMethod("noProduces");
802+
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);
803+
804+
assertThat(data.template().headers().get("Accept")).isNull();
805+
}
806+
807+
@Test
808+
void testProducesWithBlankEntryIgnored() throws NoSuchMethodException {
809+
Method method = TestTemplate_MultipleProduces.class.getDeclaredMethod("producesWithBlankEntry");
810+
MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method);
811+
812+
assertThat(data.template().headers().get("Accept")).containsExactly("application/jose", "application/json");
813+
}
814+
783815
@Test
784816
void testMultipleRequestPartAnnotations() throws NoSuchMethodException {
785817
Method method = TestTemplate_RequestPart.class.getDeclaredMethod("requestWithMultipleParts",
@@ -1160,4 +1192,20 @@ public String toString() {
11601192

11611193
}
11621194

1195+
public interface TestTemplate_MultipleProduces {
1196+
1197+
@GetMapping(value = "/test", produces = { "application/jose", "application/json" })
1198+
ResponseEntity<TestObject> multipleProduces();
1199+
1200+
@GetMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
1201+
ResponseEntity<TestObject> singleProduces();
1202+
1203+
@GetMapping("/test")
1204+
ResponseEntity<TestObject> noProduces();
1205+
1206+
@GetMapping(value = "/test", produces = { "application/jose", "", "application/json" })
1207+
ResponseEntity<TestObject> producesWithBlankEntry();
1208+
1209+
}
1210+
11631211
}

0 commit comments

Comments
 (0)