google/protobuf.dart

handle mongodb objectid _id in .mergeFromProto3Json()

flutter-painter opened this issue · 0 comments

When reading a document from mongodb
Whatever the protobuf message format I fail to unmarshall the objectId using mergeFromProto3Json
the _id field always yield the exception :
FormatException: Protobuf JSON decoding failed at: root["_id"]. Expected JSON object
protobuf-3.1.0\lib\src\protobuf\proto3_json.dart line 411

This prevents me from smoothly using mongodb's objectId
Would it please be possible to solve this issue ?

I tested with message Testing and Testing2

message Testing {
  string _id = 1;
  string name = 2;
}

message Testing2 {
  ObjectIdProto _id = 1;
  string name = 2;
}

message ObjectIdProto{
  string ObjectId = 1;
}
import 'dart:io';

import 'package:mongo_dart/mongo_dart.dart';
import 'package:protobuf/protobuf.dart';

void main(List<String> args) async {
  final collectionName = 'testing_objectids';
  final db = TestConst.localDb;
  final connection = Connection(ConnectionManager(db));
  await db.open();
  await connection.connect();
  await db.createCollection('testing_objectids');

  final document = Testing.create();
  document.nana = 'test1';

  await db
      .collection(collectionName)
      .insertOne(document.toProto3Json() as Map<String, dynamic>);

  final doc =
      await db.collection(collectionName).findOne(where.eq('name', 'test1'));

// below will fail
  final dd = Testing.create()
    ..mergeFromProto3Json(
      doc,
      ignoreUnknownFields: true,
    );

  print(dd.toString());
  await connection.close();
  exit(exitCode);
}