Skip to content

Commit dab0be0

Browse files
authored
Fix store null values implementation.
Original Pull Request #2842 (cherry picked from commit 0a1e205)
1 parent e8f269d commit dab0be0

File tree

3 files changed

+158
-4
lines changed

3 files changed

+158
-4
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchConfiguration.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
3232
import org.springframework.util.Assert;
3333

34+
import com.fasterxml.jackson.annotation.JsonInclude;
35+
import com.fasterxml.jackson.databind.ObjectMapper;
36+
import com.fasterxml.jackson.databind.SerializationFeature;
37+
3438
/**
3539
* Base class for a @{@link org.springframework.context.annotation.Configuration} class to set up the Elasticsearch
3640
* connection using the Elasticsearch Client. This class exposes different parts of the setup as Spring beans. Deriving
@@ -118,7 +122,13 @@ public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter el
118122
*/
119123
@Bean
120124
public JsonpMapper jsonpMapper() {
121-
return new JacksonJsonpMapper();
125+
// we need to create our own objectMapper that keeps null values in order to provide the storeNullValue
126+
// functionality. The one Elasticsearch would provide removes the nulls. We remove unwanted nulls before they get
127+
// into this mapper, so we can safely keep them here.
128+
var objectMapper = (new ObjectMapper())
129+
.configure(SerializationFeature.INDENT_OUTPUT, false)
130+
.setSerializationInclusion(JsonInclude.Include.ALWAYS);
131+
return new JacksonJsonpMapper(objectMapper);
122132
}
123133

124134
/**

src/test/java/org/springframework/data/elasticsearch/client/RestClientsTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1919
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
20-
import static io.specto.hoverfly.junit.dsl.HoverflyDsl.service;
21-
import static io.specto.hoverfly.junit.verification.HoverflyVerifications.atLeast;
22-
import static org.assertj.core.api.Assertions.assertThat;
20+
import static io.specto.hoverfly.junit.dsl.HoverflyDsl.*;
21+
import static io.specto.hoverfly.junit.verification.HoverflyVerifications.*;
22+
import static org.assertj.core.api.Assertions.*;
2323

2424
import co.elastic.clients.elasticsearch.ElasticsearchClient;
2525
import co.elastic.clients.transport.endpoints.BooleanResponse;
@@ -51,6 +51,10 @@
5151
/**
5252
* We need hoverfly for testing the reactive code to use a proxy. Wiremock cannot intercept the proxy calls as WebClient
5353
* uses HTTP CONNECT on proxy requests which wiremock does not support.
54+
* <br/>
55+
* Note: since 5.0 we do not use the WebClient for
56+
* the reactive code anymore, so this might be handled with two wiremocks, but there is no real need to change this test
57+
* setup.
5458
*
5559
* @author Peter-Josef Meisch
5660
*/
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.client.elc;
17+
18+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
19+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.*;
20+
21+
import org.junit.jupiter.api.DisplayName;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.junit.jupiter.api.extension.RegisterExtension;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.data.annotation.Id;
28+
import org.springframework.data.elasticsearch.annotations.Document;
29+
import org.springframework.data.elasticsearch.annotations.Field;
30+
import org.springframework.data.elasticsearch.client.ClientConfiguration;
31+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
32+
import org.springframework.lang.Nullable;
33+
import org.springframework.test.context.junit.jupiter.SpringExtension;
34+
35+
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
36+
37+
/**
38+
* Tests that need to check the data produced by the Elasticsearch client
39+
* @author Peter-Josef Meisch
40+
*/
41+
@ExtendWith(SpringExtension.class)
42+
public class ELCWiremockTests {
43+
44+
@RegisterExtension static WireMockExtension wireMock = WireMockExtension.newInstance()
45+
.options(wireMockConfig()
46+
.dynamicPort()
47+
// needed, otherwise Wiremock goes to test/resources/mappings
48+
.usingFilesUnderDirectory("src/test/resources/wiremock-mappings"))
49+
.build();
50+
51+
@Configuration
52+
static class Config extends ElasticsearchConfiguration {
53+
@Override
54+
public ClientConfiguration clientConfiguration() {
55+
return ClientConfiguration.builder()
56+
.connectedTo("localhost:" + wireMock.getPort())
57+
.build();
58+
}
59+
}
60+
61+
@Autowired ElasticsearchOperations operations;
62+
63+
@Test // #2839
64+
@DisplayName("should store null values if configured")
65+
void shouldStoreNullValuesIfConfigured() {
66+
67+
wireMock.stubFor(put(urlPathEqualTo("/null-fields/_doc/42"))
68+
.withRequestBody(equalToJson("""
69+
{
70+
"_class": "org.springframework.data.elasticsearch.client.elc.ELCWiremockTests$EntityWithNullFields",
71+
"id": "42",
72+
"field1": null
73+
}
74+
"""))
75+
.willReturn(
76+
aResponse()
77+
.withStatus(200)
78+
.withHeader("X-elastic-product", "Elasticsearch")
79+
.withHeader("content-type", "application/vnd.elasticsearch+json;compatible-with=8")
80+
.withBody("""
81+
{
82+
"_index": "null-fields",
83+
"_id": "42",
84+
"_version": 1,
85+
"result": "created",
86+
"forced_refresh": true,
87+
"_shards": {
88+
"total": 2,
89+
"successful": 1,
90+
"failed": 0
91+
},
92+
"_seq_no": 1,
93+
"_primary_term": 1
94+
}
95+
""")));
96+
97+
var entity = new EntityWithNullFields();
98+
entity.setId("42");
99+
100+
operations.save(entity);
101+
// no need to assert anything, if the field1:null is not sent, we run into a 404 error
102+
}
103+
104+
@Document(indexName = "null-fields")
105+
static class EntityWithNullFields {
106+
@Nullable
107+
@Id private String id;
108+
@Nullable
109+
@Field(storeNullValue = true) private String field1;
110+
@Nullable
111+
@Field private String field2;
112+
113+
@Nullable
114+
public String getId() {
115+
return id;
116+
}
117+
118+
public void setId(@Nullable String id) {
119+
this.id = id;
120+
}
121+
122+
@Nullable
123+
public String getField1() {
124+
return field1;
125+
}
126+
127+
public void setField1(@Nullable String field1) {
128+
this.field1 = field1;
129+
}
130+
131+
@Nullable
132+
public String getField2() {
133+
return field2;
134+
}
135+
136+
public void setField2(@Nullable String field2) {
137+
this.field2 = field2;
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)