[SERVER-74481] Unable to use BSON serializer/deserializer in a nested namespace Created: 01/Mar/23  Updated: 11/Apr/23

Status: Open
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Antonio Fuschetto Assignee: Backlog - Service Architecture
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Service Arch
Participants:

 Description   

Given an IDL defined in the namespace mongo (i.e. set to global.cpp_namespace) that defines a customer type with the serializer/deserializer functions in a nested namespace (e.g., mongo::util), the generated code doesn't take into account the specified namespace for the serializer/deserializer functions, causing a symbol not found error at compiling time.

More in details, given the following IDL:

global:
    cpp_namespace: "mongo"
...
 
types:
    status:
        bson_serialization_type: any
        cpp_type: "mongo::Status"
        serializer: "mongo::util::serializeStatusToBSON"
        deserializer: "mongo::util::deserializeStatusFromBSON"
...
 
structs:
    CoordinatorMetadata
    ...
    fields:
        error:
            type: status

The compiler generates the following code:

namespace mongo {
...
 
void CoordinatorMetadata::serialize(BSONObjBuilder* builder) const {
...
    if (_error) {
        serializeStatusToBSON((*_error), kErrorFieldName, builder);
    }
...
}}

The problem is that the function serializeStatusToBSON is defined in the namespace mongo::util, not in mongo.



 Comments   
Comment by Antonio Fuschetto [ 28/Mar/23 ]

Hi billy.donahue@mongodb.com and elizabeth.roytburd@mongodb.com, sorry for my late reply.

I'm not asking to create a mongo::util namespace. Let me describe to problem with a more tangible example:

  • The IDL is defined in the mongo namespace.
  • This IDL defines a ErrorStatus type.
  • Both serializer and deserializer functions must be defined in the same IDL's namespace (i.e., mongo) to generate C++ working code.

In this example, the serializer and deserializer functions have been defined in the mongo namespace instead of mongo::sharding_ddl_util to bypass the problem.

Can you suggest an alternative solution? At present I added the prefix sharding_ddl_util to the function names as a reminder that these should be defined in another namespace:

  • mongo::sharding_ddl_util_serializeErrorStatusToBSONmongo::sharding_ddl_util::serializeErrorStatusToBSON
  • mongo::sharding_ddl_util_deserializeErrorStatusFromBSONmongo::sharding_ddl_util::deserializeErrorStatusToBSON

Let me know if you need any additional information.

Comment by Billy Donahue [ 16/Mar/23 ]

Hi Antonio. A couple things from the ServiceArch triage of this ticket here.
We are trying to figure out what the motivation is here.

We don't want to create a "mongo::util" namespace.

A central serializeStatusToBSON function would be a design of its own and not done as part of another ticket as a side-effect.
It could have system-wide effects if it's not done with the right consideration of scope and flexibility.

I'm not clear about what your "only solution" above is doing or why it would be unacceptable.
Can you clarify this?

The stripping of the "mongo::util::" prefix from "serializeStatusToBSON" in the generated code doesn't look right to me. If I understand correctly, this is the main idea of the bug report here?

Comment by Antonio Fuschetto [ 01/Mar/23 ]

I tried to use different alternatives for the type definition, e.g., the one below, but the problem is still the same. The only solution is to define the IDL in the same nested namespace, which is not acceptable in many cases.

status:
    bson_serialization_type: any
    cpp_type: "::mongo::Status"
    serializer: "::mongo::util::serializeStatusToBSON"
    deserializer: "::mongo::util::deserializeStatusFromBSON" 

Generated at Thu Feb 08 06:27:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.