Skip to content

Commit e7e6e46

Browse files
authored
fix: prevent NPE on deserializing JSON containing invalid specversion value (#342)
* fix: prevent NPE on deserializing JSON containing invalid `specversion` value Signed-off-by: Mark Scott <[email protected]> * refactor: move test per PR review comment Signed-off-by: Mark Scott <[email protected]>
1 parent e523bfb commit e7e6e46

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public CloudEvent deserialize(JsonParser p, DeserializationContext ctxt) throws
197197
if (e.getCause() instanceof IOException) {
198198
throw (IOException) e.getCause();
199199
}
200-
throw MismatchedInputException.wrapWithPath(e, null);
200+
throw MismatchedInputException.from(p, CloudEvent.class, e.getMessage());
201201
}
202202
}
203203
}

formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
import com.fasterxml.jackson.databind.JsonNode;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
2223
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
2324
import io.cloudevents.CloudEvent;
2425
import io.cloudevents.SpecVersion;
2526
import io.cloudevents.core.builder.CloudEventBuilder;
2627
import io.cloudevents.core.provider.EventFormatProvider;
28+
import io.cloudevents.rw.CloudEventRWException;
29+
import org.junit.jupiter.api.Test;
2730
import org.junit.jupiter.params.ParameterizedTest;
2831
import org.junit.jupiter.params.provider.Arguments;
2932
import org.junit.jupiter.params.provider.MethodSource;
@@ -38,6 +41,7 @@
3841

3942
import static io.cloudevents.core.test.Data.*;
4043
import static org.assertj.core.api.Assertions.assertThat;
44+
import static org.assertj.core.api.Assertions.assertThatCode;
4145

4246
class JsonFormatTest {
4347

@@ -109,6 +113,13 @@ void eventRoundTrip(CloudEvent input) {
109113
assertThat(output).isEqualTo(normalizeToJsonValueIfNeeded(input));
110114
}
111115

116+
@Test
117+
void throwExpectedOnInvalidSpecversion() {
118+
assertThatCode(() -> getFormat().deserialize(("{\"specversion\":\"9000.1\"}").getBytes(StandardCharsets.UTF_8)))
119+
.hasCauseInstanceOf(MismatchedInputException.class)
120+
.hasMessageContaining(CloudEventRWException.newInvalidSpecVersion("9000.1").getMessage());
121+
}
122+
112123
public static Stream<Arguments> serializeTestArgumentsDefault() {
113124
return Stream.of(
114125
Arguments.of(V03_MIN, "v03/min.json"),

0 commit comments

Comments
 (0)