Skip to content

Commit 71a4266

Browse files
odrotbohmchristophstrobl
authored andcommitted
DATACMNS-1609, DATACMNS-1461 - Handle null intermediates in setting auditing property paths.
We now catch the MappingException produced by trying to set auditing property paths containing null intermediate segments and ignore those. A less expensive (non-Exception-based) approach is going to be introduced for Moore as it requires API changes to the property path setting APIs. Related tickets: DATACMNS-1438.
1 parent f7398f4 commit 71a4266

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/main/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactory.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.data.annotation.LastModifiedBy;
2828
import org.springframework.data.annotation.LastModifiedDate;
2929
import org.springframework.data.domain.Auditable;
30+
import org.springframework.data.mapping.MappingException;
3031
import org.springframework.data.mapping.PersistentEntity;
3132
import org.springframework.data.mapping.PersistentProperty;
3233
import org.springframework.data.mapping.PersistentPropertyAccessor;
@@ -174,10 +175,7 @@ public MappingMetadataAuditableBeanWrapper(PersistentPropertyAccessor<T> accesso
174175
*/
175176
@Override
176177
public Object setCreatedBy(Object value) {
177-
178-
metadata.createdByPaths.forEach(it -> this.accessor.setProperty(it, value));
179-
180-
return value;
178+
return setProperty(metadata.createdByPaths, value);
181179
}
182180

183181
/*
@@ -232,7 +230,20 @@ public T getBean() {
232230
private <S, P extends PersistentProperty<?>> S setProperty(
233231
PersistentPropertyPaths<?, ? extends PersistentProperty<?>> paths, S value) {
234232

235-
paths.forEach(it -> this.accessor.setProperty(it, value));
233+
paths.forEach(it -> {
234+
235+
try {
236+
237+
this.accessor.setProperty(it, value);
238+
239+
} catch (MappingException o_O) {
240+
241+
// Ignore null intermediate errors temporarily
242+
if (!o_O.getMessage().contains("on null intermediate")) {
243+
throw o_O;
244+
}
245+
}
246+
});
236247

237248
return value;
238249
}

src/test/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactoryUnitTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ public void writesNestedAuditingData() {
212212
});
213213
}
214214

215+
@Test // DATACMNS-1461
216+
public void skipsNullIntermediatesWhenSettingProperties() {
217+
218+
WithEmbedded withEmbedded = new WithEmbedded();
219+
220+
assertThat(factory.getBeanWrapperFor(withEmbedded)).hasValueSatisfying(it -> {
221+
assertThatCode(() -> it.setCreatedBy("user")).doesNotThrowAnyException();
222+
});
223+
}
224+
215225
private void assertLastModificationDate(Object source, TemporalAccessor expected) {
216226

217227
Sample sample = new Sample();

0 commit comments

Comments
 (0)