Skip to content
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

Issue with TRecord serialization and skipped empty string, false booleans and empty arrays #110

Open
larsenbjorn opened this issue Dec 3, 2021 · 3 comments

Comments

@larsenbjorn
Copy link
Contributor

In procedure MARS.Core.JSON.pas - TJSONObjectHelper.WriteTValue some fields are skipped if the value is empty. I see that this is beneficial in some cases, but also problematic in other cases.

We use records that are serialized in combination with PUT/PATCH requests. Now we can't know if a string field is supposed to be cleared or not, if a boolean field is supposed to be set to false or not and so on.

I think we need a way to control if serialization should skip these fields or not.

Thank you for your effort. Really like your work with support for OpenAPI 3.

@andrea-magni andrea-magni self-assigned this Dec 4, 2021
andrea-magni added a commit that referenced this issue Dec 7, 2021
…ptions.

Addresses the issue #110. I really want to find a better solution for the general problem (serialization options/tweaks) but this may be used as a temporary workaround by those affected by the breaking change I introduced with the new serialization mechanism.
@andrea-magni
Copy link
Owner

Hi @larsenbjorn , thanks for the kind words and for pointing out this issue.
I've just pushed a workaround that may be of help in your case.
To activate it, simply add somewhere very early (initialization section of the Server.Resources.pas or equivalent file) the following line:

DefaultMARSJSONSerializationOptions.SkipEmptyValues := False;

And MARS will serialize empty values to JSON.
I really want to find a better approach to address the general problem (tweak for serialization mechanism) but in the meantime this may be used as temporary workaround.

Let me know your thoughts about this, please.

Sincerely,
Andrea

@larsenbjorn
Copy link
Contributor Author

Hi,

Thank you for such quick reply and workaround!

We will be able to utilize this workaround as a temporary solution for now as we have not utilized the OpenAPI feature yet.

Changing the options to DefaultMARSJSONSerializationOptions.SkipEmptyValues := False will create an abnormality with the OpenAPI feature.
When serializing empty values the OpenAPI/swagger UI documentation will list lots of endpoints in a default section.
I guess this is part of the reason why you changed the serialization?

Since DefaultMARSJSONSerializationOptions is a global variable, I guess changing this per resource/endpoint will give funny results for concurrent requests :-)

A future solution will maybe allow for separate serialization options for each method (endpoint)?
As for serialization options for dates (issue #111) - I think the most flexibility solution would be per field in a record.

Best regards,
Bjørn

@andrea-magni
Copy link
Owner

For sure I am going to let the serialization options to be specified on many levels (Writer, Method, data type definition).
This sounds flexible enough and not too hard to implement (but you never actually know until it's done! :-) )

In general, I never wanted to write a persistence layer into MARS because I knew there would be many corner cases and little tricky things. But at the same time at some point I needed it so here we come.

Stay tuned, I'll try to work on this ASAP.

Sincerely,
Andrea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants