Skip to content

Commit 3cd746d

Browse files
authored
Handle NullNode for optional attributes in Jackson CloudEventDeserializer (#432) (#433)
In `getOptionalStringNode` we should handle `JsonNode`s that are instances of `NullNode`. Signed-off-by: Pierangelo Di Pilato <pdipilat@redhat.com>
1 parent 07c8759 commit 3cd746d

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
2525
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
2626
import com.fasterxml.jackson.databind.node.JsonNodeType;
27+
import com.fasterxml.jackson.databind.node.NullNode;
2728
import com.fasterxml.jackson.databind.node.ObjectNode;
2829
import io.cloudevents.CloudEvent;
2930
import io.cloudevents.CloudEventData;
@@ -175,12 +176,12 @@ private String getStringNode(ObjectNode objNode, JsonParser p, String attributeN
175176
}
176177

177178
private String getOptionalStringNode(ObjectNode objNode, JsonParser p, String attributeName) throws JsonProcessingException {
178-
JsonNode unparsedSpecVersion = objNode.remove(attributeName);
179-
if (unparsedSpecVersion == null) {
179+
JsonNode unparsedAttribute = objNode.remove(attributeName);
180+
if (unparsedAttribute == null || unparsedAttribute instanceof NullNode) {
180181
return null;
181182
}
182-
assertNodeType(unparsedSpecVersion, JsonNodeType.STRING, attributeName, null);
183-
return unparsedSpecVersion.asText();
183+
assertNodeType(unparsedAttribute, JsonNodeType.STRING, attributeName, null);
184+
return unparsedAttribute.asText();
184185
}
185186

186187
private void assertNodeType(JsonNode node, JsonNodeType type, String attributeName, String desc) throws JsonProcessingException {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
class JsonFormatTest {
4949

50-
private ObjectMapper mapper = new ObjectMapper();
50+
private final ObjectMapper mapper = new ObjectMapper();
5151

5252
@ParameterizedTest
5353
@MethodSource("serializeTestArgumentsDefault")
@@ -184,6 +184,7 @@ public static Stream<Arguments> serializeTestArgumentsBase64() {
184184
public static Stream<Arguments> deserializeTestArguments() {
185185
return Stream.of(
186186
Arguments.of("v03/min.json", V03_MIN),
187+
Arguments.of("v03/min_subject_null.json", V03_MIN),
187188
Arguments.of("v03/json_data.json", normalizeToJsonValueIfNeeded(V03_WITH_JSON_DATA)),
188189
Arguments.of("v03/json_data_with_ext.json", normalizeToJsonValueIfNeeded(V03_WITH_JSON_DATA_WITH_EXT)),
189190
Arguments.of("v03/base64_json_data.json", V03_WITH_JSON_DATA),
@@ -193,6 +194,7 @@ public static Stream<Arguments> deserializeTestArguments() {
193194
Arguments.of("v03/text_data.json", V03_WITH_TEXT_DATA),
194195
Arguments.of("v03/base64_text_data.json", V03_WITH_TEXT_DATA),
195196
Arguments.of("v1/min.json", V1_MIN),
197+
Arguments.of("v1/min_subject_null.json", V1_MIN),
196198
Arguments.of("v1/json_data.json", normalizeToJsonValueIfNeeded(V1_WITH_JSON_DATA)),
197199
Arguments.of("v1/json_data_with_ext.json", normalizeToJsonValueIfNeeded(V1_WITH_JSON_DATA_WITH_EXT)),
198200
Arguments.of("v1/base64_json_data.json", V1_WITH_JSON_DATA),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"specversion": "0.3",
3+
"id": "1",
4+
"type": "mock.test",
5+
"source": "http://localhost/source",
6+
"subject": null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"specversion": "1.0",
3+
"id": "1",
4+
"type": "mock.test",
5+
"source": "http://localhost/source",
6+
"subject": null
7+
}

0 commit comments

Comments
 (0)