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

Allow to suppress null values in serialization #51

Open
Ironlink opened this issue Feb 24, 2023 · 1 comment
Open

Allow to suppress null values in serialization #51

Ironlink opened this issue Feb 24, 2023 · 1 comment

Comments

@Ironlink
Copy link

Ironlink commented Feb 24, 2023

In order to minimize my response body size from my REST controllers, I would like to be able to supress all null values when serializing to JSON, however when I changed my ObjectMapper to use Include.NON_NULL by default I found that suppressNull is hard coded to false in JsonNullableSerializer. This results in considerable bloat.

You might wonder why I don't use JsonNullable.undefined() instead. I have an OpenAPI schema with 679 different fields, of which 174 are nullable. Keeping track of which fields are nullable, and changing all of those converter classes to use JsonNullable is quite a lot of work. On top of that, the Java models generated with openapi-generator-maven-plugin do not create fluent setters for JsonNullable.

It would save me a lot of effort if JsonNullableSerializer would just respect suppressNull, is that something you would agree to change?
If not, I could submit a PR to add a setting to JsonNullableModule to toggle whether suppressNull should be respected?

@robinjhector
Copy link

To give a concrete example, imagine I have a business model such as:

public record Person(String firstName, String lastName, String bio, String title, Country country) {}

I would then map that to my api model such as:

public static PersonApiModel fromBusinessModel(Person person) {
    return new PersonApiModel()
        .firstName(person.firstName())
        .lastName(person.lastName())
        .bio(person.bio())
        .title(person.title())
        .country(person.country());
}

If I for instance allow nulls for bio, title, and country, this would result in the Api model setter being called with explicit null. Which in turn would become a JsonNullable.of(null), and be serialized with literal nulls.

Like @Ironlink said, I have a bunch of these properties, and we would ideally not want to rewrite all of our business->api conversions with the following pattern:

var apiModel = new PersonApiModel()
    .firstName(person.firstName())
    .lastName(person.lastName());
if (person.bio() != null) {
    apiModel.bio(person.bio());
}
if (person.title() != null) {
    apiModel.title(person.title());
}

... etc for every possible field that can be null in java.

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

No branches or pull requests

2 participants