Skip to content

Commit d51bb02

Browse files
authored
Fix #584 (missing END_OBJECT) (#585)
1 parent 8f2797e commit d51bb02

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

protobuf/src/main/java/tools/jackson/dataformat/protobuf/ProtobufParser.java

+23-14
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,12 @@ private JsonToken _handleRootKey(int tag) throws JacksonException
679679
if (_currentField != null) {
680680
if ((f = _currentField.nextOrThisIf(id)) == null) {
681681
if ((f = _currentMessage.field(id)) == null) {
682-
return _skipUnknownField(id, wireType);
682+
return _skipUnknownField(id, wireType, false);
683683
}
684684
}
685685
} else {
686686
if ((f = _currentMessage.field(id)) == null) {
687-
return _skipUnknownField(id, wireType);
687+
return _skipUnknownField(id, wireType, false);
688688
}
689689
}
690690
_streamReadContext.setCurrentName(f.name);
@@ -715,12 +715,12 @@ private JsonToken _handleNestedKey(int tag) throws JacksonException
715715
if (_currentField != null) {
716716
if ((f = _currentField.nextOrThisIf(id)) == null) {
717717
if ((f = _currentMessage.field(id)) == null) {
718-
return _skipUnknownField(id, wireType);
718+
return _skipUnknownField(id, wireType, true);
719719
}
720720
}
721721
} else {
722722
if ((f = _currentMessage.field(id)) == null) {
723-
return _skipUnknownField(id, wireType);
723+
return _skipUnknownField(id, wireType, true);
724724
}
725725
}
726726

@@ -729,7 +729,7 @@ private JsonToken _handleNestedKey(int tag) throws JacksonException
729729
}
730730
// Note: may be null; if so, value needs to be skipped
731731
if (f == null) {
732-
return _skipUnknownField(id, wireType);
732+
return _skipUnknownField(id, wireType, true);
733733
}
734734
_streamReadContext.setCurrentName(f.name);
735735
if (!f.isValidFor(wireType)) {
@@ -891,8 +891,10 @@ private JsonToken _readNextValue(FieldType t, int nextState) throws JacksonExcep
891891
return type;
892892
}
893893

894-
private JsonToken _skipUnknownField(int tag, int wireType) throws JacksonException
894+
private JsonToken _skipUnknownField(int tag, int wireType, boolean nestedField) throws JacksonException
895895
{
896+
//System.out.println(" _skipUnknownField(tag="+tag+", wireType="+wireType+")");
897+
896898
// First: is this even allowed?
897899
if (!isEnabled(StreamReadFeature.IGNORE_UNDEFINED)) {
898900
_reportErrorF("Undefined property (id %d, wire type %d) for message type %s: not allowed to ignore, as `JsonParser.Feature.IGNORE_UNDEFINED` disabled",
@@ -905,7 +907,7 @@ private JsonToken _skipUnknownField(int tag, int wireType) throws JacksonExcepti
905907
if (_checkEnd()) { // updates _parsingContext
906908
return _updateToken(JsonToken.END_OBJECT);
907909
}
908-
if (_state == STATE_NESTED_KEY) {
910+
if (nestedField) {
909911
if (_inputPtr >= _inputEnd) {
910912
loadMoreGuaranteed();
911913
}
@@ -924,7 +926,14 @@ private JsonToken _skipUnknownField(int tag, int wireType) throws JacksonExcepti
924926
continue;
925927
}
926928
_streamReadContext.setCurrentName(_currentField.name);
927-
_state = STATE_ROOT_VALUE;
929+
930+
// 30-Apr-2025, tatu: [dataformats-binary#584] may be called for root and nested
931+
if (nestedField) {
932+
_state = STATE_NESTED_VALUE;
933+
} else {
934+
_state = STATE_ROOT_VALUE;
935+
}
936+
928937
// otherwise quickly validate compatibility
929938
if (!_currentField.isValidFor(wireType)) {
930939
_reportIncompatibleType(_currentField, wireType);
@@ -980,7 +989,7 @@ public String nextName() throws JacksonException
980989

981990
ProtobufField f = _findField(id);
982991
if (f == null) {
983-
if (_skipUnknownField(id, wireType) != JsonToken.PROPERTY_NAME) {
992+
if (_skipUnknownField(id, wireType, false) != JsonToken.PROPERTY_NAME) {
984993
return null;
985994
}
986995
// sub-optimal as skip method already set it, but:
@@ -1017,7 +1026,7 @@ public String nextName() throws JacksonException
10171026

10181027
ProtobufField f = _findField(id);
10191028
if (f == null) {
1020-
if (_skipUnknownField(id, wireType) != JsonToken.PROPERTY_NAME) {
1029+
if (_skipUnknownField(id, wireType, true) != JsonToken.PROPERTY_NAME) {
10211030
return null;
10221031
}
10231032
// sub-optimal as skip method already set it, but:
@@ -1068,7 +1077,7 @@ public boolean nextName(SerializableString sstr) throws JacksonException
10681077

10691078
ProtobufField f = _findField(id);
10701079
if (f == null) {
1071-
_skipUnknownField(id, wireType);
1080+
_skipUnknownField(id, wireType, false);
10721081
// may or may not match, but let caller figure it out
10731082
return false;
10741083
}
@@ -1104,7 +1113,7 @@ public boolean nextName(SerializableString sstr) throws JacksonException
11041113

11051114
ProtobufField f = _findField(id);
11061115
if (f == null) {
1107-
_skipUnknownField(id, wireType);
1116+
_skipUnknownField(id, wireType, true);
11081117
// may or may not match, but let caller figure it out
11091118
return false;
11101119
}
@@ -1154,7 +1163,7 @@ public int nextNameMatch(PropertyNameMatcher matcher) throws JacksonException
11541163

11551164
ProtobufField f = _findField(id);
11561165
if (f == null) {
1157-
JsonToken t = _skipUnknownField(id, wireType);
1166+
JsonToken t = _skipUnknownField(id, wireType, false);
11581167
if (t != JsonToken.PROPERTY_NAME) {
11591168
return (t == JsonToken.END_OBJECT)
11601169
? PropertyNameMatcher.MATCH_END_OBJECT : PropertyNameMatcher.MATCH_ODD_TOKEN;
@@ -1192,7 +1201,7 @@ public int nextNameMatch(PropertyNameMatcher matcher) throws JacksonException
11921201

11931202
ProtobufField f = _findField(id);
11941203
if (f == null) {
1195-
JsonToken t = _skipUnknownField(id, wireType);
1204+
JsonToken t = _skipUnknownField(id, wireType, true);
11961205
if (t != JsonToken.PROPERTY_NAME) {
11971206
return (t == JsonToken.END_OBJECT)
11981207
? PropertyNameMatcher.MATCH_END_OBJECT : PropertyNameMatcher.MATCH_ODD_TOKEN;

protobuf/src/test/java/tools/jackson/dataformat/protobuf/ReadNestedUnknownFieldsTest.java

+7-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
1010

1111
import tools.jackson.core.*;
12-
import tools.jackson.databind.MapperFeature;
1312
import tools.jackson.databind.ObjectReader;
1413
import tools.jackson.dataformat.protobuf.schema.ProtobufSchema;
1514

@@ -124,21 +123,14 @@ public static class Embed {
124123
}
125124

126125
/*
127-
/**********************************************************
126+
/**********************************************************************
128127
/* Test methods
129-
/**********************************************************
128+
/**********************************************************************
130129
*/
131130

132-
// 30-Apr-2025, tatu: Looks like we have a bug of some kind, exposed
133-
// by change to `MapperFeature.DEFAULT_VIEW_INCLUSION` defaults
134-
// (changed to `false`) but probably not caused by the change
131+
private final ProtobufMapper MAPPER = newObjectMapper();
135132

136-
// private final ProtobufMapper MAPPER = newObjectMapper();
137-
private final ProtobufMapper MAPPER = newMapperBuilder()
138-
.enable(MapperFeature.DEFAULT_VIEW_INCLUSION)
139-
.build();
140-
141-
// [dataformats-binary#108]
133+
// [dataformats-binary#108], [dataformats-binary#584]
142134
@Test
143135
public void testMultipleUnknown() throws Exception
144136
{
@@ -159,9 +151,7 @@ public void testMultipleUnknown() throws Exception
159151

160152
//System.err.println("-> "+MAPPER.valueToTree(protoR.readValue(doc)).toPrettyString());
161153

162-
// 30-Apr-2025, tatu: First, iterate over tokens
163-
// ... alas, does not actually
164-
/*
154+
// 30-Apr-2025, tatu: [dataformats-binary#584]: First, iterate over tokens
165155
try (JsonParser p = protoR.createParser(doc)) {
166156
assertToken(JsonToken.START_OBJECT, p.nextToken());
167157
assertToken(JsonToken.PROPERTY_NAME, p.nextToken());
@@ -176,9 +166,8 @@ public void testMultipleUnknown() throws Exception
176166
assertToken(JsonToken.END_OBJECT, p.nextToken());
177167
assertNull(p.nextToken());
178168
}
179-
*/
180169

181-
// and only then use databinding
170+
// and only then test databinding
182171

183172
LessNestedField lesser = protoR.readValue(doc);
184173

@@ -201,7 +190,7 @@ public void testCheckEndAfterSkip() throws Exception
201190

202191
OuterV2 v2Expected = new OuterV2();
203192
v2Expected.embed = embedV2;
204-
v2Expected.state="state";
193+
v2Expected.state = "state";
205194

206195
// serialize type with extra field
207196
byte[] doc = mapper.writer(schemaV2).writeValueAsBytes(v2Expected);

0 commit comments

Comments
 (0)