tablesmit / protobuf-net

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

(de-)serialization of messages with missing required fields #72

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
email: s.volchkov@gmail.com

Hello, Marc!
Is there a way with protobuf-net to make (de-)serialization of a message
fail with an exception if any of the fields marked as 'required' in .proto
file are in fact not there? Currently, the corresponding fields are just
assigned default values, specifically null for reference types. Checking
all fields for null is quite tedious; since the field is marked as
required, I would like to have a guarantee that the value is there if
deserialization went fine. On the other hand, during serialization, I would
like to be sure that I have filled in all the required fields: I use
google's c++ protobuf implementation on the other wire side, and it is less
forgiving with regards to omitting the required values.

What steps will reproduce the problem?

(for deserialization)
1. Create an empty MemoryStream.
2. Pass it to Serializer.NonGeneric.Deserialize() with a type with
ProtoContract and required fields.

(for serialization)
1. Create a protobuf message with required fields, do not assign value to
the field.
2. Invoke Serializer.NonGeneric.Serialize() with the message.

What is the expected output? What do you see instead?

I would like the both methods of Serializer.NonGeneric to fail. Instead,
both operations succeed; in the first case I get an object with all null
fields, in the second case I receive a message stream that cannot be
deserialized back with google's protobuf.

What version of the product are you using? On what operating system?
At the moment I am using r263.

Original issue reported on code.google.com by s.volchkov on 26 Aug 2009 at 4:00

GoogleCodeExporter commented 8 years ago
In our current project we are looking for the same feature as well. I made a 
few 
minor changes (in csharp.xslt and the Property classes in protobuf-net) to make 
it 
check required fields (well, properties only) on serialisation. We also use 
the "WithLengthPrefix" version for serialisation which provides some degree of 
checking on deserialisation.

We only use a limited set of features in protobuf-net so while the change is 
sufficient for us, it probably would not work in other scenarios (e.g. we only 
use .proto files to generate C# classes rather than defining them directly). 
Not 
sure if Marc would be interested in a patch?

Original comment by ken.c....@gmail.com on 2 Oct 2009 at 8:31

GoogleCodeExporter commented 8 years ago
This is something I'd like to have available, but for various reasons I've had 
very 
little time to look at it lately. I'd welcome a patch, of course. I'd need to 
see how 
the patch impacts existing code etc, too.

Original comment by marc.gravell on 2 Oct 2009 at 8:44

GoogleCodeExporter commented 8 years ago
Attached is the workaround I'm using. Basically it's just adding an extra 
private 
field to keep track of whether a required field has been set.

Original comment by ken.c....@gmail.com on 6 Oct 2009 at 12:55

Attachments:

GoogleCodeExporter commented 8 years ago
Are there any plans to incorporate this functionality into the library? If not, 
is there any way to check if all required fields were deserialized (= found in 
the incoming protobuf binary representation)? 

Original comment by boris.me...@gmail.com on 30 Apr 2014 at 12:32