Skip to content

Commit 75d743f

Browse files
authored
1 parent 85bc5f3 commit 75d743f

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

apm-agent-core/src/main/java/co/elastic/apm/impl/context/Request.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ public PotentiallyMultiValuedMap getFormUrlEncodedParameters() {
112112
* @param headerValue The value of the header.
113113
* @return {@code this}, for fluent method chaining
114114
*/
115-
public Request addHeader(String headerName, String headerValue) {
116-
headers.add(headerName, headerValue);
115+
public Request addHeader(String headerName, @Nullable String headerValue) {
116+
if (headerValue != null) {
117+
headers.add(headerName, headerValue);
118+
}
117119
return this;
118120
}
119121

apm-agent-core/src/main/java/co/elastic/apm/report/serialize/DslJsonSerializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,11 @@ private void writeField(final String fieldName, final PotentiallyMultiValuedMap
587587
}
588588
}
589589

590-
private void serializePotentiallyMultiValuedEntry(String key, Object value) {
590+
private void serializePotentiallyMultiValuedEntry(String key, @Nullable Object value) {
591591
jw.writeString(key);
592592
jw.writeByte(JsonWriter.SEMI);
593593
if (value instanceof String) {
594-
StringConverter.serializeNullable((String) value, jw);
594+
StringConverter.serialize((String) value, jw);
595595
} else if (value instanceof List) {
596596
jw.writeByte(ARRAY_START);
597597
final List<String> values = (List<String>) value;
@@ -601,6 +601,8 @@ private void serializePotentiallyMultiValuedEntry(String key, Object value) {
601601
jw.writeString(values.get(i));
602602
}
603603
jw.writeByte(ARRAY_END);
604+
} else if (value == null) {
605+
jw.writeNull();
604606
}
605607
}
606608

apm-agent-core/src/test/java/co/elastic/apm/report/serialize/DslJsonSerializerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package co.elastic.apm.report.serialize;
2121

2222
import co.elastic.apm.configuration.CoreConfiguration;
23+
import co.elastic.apm.impl.ElasticApmTracer;
24+
import co.elastic.apm.impl.transaction.Transaction;
2325
import com.dslplatform.json.JsonWriter;
2426
import com.fasterxml.jackson.core.JsonProcessingException;
2527
import com.fasterxml.jackson.databind.JsonNode;
@@ -32,6 +34,7 @@
3234

3335
import static org.assertj.core.api.Assertions.assertThat;
3436
import static org.assertj.core.api.SoftAssertions.assertSoftly;
37+
import static org.mockito.Mockito.mock;
3538

3639

3740
class DslJsonSerializerTest {
@@ -74,6 +77,19 @@ void testLimitStringValueLength() throws IOException {
7477
assertThat(jsonNode.get("lastString").textValue()).hasSize(DslJsonSerializer.MAX_VALUE_LENGTH).endsWith("…");
7578
}
7679

80+
@Test
81+
void testNullHeaders() throws IOException {
82+
Transaction transaction = new Transaction();
83+
transaction.getContext().getRequest().addHeader("foo", null);
84+
transaction.getContext().getRequest().getHeaders().add("bar", null);
85+
JsonNode jsonNode = objectMapper.readTree(serializer.toJsonString(transaction));
86+
System.out.println(jsonNode);
87+
// calling addHeader with a null value ignores the header
88+
assertThat(jsonNode.get("context").get("request").get("headers").get("foo")).isNull();
89+
// should a null value sneak in, it should not break
90+
assertThat(jsonNode.get("context").get("request").get("headers").get("bar").isNull()).isTrue();
91+
}
92+
7793
private String toJson(Map<String, String> map) {
7894
try {
7995
return objectMapper.writeValueAsString(map);

apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/servlet/ServletApiAdvice.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ public static Transaction onEnterServletService(@Advice.Argument(0) ServletReque
7676
}
7777
}
7878
final Enumeration headerNames = request.getHeaderNames();
79-
while (headerNames.hasMoreElements()) {
80-
final String headerName = (String) headerNames.nextElement();
81-
req.addHeader(headerName, request.getHeader(headerName));
79+
if (headerNames != null) {
80+
while (headerNames.hasMoreElements()) {
81+
final String headerName = (String) headerNames.nextElement();
82+
req.addHeader(headerName, request.getHeader(headerName));
83+
}
8284
}
8385

8486
final Principal userPrincipal = request.getUserPrincipal();

0 commit comments

Comments
 (0)