Skip to content

Commit 0295a5e

Browse files
committed
Minor clean up for #195
1 parent c570549 commit 0295a5e

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import org.apache.avro.Schema;
66

7-
import com.fasterxml.jackson.databind.ObjectMapper;
87
import com.fasterxml.jackson.dataformat.avro.deser.ScalarDecoder.*;
98
import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaHelper;
109

@@ -22,7 +21,6 @@ public abstract class AvroReaderFactory
2221
protected final static ScalarDecoder READER_LONG = new LongReader();
2322
protected final static ScalarDecoder READER_NULL = new NullReader();
2423
protected final static ScalarDecoder READER_STRING = new StringReader();
25-
private final static ObjectMapper DEFAULT_MAPPER = new ObjectMapper();
2624

2725
/**
2826
* To resolve cyclic types, need to keep track of resolved named
@@ -345,7 +343,7 @@ protected AvroStructureReader createRecordReader(Schema writerSchema, Schema rea
345343
if (!defaultFields.isEmpty()) {
346344
for (Schema.Field defaultField : defaultFields) {
347345
AvroFieldReader fr = AvroFieldDefaulters.createDefaulter(defaultField.name(),
348-
AvroSchemaHelper.parseDefaultValue(defaultField.defaultVal())
346+
AvroSchemaHelper.objectToJsonNode(defaultField.defaultVal())
349347
);
350348
if (fr == null) {
351349
throw new IllegalArgumentException("Unsupported default type: "+defaultField.schema().getType());

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public abstract class AvroSchemaHelper
2525
{
2626
/**
2727
* Dedicated mapper for handling default values (String <-> JsonNode <-> Object)
28+
*
29+
* @since 2.11
2830
*/
2931
private static final ObjectMapper DEFAULT_VALUE_MAPPER = new ObjectMapper();
3032

@@ -341,7 +343,9 @@ public static String getFullName(Schema schema) {
341343
return namespace + name;
342344
}
343345
StringBuilder sb = new StringBuilder(1 + namespace.length() + name.length());
344-
// Check if this is a nested class
346+
// 28-Feb-2020: [dataformats-binary#195] somewhat complicated logic of trying
347+
// to support differences between avro-lib 1.8 and 1.9...
348+
// Check if this is a nested class
345349
String nestedClassName = sb.append(namespace).append('$').append(name).toString();
346350
try {
347351
Class.forName(nestedClassName);
@@ -356,11 +360,17 @@ public static String getFullName(Schema schema) {
356360
}
357361
}
358362

359-
public static JsonNode parseDefaultValue(Object defaultValue) {
363+
/**
364+
* @since 2.11
365+
*/
366+
public static JsonNode objectToJsonNode(Object defaultValue) {
360367
return DEFAULT_VALUE_MAPPER.convertValue(defaultValue, JsonNode.class);
361368
}
362369

363-
public static Object convertDefaultValueToObject(JsonNode defaultJsonValue) {
370+
/**
371+
* @since 2.11
372+
*/
373+
public static Object jsonNodeToObject(JsonNode defaultJsonValue) {
364374
return DEFAULT_VALUE_MAPPER.convertValue(defaultJsonValue, Object.class);
365375
}
366376

@@ -370,6 +380,8 @@ public static Object convertDefaultValueToObject(JsonNode defaultJsonValue) {
370380
* @param defaultValue The default value to parse, in the form of a JSON string
371381
* @return a parsed JSON representation of the default value
372382
* @throws JsonMappingException If {@code defaultValue} is not valid JSON
383+
*
384+
* @since 2.11
373385
*/
374386
public static JsonNode parseDefaultValue(String defaultValue) throws JsonMappingException {
375387
if (defaultValue == null) {
@@ -378,6 +390,9 @@ public static JsonNode parseDefaultValue(String defaultValue) throws JsonMapping
378390
try {
379391
return DEFAULT_VALUE_MAPPER.readTree(defaultValue);
380392
} catch (JsonProcessingException e) {
393+
if (e instanceof JsonMappingException) {
394+
throw (JsonMappingException) e;
395+
}
381396
throw new JsonMappingException(null, "Failed to parse default value", e);
382397
}
383398
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ protected Schema.Field schemaFieldForWriter(BeanProperty prop, boolean optional)
186186
JsonNode defaultValue = AvroSchemaHelper.parseDefaultValue(prop.getMetadata().getDefaultValue());
187187
writerSchema = reorderUnionToMatchDefaultType(writerSchema, defaultValue);
188188
Schema.Field field = new Schema.Field(prop.getName(), writerSchema, prop.getMetadata().getDescription(),
189-
AvroSchemaHelper.convertDefaultValueToObject(defaultValue));
189+
AvroSchemaHelper.jsonNodeToObject(defaultValue));
190190

191191
AvroMeta meta = prop.getAnnotation(AvroMeta.class);
192192
if (meta != null) {

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project: jackson-datatypes-binaryModules:
1515
#192: (ion) Allow `IonObjectMapper` with class name annotation introspector to deserialize
1616
generic subtypes
1717
(reported, fix provided by Binh T)
18+
#195: Remove dependencies upon Jackson 1.X and Avro's JacksonUtils
19+
(contributed by Bryan H)
1820
- `AvroGenerator` overrides `getOutputContext()` properly
1921

2022
2.10.2 (05-Jan-2020)

0 commit comments

Comments
 (0)