-
Notifications
You must be signed in to change notification settings - Fork 620
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
Add @Default annotation to optionally mark dynamically initialized attibutes with constand value #1345
Comments
Be careful with using property default values especially For your case, you may try to declare a format like this
|
Thank you @shanshin,
So it is probably not a bug of a library, but its normal behavior. I will tell if it fix my issue. |
My approach solve de-serialization issue, but Init block executes after values mapping from Proto and overrides de-serializer values. Workaround will be to override required properties only if they have no default value, because Init block executes after de-serialization.
|
I propose you to add @default(<constant_value>) annotation to be able to override default behavior and allow developers to use dynamically defined default values in a class. |
@ComBatVision yes, init block can override deserialized value.
|
@default annotation looks excessively in standard situation, when it is not required, but it can help to solve issue with dynamic default values: My case with DateTime.now() or any other case where attribute receive dynamic default value will face current issue. |
|
For difficult cases, you can define a custom serializer and implement the required behavior. |
Ok. As you wish. It was just my proposition to minimize code. I have solved my initial issue by using init block. I did not proposed to make Default annotation obligatory. I just propose it to be used in case you want to force serializer to ignore usual default value in attribute. |
Existing |
Very strange behavior of Protobuf serialization found.
It always works perfect in debug session, but when I execute the same single thread code in usual run mode and repeat the same serialization in loop it fails to serialize one field from the class approx. 70% of time, but works other 30% times.
To Reproduce
There are parent abstract class and its child class like this:
I have created usual Proto2 declaration and generate Java classes with protoc to test serialization like this:
When I execute following code, I receive error - "InvalidProtocolBufferException: Message missing required fields: value.reportingDate"
I have logged serialization process, and I found that reportingDate is always present in thing object before serialization, but the length of resulting serialized byte array is sometimes 106, and sometimes 113 bytes during the same runtime, just different loop steps.
When I run the same code in debug, serialized length is always 113 and there is no error.
Error is always related to Reporting date field.
Expected behavior
It should de-serialize byte array with Google Proto2 class all times, not only in debug mode. Or at least byte array length should keep the same.
Environment
The text was updated successfully, but these errors were encountered: