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

handle mongodb objectid _id in .mergeFromProto3Json() #940

Open
flutter-painter opened this issue Aug 3, 2024 · 0 comments
Open

handle mongodb objectid _id in .mergeFromProto3Json() #940

flutter-painter opened this issue Aug 3, 2024 · 0 comments

Comments

@flutter-painter
Copy link

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);
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant