Skip to content

Allow to suppress null values in serialization #51

Open

Description

@Ironlink

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?

Activity

  1. robinjhector commented on Feb 24, 2023

    @robinjhector

    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.

  2. nrayburn-tech commented on Sep 26, 2025

    @nrayburn-tech
    Contributor

    The difference between a property in the JSON and a property not in the JSON can be relevant based on your API design.

    However if you don’t care about that, then you don’t have to use a JsonNullable to begin with. You can just use the regular class and configure Jackson to not include nulls.
    For example, if you’re returning all of this data from something like a read endpoint then it doesn’t typically matter. However, if you’re using this data for a partial update to your entity then it might matter.

    So if I’m understanding the use case correctly, my suggestion is to not use JsonNullable<T> here. Just use T directly.

    (I know the issue is old and you two have likely moved on or solved this somehow, I’m just trying to clean up open issues.)

  3. Ironlink commented on Nov 27, 2025

    @Ironlink
    Author

    We have tens of thousands of lines of OpenAPI models, and rely heavily on JsonNullable to implement partial updates. We are not interested in having JsonNullable in response models, but due to the sheer size of our specs we have chosen to share models between requests and responses. This means that EditPersonRequest and GetPersonResponse both reference the same shared model Person, which uses nullable: true to allow the client to send only the property they want to change in an EditPersonRequest.

    We then have two choices to trim null values from responses, either duplicate lots of spec with nullable: false or litter the server code with conditions to make the fields JsonNullable.undefined().

    I recognise that others may be relying on JsonNullable's current behaviour for output, and do not want to cause breakage. An option could look something like this:

    new JsonNullableModule().allowSuppressNulls(true)

    If this is agreeable, I can provide a PR.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions