Skip to content

Commit a837a64

Browse files
committed
Fix #39
1 parent 57f811b commit a837a64

File tree

4 files changed

+69
-85
lines changed

4 files changed

+69
-85
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/ScalarReaderWrapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public JsonToken nextToken() throws IOException
4040
// 19-Jan-2017, tatu: May need to be called multiple times, for root-level
4141
// sequences. Because of this need to check for EOF. But only after reading
4242
// one token successfully...
43-
if (_rootReader && _decoder.isEnd()) {
44-
return (_currToken = null);
43+
if (_rootReader) {
44+
JsonToken t = _decoder.isEnd() ? null : _wrappedReader.readValue(_parser, _decoder);
45+
return (_currToken = t);
4546
}
4647
_parser.setAvroContext(getParent());
4748
return (_currToken = _wrappedReader.readValue(_parser, _decoder));

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/UnionReader.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ final class UnionReader extends AvroStructureReader
1616
private final BinaryDecoder _decoder;
1717
private final AvroParserImpl _parser;
1818

19-
private AvroStructureReader _currentReader;
20-
2119
public UnionReader(AvroStructureReader[] memberReaders) {
2220
this(null, memberReaders, null, null);
2321
}
@@ -41,17 +39,15 @@ public UnionReader newReader(AvroReadContext parent,
4139
@Override
4240
public JsonToken nextToken() throws IOException
4341
{
44-
if (_currentReader == null) {
45-
int index = _decoder.readIndex();
46-
if (index < 0 || index >= _memberReaders.length) {
47-
throw new JsonParseException(_parser, String.format
48-
("Invalid index (%s); union only has %d types", index, _memberReaders.length));
49-
}
50-
// important: remember to create new instance
51-
// also: must pass our parent (not this instance)
52-
_currentReader = _memberReaders[index].newReader(_parent, _parser, _decoder);
42+
int index = _decoder.readIndex();
43+
if (index < 0 || index >= _memberReaders.length) {
44+
throw new JsonParseException(_parser, String.format
45+
("Invalid index (%s); union only has %d types", index, _memberReaders.length));
5346
}
54-
return (_currToken = _currentReader.nextToken());
47+
// important: remember to create new instance
48+
// also: must pass our parent (not this instance)
49+
AvroStructureReader reader = _memberReaders[index].newReader(_parent, _parser, _decoder);
50+
return (_currToken = reader.nextToken());
5551
}
5652

5753
@Override

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/Issue39Test.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/MapWithUnionTest.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.dataformat.avro;
22

3+
import java.io.IOException;
34
import java.util.*;
45

56
public class MapWithUnionTest extends AvroTestBase
@@ -14,9 +15,38 @@ public class MapWithUnionTest extends AvroTestBase
1415
+ " ] \n"
1516
+"}\n");
1617

18+
// for [dataformats-binary#39]
19+
final static String MAP_CONTAINER_SCHEMA_JSON = aposToQuotes("{\n"
20+
+" 'namespace': 'com.salesforce.conduit.avro',\n"
21+
+" 'type': 'record',\n"
22+
+" 'name': 'MapContainer',\n"
23+
+" 'fields': [\n"
24+
+" {'name':'props', \n"
25+
+" 'type' : {\n"
26+
+" 'type' : 'map', \n"
27+
+" 'values': ['null','int','long','float','double','string','boolean',{'type':'map','values':['null','int','long','float','double','string','boolean']}]\n"
28+
+" }\n"
29+
+" }\n"
30+
+" ]\n"
31+
+"}");
32+
static class MapContainer {
33+
public Map<String, Object> props;
34+
35+
public MapContainer() {}
36+
public MapContainer(Map<String, Object> p) {
37+
props = p;
38+
}
39+
}
40+
41+
/*
42+
/**********************************************************
43+
/* Test methods
44+
/**********************************************************
45+
*/
46+
1747
private final AvroMapper MAPPER = getMapper();
1848

19-
public void testRecordWithMap() throws Exception
49+
public void testRootMapWithUnion() throws Exception
2050
{
2151
AvroSchema schema = MAPPER.schemaFrom(MAP_WITH_UNION_SCHEMA_JSON);
2252
Map<String,Object> input = new LinkedHashMap<String,Object>();
@@ -33,4 +63,31 @@ public void testRecordWithMap() throws Exception
3363
assertEquals("123", result.get("a"));
3464
assertEquals("foobar", result.get("xy"));
3565
}
66+
67+
public void testMapContainerWithNested() throws IOException
68+
{
69+
Map<String,Object> map = new LinkedHashMap<String,Object>();
70+
map.put("hello", "world");
71+
Map<String,String> otherMap = new LinkedHashMap<String,String>();
72+
otherMap.put("foo", "bar");
73+
otherMap.put("zap", "bing");
74+
map.put("otherMap", otherMap);
75+
map.put("goodbye", "charlie");
76+
MapContainer event = new MapContainer(map);
77+
78+
AvroSchema avroSchema = MAPPER.schemaFrom(MAP_CONTAINER_SCHEMA_JSON);
79+
byte[] serialized = MAPPER.writer(avroSchema).writeValueAsBytes(event);
80+
81+
MapContainer deserialized = MAPPER.readerFor(MapContainer.class)
82+
.with(avroSchema)
83+
.readValue(serialized);
84+
assertEquals(3, deserialized.props.size());
85+
assertEquals("world", deserialized.props.get("hello"));
86+
assertEquals("charlie", deserialized.props.get("goodbye"));
87+
Object ob = deserialized.props.get("otherMap");
88+
assertTrue(ob instanceof Map<?,?>);
89+
Map<?,?> m = (Map<?,?>) ob;
90+
assertEquals("bar", m.get("foo"));
91+
assertEquals("bing", m.get("zap"));
92+
}
3693
}

0 commit comments

Comments
 (0)