Skip to content

Commit b1b34e0

Browse files
committed
Improve error handling for #98
1 parent 6269303 commit b1b34e0

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/VisitorFormatWrapperImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import org.apache.avro.Schema;
44

5+
import com.fasterxml.jackson.core.JsonGenerator;
6+
57
import com.fasterxml.jackson.databind.JavaType;
8+
import com.fasterxml.jackson.databind.JsonMappingException;
69
import com.fasterxml.jackson.databind.SerializerProvider;
10+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
711
import com.fasterxml.jackson.databind.jsonFormatVisitors.*;
812
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
913

@@ -164,9 +168,10 @@ public JsonNullFormatVisitor expectNullFormat(JavaType convertedType) {
164168
}
165169

166170
@Override
167-
public JsonAnyFormatVisitor expectAnyFormat(JavaType convertedType) {
171+
public JsonAnyFormatVisitor expectAnyFormat(JavaType convertedType) throws JsonMappingException {
168172
// could theoretically create union of all possible types but...
169-
return _throwUnsupported("'Any' type not supported: expectAnyFormat called with type "+convertedType);
173+
final String msg = "\"Any\" type (usually for `java.lang.Object`) not supported: `expectAnyFormat` called with type "+convertedType;
174+
throw InvalidDefinitionException.from((JsonGenerator) null, msg, convertedType);
170175
}
171176

172177
/*

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/TestSimpleGeneration.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.fasterxml.jackson.annotation.JsonValue;
99

1010
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
1112
import com.fasterxml.jackson.dataformat.avro.*;
1213

1314
import org.apache.avro.Schema;
@@ -57,20 +58,21 @@ public ByteBuffer getBytes() {
5758
/* Tests
5859
/**********************************************************
5960
*/
60-
61+
62+
private final AvroMapper MAPPER = newMapper();
63+
6164
public void testBasic() throws Exception
6265
{
63-
AvroMapper mapper = getMapper();
6466
AvroSchemaGenerator gen = new AvroSchemaGenerator();
65-
mapper.acceptJsonFormatVisitor(RootType.class, gen);
67+
MAPPER.acceptJsonFormatVisitor(RootType.class, gen);
6668
AvroSchema schema = gen.getGeneratedSchema();
6769
assertNotNull(schema);
6870

6971
String json = schema.getAvroSchema().toString(true);
7072
assertNotNull(json);
7173

7274
// And read it back too just for fun
73-
AvroSchema s2 = mapper.schemaFrom(json);
75+
AvroSchema s2 = MAPPER.schemaFrom(json);
7476
assertNotNull(s2);
7577

7678
// System.out.println("Basic schema:\n"+json);
@@ -88,15 +90,14 @@ public void testBasic() throws Exception
8890

8991
public void testEmployee() throws Exception
9092
{
91-
AvroMapper mapper = getMapper();
9293
AvroSchemaGenerator gen = new AvroSchemaGenerator();
93-
mapper.acceptJsonFormatVisitor(Employee.class, gen);
94+
MAPPER.acceptJsonFormatVisitor(Employee.class, gen);
9495
AvroSchema schema = gen.getGeneratedSchema();
9596
assertNotNull(schema);
9697

9798
String json = schema.getAvroSchema().toString(true);
9899
assertNotNull(json);
99-
AvroSchema s2 = mapper.schemaFrom(json);
100+
AvroSchema s2 = MAPPER.schemaFrom(json);
100101
assertNotNull(s2);
101102

102103
Employee empl = new Employee();
@@ -106,7 +107,7 @@ public void testEmployee() throws Exception
106107
empl.boss = null;
107108

108109
// So far so good: try producing actual Avro data...
109-
byte[] bytes = mapper.writer(schema).writeValueAsBytes(empl);
110+
byte[] bytes = MAPPER.writer(schema).writeValueAsBytes(empl);
110111
assertNotNull(bytes);
111112

112113
// and bring it back, too
@@ -120,15 +121,14 @@ public void testEmployee() throws Exception
120121

121122
public void testMap() throws Exception
122123
{
123-
AvroMapper mapper = getMapper();
124124
AvroSchemaGenerator gen = new AvroSchemaGenerator();
125-
mapper.acceptJsonFormatVisitor(StringMap.class, gen);
125+
MAPPER.acceptJsonFormatVisitor(StringMap.class, gen);
126126
AvroSchema schema = gen.getGeneratedSchema();
127127
assertNotNull(schema);
128128

129129
String json = schema.getAvroSchema().toString(true);
130130
assertNotNull(json);
131-
AvroSchema s2 = mapper.schemaFrom(json);
131+
AvroSchema s2 = MAPPER.schemaFrom(json);
132132
assertNotNull(s2);
133133

134134
// should probably verify, maybe... ?
@@ -139,18 +139,16 @@ public void testMap() throws Exception
139139
// [Issue#8]
140140
public void testWithDate() throws Exception
141141
{
142-
ObjectMapper mapper = new ObjectMapper(new AvroFactory());
143142
AvroSchemaGenerator gen = new AvroSchemaGenerator();
144-
mapper.acceptJsonFormatVisitor(WithDate.class, gen);
143+
MAPPER.acceptJsonFormatVisitor(WithDate.class, gen);
145144
AvroSchema schema = gen.getGeneratedSchema();
146145
assertNotNull(schema);
147146
}
148147

149148
public void testFixed() throws Exception
150149
{
151150
AvroSchemaGenerator gen = new AvroSchemaGenerator();
152-
ObjectMapper mapper = new ObjectMapper(new AvroFactory());
153-
mapper.acceptJsonFormatVisitor(WithFixedField.class, gen);
151+
MAPPER.acceptJsonFormatVisitor(WithFixedField.class, gen);
154152
Schema generated = gen.getAvroSchema();
155153
Schema fixedFieldSchema = generated.getField("fixedField").schema();
156154
assertEquals(Schema.Type.FIXED, fixedFieldSchema.getType());
@@ -160,4 +158,18 @@ public void testFixed() throws Exception
160158
assertEquals(Schema.Type.FIXED, wrappedFieldSchema.getType());
161159
assertEquals(8, wrappedFieldSchema.getFixedSize());
162160
}
161+
162+
// as per [dataformats-binary#98], no can do (unless we start supporting polymorphic
163+
// handling or something...)
164+
public void testSchemaForUntypedMap() throws Exception
165+
{
166+
try {
167+
MAPPER.schemaFor(Map.class);
168+
fail("Not expected to work yet");
169+
} catch (InvalidDefinitionException e) {
170+
verifyException(e, "\"Any\" type");
171+
verifyException(e, "not supported");
172+
verifyException(e, "`java.lang.Object`");
173+
}
174+
}
163175
}

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Modules:
2929
#69 (avro): Add support for `@AvroEncode` annotation
3030
#79 (proto): Fix wire type for packed arrays
3131
#95: Add new method, `withUnsafeReaderSchema` in `AvroSchema` to allow avoiding verification exception
32+
#98: [Avro] AvroMapper with Map throwing UnsupportedOperationException
33+
(reported by coder-hub@github)
3234
- [avro] Upgrade `avro-core` dep from 1.7.7 to 1.8.1
3335

3436
2.8.9.1 (not yet released)

0 commit comments

Comments
 (0)