Serializing optional fields in protobuf-net

I have a working Java client that interacts with google via serialized ProtoBuf messages. I am currently trying to translate this client to C #.

I have a file .proto

where the parameter appId

is an optional string. Its default in C # view generated by the protobuf-net library is an empty string, just like in the java view of the same file.

message AppsRequest {
  optional AppType appType = 1;
  optional string query = 2;
  optional string categoryId = 3;
  optional string appId = 4;
  optional bool withExtendedInfo = 6;
}

      

I found that when I explicitly set appId

in ""

in java client, the client stops working (403 Bad Request from Google). When I explicitly set appId

in null

in java client everything works, but only because it is hasAppId

set to false (I'm not sure how this affects serialization).

In C # client I always get 403 responses. I don't see any logic behind the difference between not setting a value and setting a default value, which seems to make all the difference in the java client. Since the output is always a binary stream, I'm not sure if successful java messages are serialized with an empty string, or not serialized at all.

In the C # client, I tried to set the IsRequired

attribute ProtoMember

to true to make them serialize, and I tried to set the default to null and set explicitly ""

, so I'm sure I tried some kind of config where the value is serialized. I also played ProtoBuf.ProtoIgnore

around with and at some point removed the parameter appId

altogether, but I was unable to avoid 403 errors in C #.

I tried to manually copy the serialized string from java and that resolved my problems, so I'm pretty sure the rest of the HTTP request works and the error can be traced to the serialized object.

My serialization is simple:

var clone = ProtoBuf.Serializer.DeepClone(request);

MemoryStream ms = new MemoryStream(2000);
ProtoBuf.Serializer.Serialize(ms, clone);

var bytearr = ms.ToArray();
string encodedData = Convert.ToBase64String(bytearr);

      

I'll admit I'm not entirely sure what it does DeepClone

. I've tried both with and without it ...

+2


a source to share


1 answer


It looks like we want to force it to be excluded; first of all, to give it a try, you can try using the "detectmissing" option in code generation. This is possible from the IDE and command line, but in a different way (let me know what you are using and I will add more).



Another similar option is to add (in the partial class) a bool {memberName}Specified {get;set;}

. There is an existing open strangeness report including blank lines by default that I am looking at.

+1


a source







All Articles