You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow to suppress null values in serialization #51
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?
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:
varapiModel = newPersonApiModel()
.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.
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.)
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:
Activity
robinjhector commentedon Feb 24, 2023
To give a concrete example, imagine I have a business model such as:
I would then map that to my api model such as:
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:
... etc for every possible field that can be null in java.
nrayburn-tech commentedon Sep 26, 2025
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 useTdirectly.(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.)
Ironlink commentedon Nov 27, 2025
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
EditPersonRequestandGetPersonResponseboth reference the same shared modelPerson, which usesnullable: trueto allow the client to send only the property they want to change in anEditPersonRequest.We then have two choices to trim null values from responses, either duplicate lots of spec with
nullable: falseor litter the server code with conditions to make the fieldsJsonNullable.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:
If this is agreeable, I can provide a PR.