Skip to content

Commit 3c6d96e

Browse files
committed
Don't try to write non-writeable properties.
Original Pull Request #2249 Closes #2230 (cherry picked from commit acf02a1) # Conflicts: # src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java
1 parent be70a39 commit 3c6d96e

File tree

7 files changed

+200
-74
lines changed

7 files changed

+200
-74
lines changed

src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedOb
478478
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
479479

480480
// Only deal with text because ES generated Ids are strings!
481-
if (indexedObjectInformation.getId() != null && idProperty != null
481+
if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable()
482482
&& idProperty.getType().isAssignableFrom(String.class)) {
483483
propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
484484
}

src/main/java/org/springframework/data/elasticsearch/core/AbstractReactiveElasticsearchTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedOb
259259
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
260260

261261
// Only deal with text because ES generated Ids are strings!
262-
if (indexedObjectInformation.getId() != null && idProperty != null
262+
if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable()
263263
&& idProperty.getType().isAssignableFrom(String.class)) {
264264
propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
265265
}

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

+100-69
Large diffs are not rendered by default.

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchIntegrationTests.java

+48
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
import org.springframework.dao.DataAccessException;
4646
import org.springframework.dao.InvalidDataAccessApiUsageException;
4747
import org.springframework.dao.OptimisticLockingFailureException;
48+
import org.springframework.data.annotation.AccessType;
4849
import org.springframework.data.annotation.Id;
50+
import org.springframework.data.annotation.ReadOnlyProperty;
4951
import org.springframework.data.annotation.Version;
5052
import org.springframework.data.domain.PageRequest;
5153
import org.springframework.data.domain.Pageable;
@@ -3718,6 +3720,21 @@ public int hashCode() {
37183720
}
37193721
}
37203722

3723+
@Test // #2230
3724+
@DisplayName("should work with readonly id")
3725+
void shouldWorkWithReadonlyId() {
3726+
3727+
ReadonlyIdEntity entity = new ReadonlyIdEntity();
3728+
entity.setPart1("foo");
3729+
entity.setPart2("bar");
3730+
operations.save(entity);
3731+
3732+
ReadonlyIdEntity readEntity = operations.get(entity.getId(), ReadonlyIdEntity.class);
3733+
3734+
assertThat(readEntity.getPart1()).isEqualTo(entity.getPart1());
3735+
assertThat(readEntity.getPart2()).isEqualTo(entity.getPart2());
3736+
}
3737+
37213738
@Document(indexName = "#{@indexNameProvider.indexName()}")
37223739
private static class SampleEntityUUIDKeyed {
37233740
@Nullable
@@ -4463,5 +4480,36 @@ public String toString() {
44634480
+ '}';
44644481
}
44654482
}
4483+
4484+
@Document(indexName = "#{@indexNameProvider.indexName()}-readonly-id")
4485+
static class ReadonlyIdEntity {
4486+
@Field(type = FieldType.Keyword) private String part1;
4487+
4488+
@Field(type = FieldType.Keyword) private String part2;
4489+
4490+
@Id
4491+
@ReadOnlyProperty
4492+
@AccessType(AccessType.Type.PROPERTY)
4493+
public String getId() {
4494+
return part1 + '-' + part2;
4495+
}
4496+
4497+
public String getPart1() {
4498+
return part1;
4499+
}
4500+
4501+
public void setPart1(String part1) {
4502+
this.part1 = part1;
4503+
}
4504+
4505+
public String getPart2() {
4506+
return part2;
4507+
}
4508+
4509+
public void setPart2(String part2) {
4510+
this.part2 = part2;
4511+
}
4512+
}
4513+
44664514
// endregion
44674515
}

src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchIntegrationTests.java

+49
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
import org.springframework.beans.factory.annotation.Autowired;
5050
import org.springframework.dao.DataAccessException;
5151
import org.springframework.dao.OptimisticLockingFailureException;
52+
import org.springframework.data.annotation.AccessType;
5253
import org.springframework.data.annotation.Id;
54+
import org.springframework.data.annotation.ReadOnlyProperty;
5355
import org.springframework.data.annotation.Version;
5456
import org.springframework.data.domain.PageRequest;
5557
import org.springframework.data.domain.Pageable;
@@ -1156,6 +1158,23 @@ void shouldReturnMonoOfReactiveSearchHits() {
11561158
.verifyComplete();
11571159
}
11581160

1161+
@Test // #2230
1162+
@DisplayName("should work with readonly id")
1163+
void shouldWorkWithReadonlyId() {
1164+
1165+
ReadonlyIdEntity entity = new ReadonlyIdEntity();
1166+
entity.setPart1("foo");
1167+
entity.setPart2("bar");
1168+
1169+
operations.save(entity).block();
1170+
1171+
operations.get(entity.getId(), ReadonlyIdEntity.class) //
1172+
.as(StepVerifier::create) //
1173+
.assertNext(readEntity -> { //
1174+
assertThat(readEntity.getPart1()).isEqualTo(entity.getPart1()); //
1175+
assertThat(readEntity.getPart2()).isEqualTo(entity.getPart2()); //
1176+
}).verifyComplete();
1177+
}
11591178
// endregion
11601179

11611180
// region Helper functions
@@ -1489,5 +1508,35 @@ public String toString() {
14891508
+ seqNoPrimaryTerm + '}';
14901509
}
14911510
}
1511+
1512+
@Document(indexName = "#{@indexNameProvider.indexName()}-readonly-id")
1513+
static class ReadonlyIdEntity {
1514+
@Field(type = FieldType.Keyword) private String part1;
1515+
1516+
@Field(type = FieldType.Keyword) private String part2;
1517+
1518+
@Id
1519+
@ReadOnlyProperty
1520+
@AccessType(AccessType.Type.PROPERTY)
1521+
public String getId() {
1522+
return part1 + '-' + part2;
1523+
}
1524+
1525+
public String getPart1() {
1526+
return part1;
1527+
}
1528+
1529+
public void setPart1(String part1) {
1530+
this.part1 = part1;
1531+
}
1532+
1533+
public String getPart2() {
1534+
return part2;
1535+
}
1536+
1537+
public void setPart2(String part2) {
1538+
this.part2 = part2;
1539+
}
1540+
}
14921541
// endregion
14931542
}

src/test/java/org/springframework/data/elasticsearch/core/event/CallbackIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static class SampleEntity {
241241
@Id private String id;
242242
@Nullable private String text;
243243

244-
@ReadOnlyProperty
244+
// @ReadOnlyProperty
245245
@Nullable private String className;
246246

247247
@Nullable

src/test/java/org/springframework/data/elasticsearch/core/event/ReactiveCallbackIntegrationTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.beans.factory.annotation.Autowired;
2828
import org.springframework.context.annotation.Configuration;
2929
import org.springframework.data.annotation.Id;
30-
import org.springframework.data.annotation.ReadOnlyProperty;
3130
import org.springframework.data.elasticsearch.annotations.Document;
3231
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
3332
import org.springframework.data.elasticsearch.core.ReactiveIndexOperations;
@@ -119,7 +118,6 @@ static class SampleEntity {
119118
@Id private String id;
120119
private String text;
121120

122-
@ReadOnlyProperty
123121
@Nullable private String className;
124122

125123
public SampleEntity(String id, String text) {

0 commit comments

Comments
 (0)