Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeated enum does not honor ignoreUnknownFields #849

Open
Anushi1998 opened this issue Jun 17, 2023 · 1 comment · May be fixed by #853
Open

Repeated enum does not honor ignoreUnknownFields #849

Anushi1998 opened this issue Jun 17, 2023 · 1 comment · May be fixed by #853
Labels

Comments

@Anushi1998
Copy link

Consider following snippet of code:

import 'dart:convert';

import 'response.pb.dart';

void main() {
  final response = Response()
    ..repeatedEnumList.addAll([EnumType.ENUM_TYPE_C, EnumType.ENUM_TYPE_B])
    ..singleEnumValue = EnumType.ENUM_TYPE_A;

  final json = response.toProto3Json() as Map<String, dynamic>;
  print(json);
  final serverAndClientInSync = Response.create()..mergeFromProto3Json(json);
 
  // WORKS
  final json2 = json;
  json2['singleEnumValue'] = 'ENUM_TYPE_CLIENT_UNAWARE';
  print(json2);
  final worksForUpgradedSingleField = Response.create()
    ..mergeFromProto3Json(json2, ignoreUnknownFields: true);

  // FAILS
  final json3 = json;
  json3['repeatedEnumList'][0] = 'ENUM_TYPE_CLIENT_UNAWARE';
  print(json3);
  final failsForUpgradedRepeatedEnumField = Response.create()
    ..mergeFromProto3Json(json3, ignoreUnknownFields: true);
}

Output

{repeatedEnumList: [ENUM_TYPE_C, ENUM_TYPE_B], singleEnumValue: ENUM_TYPE_A}
{repeatedEnumList: [ENUM_TYPE_C, ENUM_TYPE_B], singleEnumValue: ENUM_TYPE_CLIENT_UNAWARE}
{repeatedEnumList: [ENUM_TYPE_CLIENT_UNAWARE, ENUM_TYPE_B], singleEnumValue: ENUM_TYPE_CLIENT_UNAWARE}
Unhandled exception:
type 'Null' is not a subtype of type 'EnumType' of 'element'
#0      PbList.add (package:protobuf/src/protobuf/pb_list.dart)
#1      _mergeFromProto3Json.recursionHelper.<anonymous closure> (package:protobuf/src/protobuf/proto3_json.dart:385:24)
#2      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:625:13)
#3      _mergeFromProto3Json.recursionHelper (package:protobuf/src/protobuf/proto3_json.dart:338:14)
#4      _mergeFromProto3Json (package:protobuf/src/protobuf/proto3_json.dart:416:3)
#5      GeneratedMessage.mergeFromProto3Json (package:protobuf/src/protobuf/generated_message.dart:267:7)
#6      main (package:enums/enums.dart:26:7)
#7      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:296:19)
#8      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189:12)

Full Code Snippet: https://github.com/Anushi1998/DartProtobufBug/

In case of single enum field, unknown value is ignored but for repeated enums it tried to add null.
If we can avoid adding null here it will support backward compatability for repeated enums too. Happy to send a patch if this approach works.

@osa1 osa1 added the bug label Jun 20, 2023
@osa1
Copy link
Member

osa1 commented Jun 20, 2023

Good catch. I think your fix should work, convertProto3JsonValue returns null only on unknown enum values, so just skipping those when adding to the list should work.

osa1 added a commit to osa1/protobuf.dart that referenced this issue Jun 22, 2023
@osa1 osa1 linked a pull request Jun 22, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants