Skip to content

Don't try to write non-writeable properties. #2249

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

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedOb
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();

// Only deal with text because ES generated Ids are strings!
if (indexedObjectInformation.getId() != null && idProperty != null
if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable()
&& idProperty.getType().isAssignableFrom(String.class)) {
propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedOb
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();

// Only deal with text because ES generated Ids are strings!
if (indexedObjectInformation.getId() != null && idProperty != null
if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable()
&& idProperty.getType().isAssignableFrom(String.class)) {
propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private <R> R readEntity(ElasticsearchPersistentEntity<?> entity, Map<String, Ob
PersistentPropertyAccessor<R> propertyAccessor = new ConvertingPropertyAccessor<>(
targetEntity.getPropertyAccessor(result), conversionService);
// Only deal with String because ES generated Ids are strings !
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
if (idProperty != null && idProperty.isWritable() && idProperty.getType().isAssignableFrom(String.class)) {
propertyAccessor.setProperty(idProperty, document.getId());
}
}
Expand Down Expand Up @@ -406,7 +406,7 @@ protected <R> R readProperties(ElasticsearchPersistentEntity<?> entity, R instan

for (ElasticsearchPersistentProperty prop : entity) {

if (entity.isCreatorArgument(prop) || !prop.isReadable()) {
if (entity.isCreatorArgument(prop) || !prop.isReadable() || !prop.isWritable()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Version;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -3705,6 +3707,21 @@ public int hashCode() {
}
}

@Test // #2230
@DisplayName("should work with readonly id")
void shouldWorkWithReadonlyId() {

ReadonlyIdEntity entity = new ReadonlyIdEntity();
entity.setPart1("foo");
entity.setPart2("bar");
operations.save(entity);

ReadonlyIdEntity readEntity = operations.get(entity.getId(), ReadonlyIdEntity.class);

assertThat(readEntity.getPart1()).isEqualTo(entity.getPart1());
assertThat(readEntity.getPart2()).isEqualTo(entity.getPart2());
}

@Document(indexName = "#{@indexNameProvider.indexName()}")
private static class SampleEntityUUIDKeyed {
@Nullable
Expand Down Expand Up @@ -4450,5 +4467,36 @@ public String toString() {
+ '}';
}
}

@Document(indexName = "#{@indexNameProvider.indexName()}-readonly-id")
static class ReadonlyIdEntity {
@Field(type = FieldType.Keyword) private String part1;

@Field(type = FieldType.Keyword) private String part2;

@Id
@ReadOnlyProperty
@AccessType(AccessType.Type.PROPERTY)
public String getId() {
return part1 + '-' + part2;
}

public String getPart1() {
return part1;
}

public void setPart1(String part1) {
this.part1 = part1;
}

public String getPart2() {
return part2;
}

public void setPart2(String part2) {
this.part2 = part2;
}
}

// endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Version;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -1161,6 +1163,23 @@ void shouldReturnMonoOfReactiveSearchHits() {
.verifyComplete();
}

@Test // #2230
@DisplayName("should work with readonly id")
void shouldWorkWithReadonlyId() {

ReadonlyIdEntity entity = new ReadonlyIdEntity();
entity.setPart1("foo");
entity.setPart2("bar");

operations.save(entity).block();

operations.get(entity.getId(), ReadonlyIdEntity.class) //
.as(StepVerifier::create) //
.assertNext(readEntity -> { //
assertThat(readEntity.getPart1()).isEqualTo(entity.getPart1()); //
assertThat(readEntity.getPart2()).isEqualTo(entity.getPart2()); //
}).verifyComplete();
}
// endregion

// region Helper functions
Expand Down Expand Up @@ -1494,5 +1513,35 @@ public String toString() {
+ seqNoPrimaryTerm + '}';
}
}

@Document(indexName = "#{@indexNameProvider.indexName()}-readonly-id")
static class ReadonlyIdEntity {
@Field(type = FieldType.Keyword) private String part1;

@Field(type = FieldType.Keyword) private String part2;

@Id
@ReadOnlyProperty
@AccessType(AccessType.Type.PROPERTY)
public String getId() {
return part1 + '-' + part2;
}

public String getPart1() {
return part1;
}

public void setPart1(String part1) {
this.part1 = part1;
}

public String getPart2() {
return part2;
}

public void setPart2(String part2) {
this.part2 = part2;
}
}
// endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ static class SampleEntity {
@Id private String id;
@Nullable private String text;

@ReadOnlyProperty
// @ReadOnlyProperty
@Nullable private String className;

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ReactiveIndexOperations;
Expand Down Expand Up @@ -119,7 +118,6 @@ static class SampleEntity {
@Id private String id;
private String text;

@ReadOnlyProperty
@Nullable private String className;

public SampleEntity(String id, String text) {
Expand Down