Skip to content

Commit 0c24113

Browse files
committed
Dedicated customization callback for AuditableBeanWrapperFactory.
Introduced RepositoryRestConfigurer.customizeAuditableBeanWrapperFactory(…) so that implementations can customize the default instance provided by RepositoryRestMvcConfiguration. Fixes #2040.
1 parent f310a4c commit 0c24113

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurer.java

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.function.Consumer;
2121

2222
import org.springframework.core.convert.support.ConfigurableConversionService;
23+
import org.springframework.data.auditing.AuditableBeanWrapperFactory;
2324
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
2425
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
2526
import org.springframework.http.converter.HttpMessageConverter;
@@ -132,4 +133,15 @@ default void configureHttpMessageConverters(List<HttpMessageConverter<?>> messag
132133
* @param objectMapper The {@literal ObjectMapper} to be used by the system.
133134
*/
134135
default void configureJacksonObjectMapper(ObjectMapper objectMapper) {}
136+
137+
/**
138+
* Customize the {@link AuditableBeanWrapperFactory} to be used.
139+
*
140+
* @param factory will never be {@literal null}.
141+
* @return must not be {@literal null}.
142+
* @since 3.5
143+
*/
144+
default AuditableBeanWrapperFactory customizeAuditableBeanWrapperFactory(AuditableBeanWrapperFactory factory) {
145+
return factory;
146+
}
135147
}

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurerDelegate.java

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919

2020
import org.springframework.core.convert.support.ConfigurableConversionService;
21+
import org.springframework.data.auditing.AuditableBeanWrapperFactory;
2122
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
2223
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
2324
import org.springframework.http.converter.HttpMessageConverter;
@@ -121,4 +122,18 @@ public void configureValidatingRepositoryEventListener(ValidatingRepositoryEvent
121122
configurer.configureValidatingRepositoryEventListener(validatingListener);
122123
}
123124
}
125+
126+
/*
127+
* (non-Javadoc)
128+
* @see org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer#configureAuditableBeanWrapperFactory(org.springframework.data.auditing.AuditableBeanWrapperFactory)
129+
*/
130+
@Override
131+
public AuditableBeanWrapperFactory customizeAuditableBeanWrapperFactory(AuditableBeanWrapperFactory factory) {
132+
133+
for (RepositoryRestConfigurer configurer : delegates) {
134+
factory = configurer.customizeAuditableBeanWrapperFactory(factory);
135+
}
136+
137+
return factory;
138+
}
124139
}

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
169169
ObjectProvider<RepresentationModelProcessorInvoker> invoker;
170170
ObjectProvider<MessageResolver> resolver;
171171
ObjectProvider<GeoModule> geoModule;
172+
ObjectProvider<AuditableBeanWrapperFactory> auditingBeanWrapperFactoryProvider;
172173

173174
ConversionService defaultConversionService;
174175

@@ -855,7 +856,10 @@ public PluginRegistry<BackendIdConverter, Class<?>> backendIdConverterRegistry()
855856

856857
@Bean
857858
public AuditableBeanWrapperFactory auditableBeanWrapperFactory(PersistentEntities persistentEntities) {
858-
return new MappingAuditableBeanWrapperFactory(persistentEntities);
859+
860+
AuditableBeanWrapperFactory factory = new MappingAuditableBeanWrapperFactory(persistentEntities);
861+
862+
return configurerDelegate.get().customizeAuditableBeanWrapperFactory(factory);
859863
}
860864

861865
@Bean

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java

+25
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.rest.webmvc.config;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
1920
import static org.springframework.data.rest.webmvc.util.AssertionUtils.*;
2021

2122
import java.util.Collection;
@@ -37,6 +38,7 @@
3738
import org.springframework.context.support.AbstractApplicationContext;
3839
import org.springframework.core.annotation.Order;
3940
import org.springframework.core.convert.ConversionService;
41+
import org.springframework.data.auditing.AuditableBeanWrapperFactory;
4042
import org.springframework.data.domain.PageRequest;
4143
import org.springframework.data.domain.Sort.Direction;
4244
import org.springframework.data.geo.Distance;
@@ -211,6 +213,14 @@ public void registersRepositoryRestConfigurersInDeclaredOrder() {
211213
.containsSequence(userConfig.otherConfigurer(), userConfig.configurer());
212214
}
213215

216+
@Test // #2040
217+
public void appliesAuditingBeanWrapperFactoryCustomizer() {
218+
219+
AuditableBeanWrapperFactory factory = context.getBean(AuditableBeanWrapperFactory.class);
220+
221+
assertThat(factory).isEqualTo(ExtendingConfiguration.auditableBeanWrapperFactory);
222+
}
223+
214224
private static ObjectMapper getObjectMapper() {
215225

216226
AbstractJackson2HttpMessageConverter converter = context.getBean("halJacksonHttpMessageConverter",
@@ -222,6 +232,8 @@ private static ObjectMapper getObjectMapper() {
222232
@Import(RepositoryRestMvcConfiguration.class)
223233
static class ExtendingConfiguration {
224234

235+
static AuditableBeanWrapperFactory auditableBeanWrapperFactory = mock(AuditableBeanWrapperFactory.class);
236+
225237
@Bean
226238
DefaultLinkRelationProvider relProvider() {
227239
return new DefaultLinkRelationProvider();
@@ -251,6 +263,19 @@ RepositoryRestConfigurer configurer() {
251263
RepositoryRestConfigurer otherConfigurer() {
252264
return RepositoryRestConfigurer.withConfig(__ -> {});
253265
}
266+
267+
@Bean
268+
@Order(300) // #2040
269+
RepositoryRestConfigurer auditingBeanWrapperCustomizer() {
270+
271+
return new RepositoryRestConfigurer() {
272+
273+
@Override
274+
public AuditableBeanWrapperFactory customizeAuditableBeanWrapperFactory(AuditableBeanWrapperFactory factory) {
275+
return auditableBeanWrapperFactory;
276+
}
277+
};
278+
}
254279
}
255280

256281
@Configuration

0 commit comments

Comments
 (0)