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 <mark@codebrewer.org> * refactor: move test per PR review comment Signed-off-by: Mark Scott <mark@codebrewer.org>
This commit is contained in:
parent
e523bfbfbf
commit
e7e6e46bd5
|
@ -197,7 +197,7 @@ class CloudEventDeserializer extends StdDeserializer<CloudEvent> {
|
|||
if (e.getCause() instanceof IOException) {
|
||||
throw (IOException) e.getCause();
|
||||
}
|
||||
throw MismatchedInputException.wrapWithPath(e, null);
|
||||
throw MismatchedInputException.from(p, CloudEvent.class, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,11 +19,14 @@ package io.cloudevents.jackson;
|
|||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import io.cloudevents.CloudEvent;
|
||||
import io.cloudevents.SpecVersion;
|
||||
import io.cloudevents.core.builder.CloudEventBuilder;
|
||||
import io.cloudevents.core.provider.EventFormatProvider;
|
||||
import io.cloudevents.rw.CloudEventRWException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
@ -38,6 +41,7 @@ import java.util.stream.Stream;
|
|||
|
||||
import static io.cloudevents.core.test.Data.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
|
||||
class JsonFormatTest {
|
||||
|
||||
|
@ -109,6 +113,13 @@ class JsonFormatTest {
|
|||
assertThat(output).isEqualTo(normalizeToJsonValueIfNeeded(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void throwExpectedOnInvalidSpecversion() {
|
||||
assertThatCode(() -> getFormat().deserialize(("{\"specversion\":\"9000.1\"}").getBytes(StandardCharsets.UTF_8)))
|
||||
.hasCauseInstanceOf(MismatchedInputException.class)
|
||||
.hasMessageContaining(CloudEventRWException.newInvalidSpecVersion("9000.1").getMessage());
|
||||
}
|
||||
|
||||
public static Stream<Arguments> serializeTestArgumentsDefault() {
|
||||
return Stream.of(
|
||||
Arguments.of(V03_MIN, "v03/min.json"),
|
||||
|
|
Loading…
Reference in New Issue