Skip to content

MediaTypeConfigurationCustomizer(s) are not applied on HalConfiguration #2436

Open

Description

@vierbergenlars

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).

	@Bean
	MediaTypeConfigurationCustomizer<HalConfiguration> halConfigurationCustomizer() {
		return halConfiguration -> halConfiguration
				.withRenderSingleLinksFor("supplier", RenderSingleLinks.AS_ARRAY);
	}

Without spring-data-rest-webmvc, the response looks like expected (the supplier link is an array, even though only one link item is present)

{
    "id":null,
    "received":null,
    "supplier":null,
    "_version":0,
    "pay_before":null,
    "total_amount":null,
    "_links":{
        "supplier":[
            {"href":"test"}
        ]
    }
}

When adding a dependency on spring-data-rest-webmvc, the 'render single links' customization seems to get ignored, falling back to the default:

{
  "received" : null,
  "pay_before" : null,
  "total_amount" : null,
  "_links" : {
    "supplier" : {
      "href" : "test"
    }
  }
}

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)

Activity

  1. ruthst00 commented on Sep 18, 2026

    @ruthst00
    Contributor

    Root Cause

    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 customizers
    public JsonMapper halObjectMapper(LinkCollector linkCollector) {
        HalConfiguration halConfiguration = this.halConfiguration.getIfUnique(HalConfiguration::new); // ← raw bean, no customizers applied
        // ...
    }
    
    // 2. halFormsJacksonHttpMessageConverter() — ignores both HalConfiguration and HalFormsConfiguration customizers
    TypeConstrainedJacksonJsonHttpMessageConverter halFormsJacksonHttpMessageConverter(LinkCollector linkCollector) {
        HalFormsConfiguration configuration = new HalFormsConfiguration(
                halConfiguration.getIfUnique(() -> new HalConfiguration())); // ← 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 fields
    private final ObjectProvider<MediaTypeConfigurationCustomizer<HalConfiguration>> halConfigurationCustomizers;
    private final ObjectProvider<MediaTypeConfigurationCustomizer<HalFormsConfiguration>> halFormsConfigurationCustomizers;

    Step 2 — Update the constructor signature and body:

    public RepositoryRestMvcConfiguration(
            ApplicationContext context,
            @Qualifier("mvcConversionService") ObjectFactory<ConversionService> conversionService,
            ObjectProvider<LinkRelationProvider> relProvider,
            ObjectProvider<CurieProvider> curieProvider,
            ObjectProvider<HalConfiguration> halConfiguration,
            ObjectProvider<MediaTypeConfigurationCustomizer<HalConfiguration>> halConfigurationCustomizers,       // NEW
            ObjectProvider<MediaTypeConfigurationCustomizer<HalFormsConfiguration>> halFormsConfigurationCustomizers, // NEW
            ObjectProvider<JsonMapper> objectMapper,
            ObjectProvider<Builder> objectMapperBuilder,
            ObjectProvider<RepresentationModelProcessorInvoker> invoker,
            ObjectProvider<MessageResolver> resolver,
            ObjectProvider<GeoJacksonModule> geoModule,
            ObjectProvider<PathPatternParser> parser) {
    
        // ... existing code ...
        this.halConfigurationCustomizers = halConfigurationCustomizers;
        this.halFormsConfigurationCustomizers = halFormsConfigurationCustomizers;
    }

    Step 3 — Fix halObjectMapper() to use MediaTypeConfigurationFactory:

    public JsonMapper halObjectMapper(LinkCollector linkCollector) {
    
        LinkRelationProvider defaultedRelProvider = this.relProvider.getIfUnique(EvoInflectorLinkRelationProvider::new);
        CurieProvider curieProvider = this.curieProvider
                .getIfUnique(() -> new DefaultCurieProvider(Collections.emptyMap()));
    
        // Apply customizers via MediaTypeConfigurationFactory — same pattern as HalMediaTypeConfiguration
        MediaTypeConfigurationFactory<HalConfiguration, MediaTypeConfigurationCustomizer<HalConfiguration>> factory =
                new MediaTypeConfigurationFactory<>(
                        () -> this.halConfiguration.getIfAvailable(HalConfiguration::new),
                        halConfigurationCustomizers);
        HalConfiguration halConfiguration = factory.getConfiguration();
    
        HalHandlerInstantiator instantiator = new HalHandlerInstantiator(defaultedRelProvider, curieProvider,
                resolver.getObject(), halConfiguration, applicationContext.getAutowireCapableBeanFactory());
    
        return basicObjectMapperBuilder()
                .addModule(persistentEntityJackson3Module(linkCollector))
                .addModule(new HalJacksonModule())
                .handlerInstantiator(instantiator)
                .build();
    }

    Step 4 — Fix halFormsJacksonHttpMessageConverter() to use MediaTypeConfigurationFactory for both HAL and HAL Forms:

    TypeConstrainedJacksonJsonHttpMessageConverter halFormsJacksonHttpMessageConverter(LinkCollector linkCollector) {
    
        LinkRelationProvider defaultedRelProvider = this.relProvider.getIfUnique(EvoInflectorLinkRelationProvider::new);
        CurieProvider curieProvider = this.curieProvider
                .getIfUnique(() -> new DefaultCurieProvider(Collections.emptyMap()));
    
        // Apply HAL customizers first (same as HalFormsMediaTypeConfiguration does)
        MediaTypeConfigurationFactory<HalConfiguration, MediaTypeConfigurationCustomizer<HalConfiguration>> halFactory =
                new MediaTypeConfigurationFactory<>(
                        () -> this.halConfiguration.getIfAvailable(HalConfiguration::new),
                        halConfigurationCustomizers);
    
        // Then apply HAL Forms customizers on top
        MediaTypeConfigurationFactory<HalFormsConfiguration, MediaTypeConfigurationCustomizer<HalFormsConfiguration>> halFormsFactory =
                new MediaTypeConfigurationFactory<>(
                        () -> new HalFormsConfiguration(halFactory.getConfiguration()),
                        halFormsConfigurationCustomizers);
    
        HalFormsConfiguration configuration = halFormsFactory.getConfiguration();
    
        Builder builder = basicObjectMapperBuilder();
        builder.addModule(persistentEntityJackson3Module(linkCollector));
        builder.addModule(new HalFormsJacksonModule());
        builder.handlerInstantiator(new HalJacksonModule.HalHandlerInstantiator(defaultedRelProvider, curieProvider,
                resolver.getObject(), configuration.getHalConfiguration(), applicationContext.getAutowireCapableBeanFactory()));
    
        return new HalFormsHttpMessageConverter(applicationContext, builder.build());
    }

    Required Imports to Add

    import org.springframework.hateoas.mediatype.MediaTypeConfigurationCustomizer;
    import org.springframework.hateoas.mediatype.MediaTypeConfigurationFactory;
    import org.springframework.hateoas.mediatype.hal.forms.HalFormsConfiguration;

    Test to Add

    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-2436
    void appliesHalConfigurationCustomizer() {
    
        AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext(HalCustomizerConfiguration.class);
    
        JacksonJsonHttpMessageConverter converter = 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)
    static class HalCustomizerConfiguration {
    
        @Bean
        MediaTypeConfigurationCustomizer<HalConfiguration> halCustomizer() {
            return config -> config.withRenderSingleLinksFor("supplier", HalConfiguration.RenderSingleLinks.AS_ARRAY);
        }
    }

    Summary

    Location Problem Fix
    halObjectMapper() Calls halConfiguration.getIfUnique() directly — customizers ignored 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.

    cc: @odrotbohm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions