-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[BUG?] Problem roundtripping null strings #969
Comments
Update: it seems to do the same with a generic class too, not just strings: // TEST DATA
TestData? td1 = null;
TestData? td2;
using (var ms = new MemoryStream())
{
RuntimeTypeModel.Default.Serialize(ms, td1);
data = ms.ToArray();
}
using (var ms = new MemoryStream(data))
{
td2 = RuntimeTypeModel.Default.Deserialize<TestData?>(ms);
}
Console.WriteLine($"data.Length: {data.Length}");
Console.WriteLine($"td1 ({td1}) is null: {td1 is null}");
Console.WriteLine($"td2 ({td2}) is null: {td2 is null}");
[DataContract]
public class TestData
{
[DataMember(Order = 1)]
public int Id { get; set; }
[DataMember(Order = 2)]
public string? Name { get; set; }
} The output is:
And the value of td2 is this: so likely the result of just calling the ctor, I suppose. Thanks in advance for any help. |
Another update: I think I'm getting to the bottom of it, and sorry if I'm kinda using an issue on your repo here to basically explore Protobuf and learn more about it, but I haven't found a clear description around the web about the general approach to use and the expectations to have. ExplorationBased on my readings and experiments it seems to me that For example when serializing a In protobuf-net, both when serializing a I also noticed the same happens if I serialize an instance created by simply calling The unexpected thing though is that the inner Of course I'm not necessarily pointing to protobuf-net here, probably is more of a Protobuf (the protocol itself) thing, maybe part of the so called undefined behaviour world where different implementations may differ, in this case around Am I getting it right? Safe approach?Because of those premises, in general I'm thinking about what can be the best approach: to always use some sort of a "container structure" like a class, and not work directly with "simple values" ( Would you agree on such an approach? SuggestionIf all of this is correct, may I suggest adding a paragraph in the README file? Even though it would not be specific to protobuf-net but more of a protocol general thing, I assume people would be less confused and feel more welcome when approaching Protobuf in general, and protobuf-net in particular, if such concepts would be highlighted directly up-front. |
Hi, I'm using protobuf-net v3.1.22 on net6.0 (using VS2022 Community v17.3.6, just in case) and I'm observing inconsistencies while roundtripping
null
strings.Here's the minimal repro:
The output of this is:
When I serialize a
null
value, in both cases (string
andint
) it seems to produce - at least based on with understanding of how Protobuf works - the expected payload which is an empty byte[] (byte[0]
).When deserializing the
string
though, I expected to get backnull
but instead got an empty string.With
int?
instead it seems to work fine, correctly returningnull
.All the default settings are unchanged, and as can be seen I'm using the default TypeModel: the one above is the entire repro code.
In case it can be useful, this is the csproj:
I've found the issue #698 but I'm not sure this is the same thing since that involved a wrapping class.
Am I doing something wrong?
Thanks!
The text was updated successfully, but these errors were encountered: