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

null values are stripped when serializing #604

Closed
larssn opened this issue Mar 12, 2019 · 12 comments · Fixed by #631
Closed

null values are stripped when serializing #604

larssn opened this issue Mar 12, 2019 · 12 comments · Fixed by #631
Assignees

Comments

@larssn
Copy link

larssn commented Mar 12, 2019

Hello, thanks for a wonderful plugin. It's making our boilerplate related headacres go away.

However we're running into a minor issue serializing data into a Firestore compatible Map. The generated serializers are stripping out nulls (which makes sense in a JSON context).

The offending code is the if that is generated in the .g.dart files, for example:

if (object.color != null) {
      result
        ..add('color')
        ..add(serializers.serialize(object.color,
            specifiedType: const FullType(String)));
}

If we comment it out, our resulting Map has keys with nulls, as desired.

Any way to make built_value not generate that if check?

@davidmorgan
Copy link
Collaborator

Not currently.

We could potentially add it as a configuration setting on the serializer field.

@BuiltValueSerializer(includeNulls: true)

From your side, you could post-process the data to add in the nulls, but that's a bit awkward unless you have the list of keys / field names handy.

@davidmorgan davidmorgan self-assigned this Mar 13, 2019
@larssn
Copy link
Author

larssn commented Mar 13, 2019

Well it's a bit of a pain point currently. We're probably gonna lean on post-processing, since it's cleaner than setting a string to '', an array to [], a number to 0 (iffy), etc.

Your suggested solution seems like a good, clean idea though.

@larssn
Copy link
Author

larssn commented Mar 29, 2021

@davidmorgan This feature was stripped from v8 correct? If so, how to handle it? Our requirements remain the same: It is useful to serialize nulls when working with Firestore.

@davidmorgan davidmorgan reopened this Mar 29, 2021
@davidmorgan
Copy link
Collaborator

Yes--but it looks like I could actually add it back--will aim to do so.

@kuhnroyal
Copy link
Contributor

With a global option and a per-field override? #912
I would love to not have to write custom serializers for that :)

@larssn
Copy link
Author

larssn commented Apr 15, 2021

Any workaround until this gets implemented? We have a ton of code that relies on this, from the old version. There's currently a lot of glitches related to saving data, which is to be expected, but not optimal. 😅

@kuhnroyal
Copy link
Contributor

The serializer class can handle null now but all the default serializers don't serialize null.
I am currently writing the serializer manually where this is needed but that is not ideal.

@larssn
Copy link
Author

larssn commented Apr 26, 2021

@davidmorgan Will you accept a PR that re-implements this? I assume the solution is similar to the last one?

@larssn
Copy link
Author

larssn commented Apr 27, 2021

@kuhnroyal Thanks for the suggestion but we have more than 100 built value entities, so writing a custom serializer for most of them is probably gonna take longer than a PR. 😊

@kuhnroyal
Copy link
Contributor

@larssn I agree. I implemented this in the OpenAPI generator by generating it from a mustache template: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/dart/libraries/dio/serialization/built_value/class.mustache#L58

I would also love to have something like @BuiltValueField(serializeNull: true) so I can set this per field and not only global.
See #912

@larssn
Copy link
Author

larssn commented Apr 27, 2021

@kuhnroyal My first priority is just to get it back to how it was. A BuiltValueField has to come later. We have a deadline next monday, and this is a blocker.

@davidmorgan
Copy link
Collaborator

#1049

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