You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2020/12/21 12:33:30 UTC
[pulsar] 02/02: Issue 8882: GenericJsonReader converts the null
value to string "null" (#8883)
This is an automated email from the ASF dual-hosted git repository.
penghui pushed a commit to branch branch-2.7
in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit e9e415fe7cd1dcb0c00ff29eb0de5a9413041d06
Author: Enrico Olivelli <eo...@gmail.com>
AuthorDate: Thu Dec 10 06:28:51 2020 +0100
Issue 8882: GenericJsonReader converts the null value to string "null" (#8883)
Describe the bug
It looks like GenericJsonReader is not handling correctly null values
Expected behavior
The null value is not converted to a string, but it is still a null value
Changes
- Handle correctly null values
- add test case
Additional context
The problem affects Pulsar Functions/Pulsar IO
Fixes #8882
(cherry picked from commit d9f3710006749c73471a80e1150a2d2d61c154f8)
---
.../client/impl/schema/generic/GenericJsonRecord.java | 2 ++
.../client/impl/schema/generic/GenericJsonRecordTest.java | 13 +++++++++++++
2 files changed, 15 insertions(+)
diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/generic/GenericJsonRecord.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/generic/GenericJsonRecord.java
index 708b10e..85d3e8e 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/generic/GenericJsonRecord.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/generic/GenericJsonRecord.java
@@ -90,6 +90,8 @@ public class GenericJsonRecord extends VersionedGenericRecord {
} catch (IOException e) {
return fn.asText();
}
+ } else if (fn.isNull()) {
+ return null;
} else {
return fn.asText();
}
diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/generic/GenericJsonRecordTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/generic/GenericJsonRecordTest.java
index 0de3e3e..6486a24 100644
--- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/generic/GenericJsonRecordTest.java
+++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/generic/GenericJsonRecordTest.java
@@ -25,13 +25,26 @@ import org.testng.annotations.Test;
import java.util.Collections;
import static java.nio.charset.StandardCharsets.UTF_8;
+import org.apache.pulsar.client.api.schema.Field;
import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
public class GenericJsonRecordTest {
@Test
+ public void decodeNullValue() throws Exception{
+ byte[] json = "{\"somefield\":null}".getBytes(UTF_8);
+ GenericJsonRecord record
+ = new GenericJsonReader(Collections.singletonList(new Field("somefield", 0)))
+ .read(json, 0, json.length);
+ assertTrue(record.getJsonNode().get("somefield").isNull());
+ assertNull(record.getField("somefield"));
+ }
+
+
+ @Test
public void decodeLongField() throws Exception{
String jsonStr = "{\"timestamp\":1585204833128, \"count\":2, \"value\": 1.1, \"on\":true}";
byte[] jsonStrBytes = jsonStr.getBytes();