Skip to content

Commit 08468c2

Browse files
committed
Add a failing test for #39 (not skipped by maven for some reason, need to troubleshoot, but not now)
1 parent dda7a6d commit 08468c2

File tree

3 files changed

+107
-33
lines changed

3 files changed

+107
-33
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.fasterxml.jackson.dataformat.avro;
2+
3+
import java.io.IOException;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
7+
import com.fasterxml.jackson.core.JsonParser;
8+
import com.fasterxml.jackson.core.JsonToken;
9+
10+
public class Issue39Test extends AvroTestBase
11+
{
12+
final static String SCHEMA_MAP_OF_MAPS_JSON = aposToQuotes("{\n"
13+
+" 'namespace': 'com.salesforce.conduit.avro',\n"
14+
+" 'type': 'record',\n"
15+
+" 'name': 'MapContainer',\n"
16+
+" 'fields': [\n"
17+
+" {'name':'props', \n"
18+
+" 'type' : {\n"
19+
+" 'type' : 'map', \n"
20+
+" 'values': ['null','int','long','float','double','string','boolean',{'type':'map','values':['null','int','long','float','double','string','boolean']}]\n"
21+
+" }\n"
22+
+" }\n"
23+
+" ]\n"
24+
+"}");
25+
static class MapContainer {
26+
public Map<String, Object> props;
27+
28+
public MapContainer() {}
29+
public MapContainer(Map<String, Object> p) {
30+
props = p;
31+
}
32+
}
33+
34+
/*
35+
/**********************************************************
36+
/* Test methods
37+
/**********************************************************
38+
*/
39+
40+
private final AvroMapper MAPPER = getMapper();
41+
42+
public void testMapOfMaps() throws IOException
43+
{
44+
Map<String,Object> map = new LinkedHashMap<String,Object>();
45+
map.put("hello", "world");
46+
map.put("goodbye", "charlie");
47+
Map<String,String> otherMap = new LinkedHashMap<String,String>();
48+
otherMap.put("foo", "bar");
49+
map.put("otherMap", otherMap);
50+
MapContainer event = new MapContainer(map);
51+
52+
AvroSchema avroSchema = MAPPER.schemaFrom(SCHEMA_MAP_OF_MAPS_JSON);
53+
byte[] serialized = MAPPER.writer(avroSchema).writeValueAsBytes(event);
54+
55+
/*
56+
MapContainer deserialized = MAPPER.readerFor(MapContainer.class)
57+
.with(avroSchema)
58+
.readValue(serialized);
59+
assertEquals(3, deserialized.props.size());
60+
*/
61+
62+
JsonParser p = MAPPER.getFactory().createParser(serialized);
63+
p.setSchema(avroSchema);
64+
JsonToken t;
65+
while ((t = p.nextToken()) != null) {
66+
System.err.println("GOT: "+t);
67+
}
68+
System.err.println("DONE!");
69+
}
70+
}
Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.fasterxml.jackson.dataformat.avro;
22

33
import java.io.IOException;
4-
import java.util.Map;
5-
6-
import org.apache.avro.Schema;
4+
import java.util.*;
75

86
import com.fasterxml.jackson.annotation.JsonProperty;
97

@@ -15,33 +13,39 @@
1513

1614
public class NestedMapTest extends AvroTestBase
1715
{
18-
public static class Nester {
19-
@JsonProperty
20-
public Map<String,Map<String,Integer>> nested;
21-
}
22-
23-
public void testSerialization() throws IOException
24-
{
25-
Nester fromJson = new ObjectMapper().readValue(
26-
"{\"nested\": {\"map\":{\"value\":1}}}"
27-
, Nester.class);
28-
29-
AvroMapper mapper = new AvroMapper();
30-
//Generate schema from class
31-
AvroSchemaGenerator gen = new AvroSchemaGenerator();
32-
mapper.acceptJsonFormatVisitor(Nester.class, gen);
33-
Schema schema = gen.getGeneratedSchema().getAvroSchema();
34-
35-
//Serialize
36-
byte[] avroData = mapper.writer(new AvroSchema(schema))
37-
.writeValueAsBytes(fromJson);
38-
39-
//Deserialize
40-
Nester nester = mapper.readerFor(Nester.class)
41-
.with(new AvroSchema(schema))
42-
.readValue(avroData);
43-
int val = nester.nested.get("map").get("value");
44-
assertEquals(1, val);
45-
46-
}
16+
public static class Nester {
17+
@JsonProperty
18+
public Map<String,Map<String,Integer>> nested;
19+
}
20+
21+
/*
22+
/**********************************************************
23+
/* Test methods
24+
/**********************************************************
25+
*/
26+
27+
private final AvroMapper MAPPER = getMapper();
28+
29+
public void testSerialization() throws IOException
30+
{
31+
Nester fromJson = new ObjectMapper().readValue(
32+
"{\"nested\": {\"map\":{\"value\":1}}}"
33+
, Nester.class);
34+
35+
//Generate schema from class
36+
AvroSchemaGenerator gen = new AvroSchemaGenerator();
37+
MAPPER.acceptJsonFormatVisitor(Nester.class, gen);
38+
AvroSchema schema = gen.getGeneratedSchema();
39+
40+
//Serialize
41+
byte[] avroData = MAPPER.writer(schema)
42+
.writeValueAsBytes(fromJson);
43+
44+
//Deserialize
45+
Nester nester = MAPPER.readerFor(Nester.class)
46+
.with(schema)
47+
.readValue(avroData);
48+
int val = nester.nested.get("map").get("value");
49+
assertEquals(1, val);
50+
}
4751
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
<properties>
3535
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36-
<version.jackson.core>2.8.6</version.jackson.core>
36+
<version.jackson.core>2.8.7-SNAPSHOT</version.jackson.core>
3737
<version.asm>5.1</version.asm>
3838
</properties>
3939

0 commit comments

Comments
 (0)