Skip to content

Handle NullNode for optional attributes in Jackson CloudEventDeserializer #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
import io.cloudevents.core.format.EventDeserializationException;
import io.cloudevents.core.provider.EventFormatProvider;
import io.cloudevents.rw.CloudEventRWException;
import org.junit.jupiter.api.Assertions;
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;

import java.io.IOException;
import java.math.BigInteger;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -47,7 +45,7 @@

class JsonFormatTest {

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

@ParameterizedTest
@MethodSource("serializeTestArgumentsDefault")
Expand Down Expand Up @@ -184,6 +182,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 @@ -193,6 +192,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
}