You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MediaTypeConfigurationCustomizer(s) are not applied on HalConfiguration #2436
spring-hateoas has introduced MediaTypeConfigurationCustomizer, which can be used to configure/modify HalConfiguration and HalFormsConfiguration in an additive way instead of overwriting the whole bean. (see spring-projects/spring-hateoas#2035)
However, these customizers are not applied in spring-data-rest where HalConfiguration or HalFormsConfiguration are used (in RepositoryRestMvcConfiguration), leading to an inconsistency between the configuration as used by spring-hateoas and the one used by spring-data-rest.
I bumped into this because I am using a customizer-based configuration, and its behavior is different when using only spring-hateoas or when having a dependency on spring-data-rest-webmvc (even though it is not being used for this particular endpoint).
I have an example application displaying the issue here: invoice-api.zip.
This shows the issue with spring-data-rest-webmvc.
To see the behavior without spring-data-rest-webmvc, either uncomment the exclude line in build.gradle or remove the whole dependeny on the spring-data-rest starter)
The bug is in RepositoryRestMvcConfiguration. The two methods that build HAL and HAL Forms Jackson mappers — halObjectMapper() and halFormsJacksonHttpMessageConverter() — directly call this.halConfiguration.getIfUnique(...) to obtain a HalConfiguration bean, completely bypassing any registered MediaTypeConfigurationCustomizer<HalConfiguration> or MediaTypeConfigurationCustomizer<HalFormsConfiguration> beans.
Spring HATEOAS's own HalMediaTypeConfiguration and HalFormsMediaTypeConfiguration use a MediaTypeConfigurationFactory that collects all customizers via an ObjectProvider<MediaTypeConfigurationCustomizer<?>> and applies them in order. Spring Data REST does not do this — it just grabs the raw bean.
The two offending methods:
// In RepositoryRestMvcConfiguration.java// 1. halObjectMapper() — ignores HalConfiguration customizerspublicJsonMapperhalObjectMapper(LinkCollectorlinkCollector) {
HalConfigurationhalConfiguration = this.halConfiguration.getIfUnique(HalConfiguration::new); // ← raw bean, no customizers applied// ...
}
// 2. halFormsJacksonHttpMessageConverter() — ignores both HalConfiguration and HalFormsConfiguration customizersTypeConstrainedJacksonJsonHttpMessageConverterhalFormsJacksonHttpMessageConverter(LinkCollectorlinkCollector) {
HalFormsConfigurationconfiguration = newHalFormsConfiguration(
halConfiguration.getIfUnique(() -> newHalConfiguration())); // ← raw bean, no customizers applied// ...
}
The Fix
The fix mirrors exactly what spring-hateoas does in HalMediaTypeConfiguration and HalFormsMediaTypeConfiguration: inject ObjectProvider<MediaTypeConfigurationCustomizer<?>> beans and use MediaTypeConfigurationFactory to apply them.
Step 1 — Add new constructor parameters for the customizer providers:
// Add these two new fieldsprivatefinalObjectProvider<MediaTypeConfigurationCustomizer<HalConfiguration>> halConfigurationCustomizers;
privatefinalObjectProvider<MediaTypeConfigurationCustomizer<HalFormsConfiguration>> halFormsConfigurationCustomizers;
Step 2 — Update the constructor signature and body:
A regression test should be added to RepositoryRestMvConfigurationIntegrationTests verifying that a MediaTypeConfigurationCustomizer<HalConfiguration> bean registered in the application context is actually applied when the HAL message converter is built:
@Test// GH-2436voidappliesHalConfigurationCustomizer() {
AnnotationConfigApplicationContextctx =
newAnnotationConfigApplicationContext(HalCustomizerConfiguration.class);
JacksonJsonHttpMessageConverterconverter = ctx.getBean(
"halJacksonHttpMessageConverter", JacksonJsonHttpMessageConverter.class);
// Verify the customizer was applied — e.g., check the mapper's configuration// (exact assertion depends on what the customizer changes)assertThat(converter).isNotNull();
ctx.close();
}
@Configuration@Import(RepositoryRestMvcConfiguration.class)
staticclassHalCustomizerConfiguration {
@BeanMediaTypeConfigurationCustomizer<HalConfiguration> halCustomizer() {
returnconfig -> config.withRenderSingleLinksFor("supplier", HalConfiguration.RenderSingleLinks.AS_ARRAY);
}
}
Use MediaTypeConfigurationFactory with halConfigurationCustomizers
halFormsJacksonHttpMessageConverter()
Same issue for both HalConfiguration and HalFormsConfiguration
Use two chained MediaTypeConfigurationFactory instances, mirroring HalFormsMediaTypeConfiguration
Constructor
No ObjectProvider for customizers injected
Add two new ObjectProvider<MediaTypeConfigurationCustomizer<...>> parameters
The fix is a straightforward alignment with the pattern already established in spring-hateoas itself — the only reason it was missed is that RepositoryRestMvcConfiguration builds its own Jackson mappers independently rather than delegating to the HypermediaMappingInformation infrastructure that already handles customizers correctly.
I'm happy to submit a PR with the recommended fix above if folks agree this is the correct solution.
Activity
ruthst00 commentedon Sep 18, 2026
Root Cause
The bug is in
RepositoryRestMvcConfiguration. The two methods that build HAL and HAL Forms Jackson mappers —halObjectMapper()andhalFormsJacksonHttpMessageConverter()— directly callthis.halConfiguration.getIfUnique(...)to obtain aHalConfigurationbean, completely bypassing any registeredMediaTypeConfigurationCustomizer<HalConfiguration>orMediaTypeConfigurationCustomizer<HalFormsConfiguration>beans.Spring HATEOAS's own
HalMediaTypeConfigurationandHalFormsMediaTypeConfigurationuse aMediaTypeConfigurationFactorythat collects all customizers via anObjectProvider<MediaTypeConfigurationCustomizer<?>>and applies them in order. Spring Data REST does not do this — it just grabs the raw bean.The two offending methods:
The Fix
The fix mirrors exactly what spring-hateoas does in
HalMediaTypeConfigurationandHalFormsMediaTypeConfiguration: injectObjectProvider<MediaTypeConfigurationCustomizer<?>>beans and useMediaTypeConfigurationFactoryto apply them.Step 1 — Add new constructor parameters for the customizer providers:
Step 2 — Update the constructor signature and body:
Step 3 — Fix
halObjectMapper()to useMediaTypeConfigurationFactory:Step 4 — Fix
halFormsJacksonHttpMessageConverter()to useMediaTypeConfigurationFactoryfor both HAL and HAL Forms:Required Imports to Add
Test to Add
A regression test should be added to
RepositoryRestMvConfigurationIntegrationTestsverifying that aMediaTypeConfigurationCustomizer<HalConfiguration>bean registered in the application context is actually applied when the HAL message converter is built:Summary
halObjectMapper()halConfiguration.getIfUnique()directly — customizers ignoredMediaTypeConfigurationFactorywithhalConfigurationCustomizershalFormsJacksonHttpMessageConverter()HalConfigurationandHalFormsConfigurationMediaTypeConfigurationFactoryinstances, mirroringHalFormsMediaTypeConfigurationObjectProviderfor customizers injectedObjectProvider<MediaTypeConfigurationCustomizer<...>>parametersThe fix is a straightforward alignment with the pattern already established in spring-hateoas itself — the only reason it was missed is that
RepositoryRestMvcConfigurationbuilds its own Jackson mappers independently rather than delegating to theHypermediaMappingInformationinfrastructure that already handles customizers correctly.I'm happy to submit a PR with the recommended fix above if folks agree this is the correct solution.
cc: @odrotbohm