@@ -1871,6 +1871,28 @@ private Map<String, Object> writeToMap(Object source) {
1871
1871
return sink ;
1872
1872
}
1873
1873
1874
+ @ Test // #2364
1875
+ @ DisplayName ("should not write id property to document source if configured so" )
1876
+ void shouldNotWriteIdPropertyToDocumentSourceIfConfiguredSo () throws JSONException {
1877
+
1878
+ @ Language ("JSON" )
1879
+ var expected = """
1880
+ {
1881
+ "_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$DontWriteIdToSourceEntity",
1882
+ "text": "some text"
1883
+ }
1884
+ """ ;
1885
+ var entity = new DontWriteIdToSourceEntity ();
1886
+ entity .setId ("42" );
1887
+ entity .setText ("some text" );
1888
+
1889
+ Document document = Document .create ();
1890
+ mappingElasticsearchConverter .write (entity , document );
1891
+ String json = document .toJson ();
1892
+
1893
+ assertEquals (expected , json , true );
1894
+ }
1895
+
1874
1896
// region entities
1875
1897
public static class Sample {
1876
1898
@ Nullable public @ ReadOnlyProperty String readOnly ;
@@ -2885,7 +2907,7 @@ private static final class ImmutableEntityWithCollections {
2885
2907
@ Nullable private Set <Child > childrenSet ;
2886
2908
2887
2909
public ImmutableEntityWithCollections (@ Nullable List <String > stringList , @ Nullable Set <String > stringSet ,
2888
- @ Nullable List <Child > childrenList , @ Nullable Set <Child > childrenSet ) {
2910
+ @ Nullable List <Child > childrenList , @ Nullable Set <Child > childrenSet ) {
2889
2911
this .stringList = stringList ;
2890
2912
this .stringSet = stringSet ;
2891
2913
this .childrenList = childrenList ;
@@ -2927,6 +2949,31 @@ public String getName() {
2927
2949
}
2928
2950
}
2929
2951
}
2952
+
2953
+ @ org .springframework .data .elasticsearch .annotations .Document (indexName = "doesnt-matter" , storeIdInSource = false )
2954
+ static class DontWriteIdToSourceEntity {
2955
+ @ Nullable private String id ;
2956
+ @ Nullable
2957
+ @ Field (type = FieldType .Text ) private String text ;
2958
+
2959
+ @ Nullable
2960
+ public String getId () {
2961
+ return id ;
2962
+ }
2963
+
2964
+ public void setId (@ Nullable String id ) {
2965
+ this .id = id ;
2966
+ }
2967
+
2968
+ @ Nullable
2969
+ public String getText () {
2970
+ return text ;
2971
+ }
2972
+
2973
+ public void setText (@ Nullable String text ) {
2974
+ this .text = text ;
2975
+ }
2976
+ }
2930
2977
// endregion
2931
2978
2932
2979
private static String reverse (Object o ) {
0 commit comments