Skip to content

Commit db9f825

Browse files
committed
Backport fix for #108
1 parent 53888b9 commit db9f825

File tree

4 files changed

+136
-11
lines changed

4 files changed

+136
-11
lines changed

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufParser.java

+26-6
Original file line numberDiff line numberDiff line change
@@ -737,12 +737,16 @@ private JsonToken _handleRootKey(int tag) throws IOException
737737
int id = (tag >> 3);
738738

739739
ProtobufField f;
740-
if ((_currentField == null) || (f = _currentField.nextOrThisIf(id)) == null) {
741-
f = _currentMessage.field(id);
742-
}
743-
// Note: may be null; if so, value needs to be skipped
744-
if (f == null) {
745-
return _skipUnknownField(id, wireType);
740+
if (_currentField != null) {
741+
if ((f = _currentField.nextOrThisIf(id)) == null) {
742+
if ((f = _currentMessage.field(id)) == null) {
743+
return _skipUnknownField(id, wireType);
744+
}
745+
}
746+
} else {
747+
if ((f = _currentMessage.field(id)) == null) {
748+
return _skipUnknownField(id, wireType);
749+
}
746750
}
747751
_parsingContext.setCurrentName(f.name);
748752
// otherwise quickly validate compatibility
@@ -769,9 +773,25 @@ private JsonToken _handleNestedKey(int tag) throws IOException
769773
int id = (tag >> 3);
770774

771775
ProtobufField f;
776+
if (_currentField != null) {
777+
if ((f = _currentField.nextOrThisIf(id)) == null) {
778+
if ((f = _currentMessage.field(id)) == null) {
779+
return _skipUnknownField(id, wireType);
780+
}
781+
}
782+
} else {
783+
if ((f = _currentMessage.field(id)) == null) {
784+
return _skipUnknownField(id, wireType);
785+
}
786+
}
787+
772788
if ((_currentField == null) || (f = _currentField.nextOrThisIf(id)) == null) {
773789
f = _currentMessage.field(id);
774790
}
791+
// Note: may be null; if so, value needs to be skipped
792+
if (f == null) {
793+
return _skipUnknownField(id, wireType);
794+
}
775795
_parsingContext.setCurrentName(f.name);
776796
if (!f.isValidFor(wireType)) {
777797
_reportIncompatibleType(f, wireType);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.fasterxml.jackson.dataformat.protobuf;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import com.fasterxml.jackson.core.JsonParser;
6+
7+
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
8+
9+
// [dataformats-binary#108]
10+
public class ReadNestedUnknownFieldsTest extends ProtobufTestBase
11+
{
12+
public static class LessNestedField {
13+
@JsonProperty(value = "f1", index = 1)
14+
private NestedOneField f1;
15+
16+
public NestedOneField getF1() {
17+
return f1;
18+
}
19+
20+
public void setF1(NestedOneField f1) {
21+
this.f1 = f1;
22+
}
23+
}
24+
25+
public static class MoreNestedField {
26+
27+
@JsonProperty(value = "f1", index = 1)
28+
private NestedTwoField f1;
29+
30+
public NestedTwoField getF1() {
31+
return f1;
32+
}
33+
34+
public void setF1(NestedTwoField f1) {
35+
this.f1 = f1;
36+
}
37+
}
38+
39+
public static class NestedOneField {
40+
41+
@JsonProperty(value = "nested2", index = 2)
42+
private int nested2;
43+
44+
public int getNested2() {
45+
return nested2;
46+
}
47+
48+
public void setNested2(int nested2) {
49+
this.nested2 = nested2;
50+
}
51+
}
52+
53+
public static class NestedTwoField {
54+
55+
@JsonProperty(value = "nested1", index = 1)
56+
private int nested1;
57+
58+
@JsonProperty(value = "nested2", index = 2)
59+
private int nested2;
60+
61+
public int getNested1() {
62+
return nested1;
63+
}
64+
65+
public void setNested1(int nested1) {
66+
this.nested1 = nested1;
67+
}
68+
69+
public int getNested2() {
70+
return nested2;
71+
}
72+
73+
public void setNested2(int nested2) {
74+
this.nested2 = nested2;
75+
}
76+
}
77+
78+
/*
79+
/**********************************************************
80+
/* Test methods
81+
/**********************************************************
82+
*/
83+
84+
final ProtobufMapper MAPPER = new ProtobufMapper();
85+
86+
public void testMultipleUnknown() throws Exception
87+
{
88+
MoreNestedField moreNestedField = new MoreNestedField();
89+
NestedTwoField nestedTwoField = new NestedTwoField();
90+
nestedTwoField.setNested1(1);
91+
nestedTwoField.setNested2(2);
92+
moreNestedField.setF1(nestedTwoField);
93+
94+
byte[] in = MAPPER.writerFor(MoreNestedField.class)
95+
.with(MAPPER.generateSchemaFor(MoreNestedField.class))
96+
.writeValueAsBytes(moreNestedField);
97+
98+
LessNestedField lesser = MAPPER.readerFor(LessNestedField.class)
99+
.with(MAPPER.generateSchemaFor(LessNestedField.class))
100+
// important: skip through unknown
101+
.with(JsonParser.Feature.IGNORE_UNDEFINED)
102+
.readValue(in);
103+
104+
assertEquals(moreNestedField.getF1().getNested2(), lesser.getF1().getNested2());
105+
}
106+
}

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ReadUnkownFieldsTest.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,17 @@ public void setF3(int f3) {
6868

6969
public void testMultipleUnknown() throws Exception
7070
{
71-
ProtobufMapper mapper = new ProtobufMapper();
72-
7371
ThreeField threeField = new ThreeField();
7472
threeField.setF1(1);
7573
threeField.setF2(2);
7674
threeField.setF3(3);
7775

7876
ProtobufSchema schemaWith3 = MAPPER.generateSchemaFor(ThreeField.class);
79-
byte[] in = mapper.writerFor(ThreeField.class).with(schemaWith3)
77+
byte[] in = MAPPER.writer(schemaWith3)
8078
.writeValueAsBytes(threeField);
8179

8280
ProtobufSchema schemaWith1 = MAPPER.generateSchemaFor(OneField.class);
83-
OneField oneField = mapper.readerFor(OneField.class).with(schemaWith1)
81+
OneField oneField = MAPPER.readerFor(OneField.class).with(schemaWith1)
8482
// important: skip through unknown
8583
.with(JsonParser.Feature.IGNORE_UNDEFINED)
8684
.readValue(in);

release-notes/VERSION

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ Modules:
1515

1616
2.8.11 (not yet released)
1717

18-
#106: fix calling _skipUnknownValue() twice
18+
#106: (protobuf) fix calling _skipUnknownValue() twice
1919
(reported, contributed fix for by marsqing@github@github)
20+
#108: (protobuf) fix NPE in skip unknown nested key
2021

2122
2.8.10 (24-Aug-2017)
2223

0 commit comments

Comments
 (0)