rongfengliang / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

ProtoMembers with IsRequired=true aren't really reqired #262

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I am not sure, if this is a bug, or it's by design (or whether I am missing 
something). But definitely it is behavior I would not expect.

What steps will reproduce the problem?
1. Create a message with a ProtoMember with the IsRequired property set to true
2. Create an empty MemoryStream
3. Deserialize empty MemoryStream

please, see attachment for sample.

What is the expected output? What do you see instead?
Deserialize returns deserialized message and the property which is required is 
set to it's default value.

I would expect Deserialize function to fail (e.g. throw an exception, return 
null) or at least provide some kind of warning, because required ProtomeMember 
is not included in the message

The situation is the same if input stream contains random data, that doesn't 
represent valid proto message.

What version of the product are you using? On what operating system?
protobuf-net v2 r480, Win 7

Original issue reported on code.google.com by lu...@kabrt.cz on 2 Jan 2012 at 10:37

Attachments:

GoogleCodeExporter commented 9 years ago
I had a similar problem where code like

class TestClass 
{ 
 [ProtoMember(3,IsRequired=true)]
 TestObject myObj;
}
class TestObj
{
[ProtoMember(1)]
double Value1;
[ProtoMember(2)]
double Value2;
}

would not end up sending the object in the case of default values, because the 
IsRequired value doesn't flow down to the TestObject.  If you set IsRequired to 
true in the object, it works as expected.

It can be a little odd if you use TestObj everywhere, like it's a base type 
that is common throughout your application because the IsRequired = true seems 
to flow up.  Setting it on the TestObject will override whatever declaration 
you put on the instantiated object itself.

So, this will always serialize the object and send it even though the object is 
not required:

class TestClass 
{ 
 [ProtoMember(3,IsRequired=false)]
 TestObject myObj;
}
class TestObj
{
[ProtoMember(1,IsRequired=true)]
double Value1;
[ProtoMember(2,IsRequired=true)]
double Value2;
}

v480, Win7.

Original comment by mikepe...@gmail.com on 3 Feb 2012 at 9:14

GoogleCodeExporter commented 9 years ago
Any further developments with this? I can't find any documentation on the 
IsRequired option? Thanks.

Original comment by p...@mac.gen.nz on 24 Feb 2012 at 11:42

GoogleCodeExporter commented 9 years ago
Currently, IsRequired primarily impacts **serialization**, forcing it to ignore 
some default rules. It is perhaps a little too forgiving at deserialization; 
enforcing this data at the point of deserialization is on the list for 
attention at some point (on an opt-in basis).

Original comment by marc.gravell on 24 Feb 2012 at 12:14