Skip to content

Commit 2524cdf

Browse files
Fix NPE on null body with structured message (#306)
* Fix NPE on null body with structured message Signed-off-by: Francesco Guardiani <[email protected]> * Improved test robustness Signed-off-by: Francesco Guardiani <[email protected]>
1 parent eeb83c3 commit 2524cdf

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

core/src/main/java/io/cloudevents/core/message/impl/GenericStructuredMessageReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
public class GenericStructuredMessageReader extends BaseStructuredMessageReader {
2828

29-
private EventFormat format;
30-
private byte[] payload;
29+
private final EventFormat format;
30+
private final byte[] payload;
3131

3232
public GenericStructuredMessageReader(EventFormat format, byte[] payload) {
3333
this.format = format;

http/vertx/src/main/java/io/cloudevents/http/vertx/VertxMessageFactory.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import io.cloudevents.core.message.impl.MessageUtils;
77
import io.cloudevents.http.vertx.impl.BinaryVertxMessageReaderImpl;
88
import io.cloudevents.http.vertx.impl.CloudEventsHeaders;
9-
import io.cloudevents.http.vertx.impl.VertxWebClientRequestMessageWriterImpl;
109
import io.cloudevents.http.vertx.impl.VertxHttpServerResponseMessageWriterImpl;
10+
import io.cloudevents.http.vertx.impl.VertxWebClientRequestMessageWriterImpl;
11+
import io.cloudevents.lang.Nullable;
12+
import io.cloudevents.rw.CloudEventRWException;
1113
import io.cloudevents.rw.CloudEventWriter;
1214
import io.vertx.core.*;
1315
import io.vertx.core.buffer.Buffer;
@@ -35,12 +37,18 @@ private VertxMessageFactory() {
3537
* @param headers Http headers
3638
* @param body nullable buffer of the body
3739
* @return a Message implementation with potentially an unknown encoding
38-
* @throws IllegalArgumentException If, in case of binary mode, the spec version is invalid
3940
*/
40-
public static MessageReader createReader(MultiMap headers, Buffer body) throws IllegalArgumentException {
41+
public static MessageReader createReader(MultiMap headers, @Nullable Buffer body) throws CloudEventRWException {
4142
return MessageUtils.parseStructuredOrBinaryMessage(
4243
() -> headers.get(HttpHeaders.CONTENT_TYPE),
43-
format -> new GenericStructuredMessageReader(format, body.getBytes()),
44+
format -> {
45+
if (body != null) {
46+
return new GenericStructuredMessageReader(format, body.getBytes());
47+
}
48+
throw CloudEventRWException.newOther(new IllegalStateException(
49+
"Found a structured message using format " + format.serializedContentType() + " with null body"
50+
));
51+
},
4452
() -> headers.get(CloudEventsHeaders.SPEC_VERSION),
4553
sv -> new BinaryVertxMessageReaderImpl(sv, headers, body)
4654
);

http/vertx/src/test/java/io/cloudevents/http/vertx/VertxMessageFactoryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import io.cloudevents.core.message.Encoding;
2323
import io.cloudevents.core.message.MessageReader;
2424
import io.cloudevents.core.mock.CSVFormat;
25+
import io.cloudevents.rw.CloudEventRWException;
2526
import io.cloudevents.types.Time;
2627
import io.vertx.core.MultiMap;
2728
import io.vertx.core.buffer.Buffer;
29+
import org.junit.jupiter.api.Test;
2830
import org.junit.jupiter.params.ParameterizedTest;
2931
import org.junit.jupiter.params.provider.Arguments;
3032
import org.junit.jupiter.params.provider.MethodSource;
@@ -33,9 +35,22 @@
3335

3436
import static io.cloudevents.core.test.Data.*;
3537
import static org.assertj.core.api.Assertions.assertThat;
38+
import static org.assertj.core.api.Assertions.assertThatCode;
3639

3740
public class VertxMessageFactoryTest {
3841

42+
@Test
43+
public void structuredMessageWithoutBody() {
44+
MultiMap headers = MultiMap
45+
.caseInsensitiveMultiMap()
46+
.add("content-type", CSVFormat.INSTANCE.serializedContentType() + "; charset=utf8");
47+
48+
assertThatCode(() -> VertxMessageFactory.createReader(headers, null))
49+
.isInstanceOf(CloudEventRWException.class)
50+
.hasCauseInstanceOf(IllegalStateException.class)
51+
.hasMessageContaining("null body");
52+
}
53+
3954
@ParameterizedTest
4055
@MethodSource("binaryTestArguments")
4156
public void readBinary(MultiMap headers, Buffer body, CloudEvent event) {

0 commit comments

Comments
 (0)