@@ -1893,6 +1893,35 @@ void shouldNotWriteIdPropertyToDocumentSourceIfConfiguredSo() throws JSONExcepti
1893
1893
assertEquals (expected , json , true );
1894
1894
}
1895
1895
1896
+ @ Test // #2290
1897
+ @ DisplayName ("should respect field setting for empty properties" )
1898
+ void shouldRespectFieldSettingForEmptyProperties () throws JSONException {
1899
+ @ Language ("JSON" )
1900
+ var expected = """
1901
+ {
1902
+ "_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$EntityWithPropertiesThatMightBeEmpty",
1903
+ "id": "42",
1904
+ "stringToWriteWhenEmpty": "",
1905
+ "listToWriteWhenEmpty": [],
1906
+ "mapToWriteWhenEmpty": {}
1907
+ }
1908
+ """ ;
1909
+ var entity = new EntityWithPropertiesThatMightBeEmpty ();
1910
+ entity .setId ("42" );
1911
+ entity .setStringToWriteWhenEmpty ("" );
1912
+ entity .setStringToNotWriteWhenEmpty ("" );
1913
+ entity .setListToWriteWhenEmpty (emptyList ());
1914
+ entity .setListToNotWriteWhenEmpty (emptyList ());
1915
+ entity .setMapToWriteWhenEmpty (emptyMap ());
1916
+ entity .setMapToNotWriteWhenEmpty (emptyMap ());
1917
+
1918
+ Document document = Document .create ();
1919
+ mappingElasticsearchConverter .write (entity , document );
1920
+ String json = document .toJson ();
1921
+
1922
+ assertEquals (expected , json , true );
1923
+ }
1924
+
1896
1925
// region entities
1897
1926
public static class Sample {
1898
1927
@ Nullable public @ ReadOnlyProperty String readOnly ;
@@ -2974,6 +3003,91 @@ public void setText(@Nullable String text) {
2974
3003
this .text = text ;
2975
3004
}
2976
3005
}
3006
+
3007
+ static class EntityWithPropertiesThatMightBeEmpty {
3008
+ @ Nullable private String id ;
3009
+
3010
+ @ Field (type = FieldType .Text )
3011
+ @ Nullable private String stringToWriteWhenEmpty ;
3012
+
3013
+ @ Field (type = FieldType .Text , storeEmptyValue = false )
3014
+ @ Nullable private String stringToNotWriteWhenEmpty ;
3015
+
3016
+ @ Field (type = FieldType .Nested )
3017
+ @ Nullable private List <String > listToWriteWhenEmpty ;
3018
+
3019
+ @ Field (type = FieldType .Nested , storeEmptyValue = false )
3020
+ @ Nullable private List <String > listToNotWriteWhenEmpty ;
3021
+
3022
+ @ Field (type = FieldType .Nested )
3023
+ @ Nullable private Map <String , String > mapToWriteWhenEmpty ;
3024
+
3025
+ @ Field (type = FieldType .Nested , storeEmptyValue = false )
3026
+ @ Nullable private Map <String , String > mapToNotWriteWhenEmpty ;
3027
+
3028
+ @ Nullable
3029
+ public String getId () {
3030
+ return id ;
3031
+ }
3032
+
3033
+ public void setId (@ Nullable String id ) {
3034
+ this .id = id ;
3035
+ }
3036
+
3037
+ @ Nullable
3038
+ public String getStringToWriteWhenEmpty () {
3039
+ return stringToWriteWhenEmpty ;
3040
+ }
3041
+
3042
+ public void setStringToWriteWhenEmpty (@ Nullable String stringToWriteWhenEmpty ) {
3043
+ this .stringToWriteWhenEmpty = stringToWriteWhenEmpty ;
3044
+ }
3045
+
3046
+ @ Nullable
3047
+ public String getStringToNotWriteWhenEmpty () {
3048
+ return stringToNotWriteWhenEmpty ;
3049
+ }
3050
+
3051
+ public void setStringToNotWriteWhenEmpty (@ Nullable String stringToNotWriteWhenEmpty ) {
3052
+ this .stringToNotWriteWhenEmpty = stringToNotWriteWhenEmpty ;
3053
+ }
3054
+
3055
+ @ Nullable
3056
+ public List <String > getListToWriteWhenEmpty () {
3057
+ return listToWriteWhenEmpty ;
3058
+ }
3059
+
3060
+ public void setListToWriteWhenEmpty (@ Nullable List <String > listToWriteWhenEmpty ) {
3061
+ this .listToWriteWhenEmpty = listToWriteWhenEmpty ;
3062
+ }
3063
+
3064
+ @ Nullable
3065
+ public List <String > getListToNotWriteWhenEmpty () {
3066
+ return listToNotWriteWhenEmpty ;
3067
+ }
3068
+
3069
+ public void setListToNotWriteWhenEmpty (@ Nullable List <String > listToNotWriteWhenEmpty ) {
3070
+ this .listToNotWriteWhenEmpty = listToNotWriteWhenEmpty ;
3071
+ }
3072
+
3073
+ @ Nullable
3074
+ public Map <String , String > getMapToWriteWhenEmpty () {
3075
+ return mapToWriteWhenEmpty ;
3076
+ }
3077
+
3078
+ public void setMapToWriteWhenEmpty (@ Nullable Map <String , String > mapToWriteWhenEmpty ) {
3079
+ this .mapToWriteWhenEmpty = mapToWriteWhenEmpty ;
3080
+ }
3081
+
3082
+ @ Nullable
3083
+ public Map <String , String > getMapToNotWriteWhenEmpty () {
3084
+ return mapToNotWriteWhenEmpty ;
3085
+ }
3086
+
3087
+ public void setMapToNotWriteWhenEmpty (@ Nullable Map <String , String > mapToNotWriteWhenEmpty ) {
3088
+ this .mapToNotWriteWhenEmpty = mapToNotWriteWhenEmpty ;
3089
+ }
3090
+ }
2977
3091
// endregion
2978
3092
2979
3093
private static String reverse (Object o ) {
0 commit comments