From 16ce1bb0e4a998aca0df8f3db44965c2cb0ccb36 Mon Sep 17 00:00:00 2001 From: Pierangelo Di Pilato Date: Tue, 21 Dec 2021 11:22:02 +0100 Subject: [PATCH] Handle NullNode for optional attributes in Jackson CloudEventDeserializer (#432) In `getOptionalStringNode` we should handle `JsonNode`s that are instances of `NullNode`. Signed-off-by: Pierangelo Di Pilato --- .../io/cloudevents/jackson/CloudEventDeserializer.java | 9 +++++---- .../test/java/io/cloudevents/jackson/JsonFormatTest.java | 4 +++- .../src/test/resources/v03/min_subject_null.json | 7 +++++++ .../src/test/resources/v1/min_subject_null.json | 7 +++++++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 formats/json-jackson/src/test/resources/v03/min_subject_null.json create mode 100644 formats/json-jackson/src/test/resources/v1/min_subject_null.json diff --git a/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java b/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java index 87d9a9196..e6346faf7 100644 --- a/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java +++ b/formats/json-jackson/src/main/java/io/cloudevents/jackson/CloudEventDeserializer.java @@ -24,6 +24,7 @@ import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.databind.node.JsonNodeType; +import com.fasterxml.jackson.databind.node.NullNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.cloudevents.CloudEvent; import io.cloudevents.CloudEventData; @@ -175,12 +176,12 @@ private String getStringNode(ObjectNode objNode, JsonParser p, String attributeN } private String getOptionalStringNode(ObjectNode objNode, JsonParser p, String attributeName) throws JsonProcessingException { - JsonNode unparsedSpecVersion = objNode.remove(attributeName); - if (unparsedSpecVersion == null) { + JsonNode unparsedAttribute = objNode.remove(attributeName); + if (unparsedAttribute == null || unparsedAttribute instanceof NullNode) { return null; } - assertNodeType(unparsedSpecVersion, JsonNodeType.STRING, attributeName, null); - return unparsedSpecVersion.asText(); + assertNodeType(unparsedAttribute, JsonNodeType.STRING, attributeName, null); + return unparsedAttribute.asText(); } private void assertNodeType(JsonNode node, JsonNodeType type, String attributeName, String desc) throws JsonProcessingException { diff --git a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java index de99403de..59cc8d91c 100644 --- a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java +++ b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java @@ -47,7 +47,7 @@ class JsonFormatTest { - private ObjectMapper mapper = new ObjectMapper(); + private final ObjectMapper mapper = new ObjectMapper(); @ParameterizedTest @MethodSource("serializeTestArgumentsDefault") @@ -184,6 +184,7 @@ public static Stream serializeTestArgumentsBase64() { public static Stream deserializeTestArguments() { return Stream.of( Arguments.of("v03/min.json", V03_MIN), + Arguments.of("v03/min_subject_null.json", V03_MIN), Arguments.of("v03/json_data.json", normalizeToJsonValueIfNeeded(V03_WITH_JSON_DATA)), Arguments.of("v03/json_data_with_ext.json", normalizeToJsonValueIfNeeded(V03_WITH_JSON_DATA_WITH_EXT)), Arguments.of("v03/base64_json_data.json", V03_WITH_JSON_DATA), @@ -193,6 +194,7 @@ public static Stream deserializeTestArguments() { Arguments.of("v03/text_data.json", V03_WITH_TEXT_DATA), Arguments.of("v03/base64_text_data.json", V03_WITH_TEXT_DATA), Arguments.of("v1/min.json", V1_MIN), + Arguments.of("v1/min_subject_null.json", V1_MIN), Arguments.of("v1/json_data.json", normalizeToJsonValueIfNeeded(V1_WITH_JSON_DATA)), Arguments.of("v1/json_data_with_ext.json", normalizeToJsonValueIfNeeded(V1_WITH_JSON_DATA_WITH_EXT)), Arguments.of("v1/base64_json_data.json", V1_WITH_JSON_DATA), diff --git a/formats/json-jackson/src/test/resources/v03/min_subject_null.json b/formats/json-jackson/src/test/resources/v03/min_subject_null.json new file mode 100644 index 000000000..ff7a923e0 --- /dev/null +++ b/formats/json-jackson/src/test/resources/v03/min_subject_null.json @@ -0,0 +1,7 @@ +{ + "specversion": "0.3", + "id": "1", + "type": "mock.test", + "source": "http://localhost/source", + "subject": null +} diff --git a/formats/json-jackson/src/test/resources/v1/min_subject_null.json b/formats/json-jackson/src/test/resources/v1/min_subject_null.json new file mode 100644 index 000000000..836aac090 --- /dev/null +++ b/formats/json-jackson/src/test/resources/v1/min_subject_null.json @@ -0,0 +1,7 @@ +{ + "specversion": "1.0", + "id": "1", + "type": "mock.test", + "source": "http://localhost/source", + "subject": null +}