Skip to content

Commit

Permalink
Handle NullNode for optional attributes in Jackson CloudEventDeserial…
Browse files Browse the repository at this point in the history
…izer (#432)

In `getOptionalStringNode` we should handle `JsonNode`s that are
instances of `NullNode`.

Signed-off-by: Pierangelo Di Pilato <pdipilat@redhat.com>
  • Loading branch information
pierDipi committed Dec 21, 2021
1 parent ceb0675 commit cc78625
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -203,12 +204,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

class JsonFormatTest {

private ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = new ObjectMapper();

@ParameterizedTest
@MethodSource("serializeTestArgumentsDefault")
Expand Down Expand Up @@ -198,6 +198,7 @@ public static Stream<Arguments> serializeTestArgumentsBase64() {
public static Stream<Arguments> 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),
Expand All @@ -207,6 +208,7 @@ public static Stream<Arguments> 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),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"specversion": "0.3",
"id": "1",
"type": "mock.test",
"source": "http://localhost/source",
"subject": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"specversion": "1.0",
"id": "1",
"type": "mock.test",
"source": "http://localhost/source",
"subject": null
}

0 comments on commit cc78625

Please sign in to comment.