Skip to content

Commit 547e386

Browse files
committed
Fix issue when multiple records were omitted
1 parent 0ea0a02 commit 547e386

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>org.mihkel</groupId>
66
<artifactId>avro-json-decoder</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.0</version>
8+
<version>1.1</version>
99
<name>avro-json-decoder</name>
1010
<url>http://maven.apache.org</url>
1111

src/main/java/org/mihkel/avro/io/ExtendedJsonDecoder.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,18 +745,24 @@ private static Field findField(Schema schema, String name) {
745745
return schema.getField(name);
746746
}
747747

748+
Field foundField = null;
749+
748750
for (Field field : schema.getFields()) {
749751
Schema fieldSchema = field.schema();
750752
if (Type.RECORD.equals(fieldSchema.getType())) {
751-
return findField(fieldSchema, name);
753+
foundField = findField(fieldSchema, name);
752754
} else if (Type.ARRAY.equals(fieldSchema.getType())) {
753-
return findField(fieldSchema.getElementType(), name);
755+
foundField = findField(fieldSchema.getElementType(), name);
754756
} else if (Type.MAP.equals(fieldSchema.getType())) {
755-
return findField(fieldSchema.getValueType(), name);
757+
foundField = findField(fieldSchema.getValueType(), name);
758+
}
759+
760+
if (foundField != null) {
761+
return foundField;
756762
}
757763
}
758764

759-
return null;
765+
return foundField;
760766
}
761767
}
762768

src/test/java/org/mihkel/avro/io/ExtendedJsonDecoderTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.mihkel.avro.io;
22

3+
import java.io.IOException;
4+
35
import org.apache.avro.Schema;
46
import org.apache.avro.generic.GenericDatumReader;
57
import org.apache.avro.generic.GenericRecord;
@@ -60,4 +62,67 @@ private void checkNumeric(String type, Object value) throws Exception {
6062
Assert.assertEquals(200, in.readLong());
6163
in.skipArray();
6264
}
65+
66+
@Test
67+
public void testNullsAreInferred() throws IOException {
68+
String w = "{\"type\":\"record\",\"name\":\"R\",\"fields\":[{\"type\":[\"null\",\"long\"],\"name\":\"a\",\"default\":null}]}";
69+
GenericRecord record = readRecord(w, "{}");
70+
71+
Assert.assertNull(record.get("a"));
72+
}
73+
74+
@Test
75+
public void testDefaultValuesAreInferred() throws IOException {
76+
String w = "{\"type\":\"record\",\"name\":\"R\",\"fields\":[{\"type\":\"long\",\"name\":\"a\",\"default\":7}]}";
77+
GenericRecord record = readRecord(w, "{}");
78+
79+
Assert.assertEquals(7L, record.get("a"));
80+
}
81+
82+
@Test
83+
public void testNestedNullsAreInferred() throws IOException {
84+
String w = "{\"type\":\"record\",\"name\":\"R\",\"fields\":[{\"name\":\"S\",\"type\":" +
85+
"{\"type\":\"record\",\"name\":\"S\",\"fields\":[{\"type\":[\"null\",\"long\"],\"name\":\"a\",\"default\":null},{\"type\":\"long\",\"name\":\"b\"}]}}]}";
86+
String data = "{\"S\": {\"b\":1}}";
87+
GenericRecord record = ((GenericRecord)readRecord(w, data).get("S"));
88+
Assert.assertNull(record.get("a"));
89+
}
90+
91+
@Test
92+
public void testArraysCanBeNull() throws IOException {
93+
String w = "{\"type\":\"record\",\"name\":\"R\",\"fields\":[{\"type\":[\"null\",{\"type\":\"array\",\"items\":\"long\"}],\"name\":\"A\",\"default\":null}]}";
94+
String data = "{}";
95+
GenericRecord record = readRecord(w, data);
96+
Assert.assertNull(record.get("A"));
97+
}
98+
99+
@Test
100+
public void testRecordCanBeNull() throws IOException {
101+
String w = "{\"type\":\"record\",\"name\":\"R\",\"namespace\":\"com.playtech.bex.massupdate.api\",\"fields\":" +
102+
"[{\"name\":\"S\",\"type\":[\"null\",{\"type\":\"record\",\"name\":\"S\",\"fields\":[{\"name\":\"A\",\"type\":\"long\"}]}],\"default\":null}]}";
103+
String data = "{}";
104+
GenericRecord record = readRecord(w, data);
105+
Assert.assertNull(record.get("S"));
106+
}
107+
108+
@Test
109+
public void testWtf() throws IOException {
110+
String w = "{\"type\":\"record\",\"name\":\"wrapper\",\"fields\":[{\"name\":\"data\",\"type\":" +
111+
"{\"type\":\"array\",\"items\":{\"type\":\"record\",\"name\":\"r1\",\"fields\":" +
112+
"[{\"name\":\"r1\",\"type\":{\"type\":\"array\",\"items\":{\"type\":\"record\",\"name\":\"sr2\",\"fields\":" +
113+
"[{\"name\":\"sr2\",\"type\":\"string\"}]}}},{\"name\":\"r2\",\"type\":{\"type\":\"array\",\"items\":" +
114+
"{\"type\":\"record\",\"name\":\"r2\",\"fields\":[{\"name\":\"notfound1\",\"type\":[\"null\"," +
115+
"{\"type\":\"array\",\"items\":\"string\"}],\"default\":null},{\"name\":\"notfound2\",\"type\":" +
116+
"[\"null\",{\"type\":\"array\",\"items\":\"string\"}],\"default\":null}]}}}]}}}]}";
117+
String data = "{\"data\":[{\"r1\":[],\"r2\":[{\"notfound1\":{\"array\":[\"val1\",\"val2\"]}}]}]}";
118+
GenericRecord record = readRecord(w, data);
119+
Assert.assertNull(record.get("S"));
120+
}
121+
122+
public GenericRecord readRecord(String schemaString, String jsonData) throws IOException {
123+
Schema schema = Schema.parse(schemaString);
124+
Decoder decoder = new ExtendedJsonDecoder(schema, jsonData);
125+
DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema);
126+
return datumReader.read(null, decoder);
127+
}
63128
}

0 commit comments

Comments
 (0)