Skip to content

Commit cc78625

Browse files
authored
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 <[email protected]>
1 parent ceb0675 commit cc78625

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;
@@ -203,12 +204,12 @@ private String getStringNode(ObjectNode objNode, JsonParser p, String attributeN
203204
}
204205

205206
private String getOptionalStringNode(ObjectNode objNode, JsonParser p, String attributeName) throws JsonProcessingException {
206-
JsonNode unparsedSpecVersion = objNode.remove(attributeName);
207-
if (unparsedSpecVersion == null) {
207+
JsonNode unparsedAttribute = objNode.remove(attributeName);
208+
if (unparsedAttribute == null || unparsedAttribute instanceof NullNode) {
208209
return null;
209210
}
210-
assertNodeType(unparsedSpecVersion, JsonNodeType.STRING, attributeName, null);
211-
return unparsedSpecVersion.asText();
211+
assertNodeType(unparsedAttribute, JsonNodeType.STRING, attributeName, null);
212+
return unparsedAttribute.asText();
212213
}
213214

214215
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
@@ -45,7 +45,7 @@
4545

4646
class JsonFormatTest {
4747

48-
private ObjectMapper mapper = new ObjectMapper();
48+
private final ObjectMapper mapper = new ObjectMapper();
4949

5050
@ParameterizedTest
5151
@MethodSource("serializeTestArgumentsDefault")
@@ -198,6 +198,7 @@ public static Stream<Arguments> serializeTestArgumentsBase64() {
198198
public static Stream<Arguments> deserializeTestArguments() {
199199
return Stream.of(
200200
Arguments.of("v03/min.json", V03_MIN),
201+
Arguments.of("v03/min_subject_null.json", V03_MIN),
201202
Arguments.of("v03/json_data.json", normalizeToJsonValueIfNeeded(V03_WITH_JSON_DATA)),
202203
Arguments.of("v03/json_data_with_ext.json", normalizeToJsonValueIfNeeded(V03_WITH_JSON_DATA_WITH_EXT)),
203204
Arguments.of("v03/base64_json_data.json", V03_WITH_JSON_DATA),
@@ -207,6 +208,7 @@ public static Stream<Arguments> deserializeTestArguments() {
207208
Arguments.of("v03/text_data.json", V03_WITH_TEXT_DATA),
208209
Arguments.of("v03/base64_text_data.json", V03_WITH_TEXT_DATA),
209210
Arguments.of("v1/min.json", V1_MIN),
211+
Arguments.of("v1/min_subject_null.json", V1_MIN),
210212
Arguments.of("v1/json_data.json", normalizeToJsonValueIfNeeded(V1_WITH_JSON_DATA)),
211213
Arguments.of("v1/json_data_with_ext.json", normalizeToJsonValueIfNeeded(V1_WITH_JSON_DATA_WITH_EXT)),
212214
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)