Skip to content

Commit 56b4afb

Browse files
committed
Fix [Bug] API GW V2 Payload setting same header on Single and MultiValue micronaut-projects#1858
1 parent 32e4747 commit 56b4afb

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

function-aws-api-proxy-test/src/main/java/io/micronaut/function/aws/proxy/test/DefaultServletToAwsProxyResponseAdapter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ private void populateHeaders(@NonNull ConversionService conversionService,
7373
MutableMapListOfStringAndMapStringConvertibleMultiValue entries = new MutableMapListOfStringAndMapStringConvertibleMultiValue(conversionService, multiValueHeaders, singleHeaders);
7474

7575
for (String name: entries.names()) {
76-
response.addHeader(name, String.join(",", entries.getAll(name)));
76+
entries.getAll(name)
77+
.forEach(headerValue -> response.addHeader(name, headerValue));
7778
}
7879
}
7980

function-aws-api-proxy-test/src/test/groovy/io/micronaut/function/aws/proxy/test/AwsApiProxyTestServerSpec.groovy

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.micronaut.function.aws.proxy.test
22

3+
import io.micronaut.http.HttpHeaders
34
import io.micronaut.http.HttpRequest
45
import io.micronaut.http.HttpResponse
56
import io.micronaut.http.HttpStatus
@@ -79,6 +80,19 @@ class AwsApiProxyTestServerSpec extends Specification {
7980
response.body.get() == (1..256).collect { it as byte } as byte[]
8081
}
8182

83+
@Issue('https://github.com/micronaut-projects/micronaut-aws/issues/1858')
84+
void 'can return a ByteArray'() {
85+
when:
86+
HttpResponse<?> response = client.toBlocking()
87+
.exchange(HttpRequest.GET('/multiple-headers'))
88+
89+
then:
90+
response.status == HttpStatus.OK
91+
response.headers.getAll(HttpHeaders.LINK) ==
92+
['<https://example1.com>; rel="next"', '<https://example2.com>; rel="prev"']
93+
response.headers.get(HttpHeaders.CACHE_CONTROL) == "no-cache"
94+
}
95+
8296
@Controller
8397
static class TestController {
8498
@Get(value = '/test', produces = MediaType.TEXT_PLAIN)
@@ -105,5 +119,13 @@ class AwsApiProxyTestServerSpec extends Specification {
105119
HttpResponse<byte[]> byteArray() {
106120
return HttpResponse.ok((1..256).collect { it as byte } as byte[])
107121
}
122+
123+
@Post(value = '/multiple-headers', processes = MediaType.TEXT_PLAIN)
124+
HttpResponse emptyBody() {
125+
return HttpResponse.ok()
126+
.header(HttpHeaders.LINK, '<https://example1.com>; rel="next"')
127+
.header(HttpHeaders.LINK, '<https://example2.com>; rel="prev"')
128+
.header(HttpHeaders.CACHE_CONTROL, "no-cache")
129+
}
108130
}
109131
}

function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/MapCollapseUtils.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ private MapCollapseUtils() {
4343
public static Map<String, String> getSingleValueHeaders(MutableHttpHeaders headers) {
4444
Map<String, String> result = new HashMap<>();
4545
for (String paramName : headers.names()) {
46-
result.put(paramName, headers.get(paramName));
46+
List<String> values = headers.getAll(paramName);
47+
if (values.size() == 1) {
48+
result.put(paramName, values.get(0));
49+
}
4750
}
4851
return result;
4952
}
@@ -57,7 +60,10 @@ public static Map<String, String> getSingleValueHeaders(MutableHttpHeaders heade
5760
public static Map<String, List<String>> getMultiHeaders(MutableHttpHeaders headers) {
5861
Map<String, List<String>> result = new HashMap<>();
5962
for (String paramName : headers.names()) {
60-
result.put(paramName, headers.getAll(paramName));
63+
List<String> values = headers.getAll(paramName);
64+
if (values.size() > 1) {
65+
result.put(paramName, values);
66+
}
6167
}
6268
return result;
6369
}

0 commit comments

Comments
 (0)