Skip to content

Commit

Permalink
[CLIENT] fixed NPE in GenericJsonRecord (apache#10482)
Browse files Browse the repository at this point in the history
Fixes apache#10472

GenericJsonRecord should not error out if the non-existent field is queried.

Added a null check and returning null if the field does not exist.

Extended one of the tests for GenericJsonRecord.

(cherry picked from commit ff076e3)
  • Loading branch information
abhilashmandaliya authored and eolivelli committed May 20, 2021
1 parent bf00805 commit 0f11747
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ default Object getField(Field field) {
* Retrieve the value of the provided <tt>fieldName</tt>.
*
* @param fieldName the field name
* @return the value object
* @return the value object, or null if field doesn't exist
*/
Object getField(String fieldName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public JsonNode getJsonNode() {
@Override
public Object getField(String fieldName) {
JsonNode fn = jn.get(fieldName);
if (fn == null) {
return null;
}
if (fn.isContainerNode()) {
AtomicInteger idx = new AtomicInteger(0);
List<Field> fields = Lists.newArrayList(fn.fieldNames())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@
import org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo;
import org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBar;
import org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBarList;
import org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord;
import org.apache.pulsar.client.impl.schema.generic.GenericSchemaImpl;
import org.apache.pulsar.common.schema.SchemaInfo;
import org.apache.pulsar.common.schema.SchemaType;
import org.assertj.core.api.Assertions;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -451,5 +454,8 @@ public void testJsonGenericRecordBuilder() {
assertEquals("USA", seller3Record.getField("state"));
assertEquals("oakstreet", seller3Record.getField("street"));
assertEquals(9999, seller3Record.getField("zipCode"));

assertTrue(pc3Record instanceof GenericJsonRecord);
Assertions.assertThatCode(() -> pc3Record.getField("I_DO_NOT_EXIST")).doesNotThrowAnyException();
}
}

0 comments on commit 0f11747

Please sign in to comment.