-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Ignore property when value is null or empty #2290
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
Comments
What kind of serialization are you talking about? Spring Data Elasticsearch about? The |
An Object's property that has a null or empty value DOES get sent as EMPTY if the property type is a Map. A simple example is below:
When using Spring-data-elastic to save this Person class, which only has the name set to "foo" (
I'm trying to ONLY serialize the I believe the heart of the issues lies within a |
I have just tested this with the current version: Using this entity: public class Foo {
@Id
private String id;
@Nullable
@Field(type = FieldType.Object)
private Map<String, Object> map1;
@Nullable
@Field(type = FieldType.Object)
private Map<String, Object> map2;
// getter and setter
} and this code: var foo = new Foo();
foo.setId("42");
foo.setMap1(null);
foo.setMap2(new LinkedHashMap<>());
operations.save(foo); the following is sent to Elasticsearch: {"_class":"com.sothawo.springdataelastictest.so.Foo","id":"42","map2":{}} The property with the value The code you show is not the place where a property is written, the check for this is at https://github.com/spring-projects/spring-data-elasticsearch/blob/main/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java#L945-L955 You can use an @Component
public class FooBeforeConvertCallback implements BeforeConvertCallback<Foo> {
@Override
public Foo onBeforeConvert(Foo foo, IndexCoordinates indexCoordinates) {
if (CollectionUtils.isEmpty(foo.getMap1())) {
foo.setMap1(null);
}
if (CollectionUtils.isEmpty(foo.getMap2())) {
foo.setMap2(null);
}
return foo;
}
} |
Perfect example. The initialized, but empty I still think this is a valid issue as this will cause me to have to write a lot of Callback code. Can you not update the I would simple override the |
The question is: which criteria defines that a property is not null, initialized and empty? This would have to work with Collections, but probably with String as well? And it would need an extra configuration on the field, as not writing non-null values is not the default behaviour. For example, Elasticsearch diffferentiates between an empty ("") string and a non-existent string when a Another point to consider: what happens to properties that are not nullable, for example when the application using Spring Data Elasticsearch is written in Kotlin and has a non-null It would be possible to add a property |
As a work-around I've had to do the following, which ONLY sets the value to NULL if it's an empty ElasticsearchConfiguration.java
|
I still think that we need the ability to remove or ignore null or empty properties when serializing. This can be accomplished through @field or any other annotation. |
Due to the migration from Jackson there's currently no way to ignore a property with a null or empty value when serializing. Is it possible to add this attribute ("IgnoreNullOrEmpty") to the Field annotation?
The text was updated successfully, but these errors were encountered: