@@ -164,6 +164,8 @@ void _mergeFromProto3Json(
164
164
ignoreUnknownFields, supportNamesWithUnderscores, permissiveEnums);
165
165
166
166
void recursionHelper (Object ? json, _FieldSet fieldSet) {
167
+ // Convert a JSON object to proto object. Returns `null` on unknown enum
168
+ // values when [ignoreUnknownFields] is [true].
167
169
Object ? convertProto3JsonValue (Object value, FieldInfo fieldInfo) {
168
170
final fieldType = fieldInfo.type;
169
171
switch (PbFieldType ._baseType (fieldType)) {
@@ -174,14 +176,12 @@ void _mergeFromProto3Json(
174
176
throw context.parseException ('Expected bool value' , json);
175
177
case PbFieldType ._BYTES_BIT :
176
178
if (value is String ) {
177
- Uint8List result;
178
179
try {
179
- result = base64Decode (value);
180
+ return base64Decode (value);
180
181
} on FormatException {
181
182
throw context.parseException (
182
183
'Expected bytes encoded as base64 String' , json);
183
184
}
184
- return result;
185
185
}
186
186
throw context.parseException (
187
187
'Expected bytes encoded as base64 String' , value);
@@ -264,14 +264,12 @@ void _mergeFromProto3Json(
264
264
case PbFieldType ._SFIXED64_BIT :
265
265
if (value is int ) return Int64 (value);
266
266
if (value is String ) {
267
- Int64 result;
268
267
try {
269
- result = Int64 .parseInt (value);
268
+ return Int64 .parseInt (value);
270
269
} on FormatException {
271
270
throw context.parseException (
272
271
'Expected int or stringified int' , value);
273
272
}
274
- return result;
275
273
}
276
274
throw context.parseException (
277
275
'Expected int or stringified int' , value);
@@ -368,9 +366,12 @@ void _mergeFromProto3Json(
368
366
throw context.parseException ('Expected a String key' , subKey);
369
367
}
370
368
context.addMapIndex (subKey);
371
- fieldValues[decodeMapKey (subKey, mapFieldInfo.keyFieldType)] =
372
- convertProto3JsonValue (
373
- subValue, mapFieldInfo.valueFieldInfo);
369
+ final key = decodeMapKey (subKey, mapFieldInfo.keyFieldType);
370
+ final value = convertProto3JsonValue (
371
+ subValue, mapFieldInfo.valueFieldInfo);
372
+ if (value != null ) {
373
+ fieldValues[key] = value;
374
+ }
374
375
context.popIndex ();
375
376
});
376
377
} else {
@@ -382,7 +383,10 @@ void _mergeFromProto3Json(
382
383
for (var i = 0 ; i < value.length; i++ ) {
383
384
final entry = value[i];
384
385
context.addListIndex (i);
385
- values.add (convertProto3JsonValue (entry, fieldInfo));
386
+ final parsedValue = convertProto3JsonValue (entry, fieldInfo);
387
+ if (parsedValue != null ) {
388
+ values.add (parsedValue);
389
+ }
386
390
context.popIndex ();
387
391
}
388
392
} else {
@@ -402,8 +406,15 @@ void _mergeFromProto3Json(
402
406
original.mergeFromMessage (parsedSubMessage);
403
407
}
404
408
} else {
405
- fieldSet._setFieldUnchecked (
406
- meta, fieldInfo, convertProto3JsonValue (value, fieldInfo));
409
+ final parsedValue = convertProto3JsonValue (value, fieldInfo);
410
+ if (parsedValue == null ) {
411
+ // Unknown enum
412
+ if (! ignoreUnknownFields) {
413
+ throw context.parseException ('Unknown enum value' , value);
414
+ }
415
+ } else {
416
+ fieldSet._setFieldUnchecked (meta, fieldInfo, parsedValue);
417
+ }
407
418
}
408
419
context.popIndex ();
409
420
});
0 commit comments