Skip to content

Commit f0e6816

Browse files
committed
Fix #177
1 parent fc55b4d commit f0e6816

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ protected AvroStructureReader(AvroReadContext parent, int type, String typeId) {
1818
super(parent, typeId);
1919
_type = type;
2020
}
21+
22+
/*
23+
/**********************************************************************
24+
/* Metadata access
25+
/**********************************************************************
26+
*/
27+
28+
/**
29+
* Method that may be called to check if the values "read" by this reader
30+
* are zero-length, that is, consume no content: most common example being
31+
* Record with no fields.
32+
*<p>
33+
* Note: Arrays can not return {@code true} as they need to encode length
34+
* even for "empty" arrays.
35+
*
36+
* @since 2.10
37+
*/
38+
public boolean consumesNoContent() {
39+
return false;
40+
}
2141

2242
/*
2343
/**********************************************************************

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ protected RecordReader(AvroReadContext parent, AvroFieldReader[] fieldReaders, A
3535
@Override
3636
public String getCurrentName() { return _currentName; }
3737

38+
@Override
39+
public boolean consumesNoContent() {
40+
// 26-Aug-2019, tatu: As per [dataformats-binary#177], 0-field Records consume
41+
// no content. It may be possible other variants exist too (fields with "Constant"
42+
// value?), but let's start with the simple case
43+
return _fieldReaders.length == 0;
44+
}
45+
3846
@Override
3947
public final void skipValue(AvroParserImpl parser) throws IOException {
4048
for (int i = 0, end = _fieldReaders.length; i < end; ++i) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ public JsonToken nextToken() throws IOException {
2626
// First: possibly we are at end. Could theoretically check against
2727
// empty streams but...
2828
if (_parser.checkInputEnd()) {
29-
return null;
29+
// 26-Aug-2019, tatu: As per [dataformats-binary#177], 0-field Records consume
30+
// no content, and if so we MUST NOT indicate end-of-content:
31+
if (!_valueReader.consumesNoContent()) {
32+
return null;
33+
}
3034
}
3135
return _valueReader.newReader(this, _parser).nextToken();
3236
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package com.fasterxml.jackson.dataformat.avro.failing;
1+
package com.fasterxml.jackson.dataformat.avro;
22

33
import com.fasterxml.jackson.annotation.JsonAutoDetect;
44

55
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
66
import com.fasterxml.jackson.dataformat.avro.AvroSchema;
7-
import com.fasterxml.jackson.dataformat.avro.AvroTestBase;
87

9-
public class EmptyRecordRead177Test extends AvroTestBase
8+
public class RootEmptyRecord177Test extends AvroTestBase
109
{
1110
private final AvroMapper MAPPER = getMapper();
1211

@@ -25,7 +24,6 @@ public void testEmptyRecord() throws Exception {
2524
final Empty empty = new Empty();
2625

2726
byte[] ser = MAPPER.writer().with(SCHEMA).writeValueAsBytes(empty);
28-
2927
final Empty result = MAPPER.readerFor(Empty.class)
3028
.with(SCHEMA)
3129
.readValue(ser);

release-notes/VERSION-2.x

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ Project: jackson-datatypes-binaryModules:
1212

1313
#168: (avro) `JsonMappingException` for union types with multiple Record types
1414
(reported by Juliana A; fix contributed by Marcos P)
15-
#173: (Avro) Improve Union type serialization performance
15+
#173: (avro) Improve Union type serialization performance
1616
(fix contributed by Marcos P)
17+
#177: (avro) Deserialization of "empty" Records as root values fails
18+
(reported by Macros P)
1719

1820
2.10.0.pr1 (19-Jul-2019)
1921

0 commit comments

Comments
 (0)