spring-projects / spring-projects/spring-data-rest

MediaTypeConfigurationCustomizer(s) are not applied on HalConfiguration

Open
#2,436 1 comment 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Nov 18, 2024.

status: waiting-for-triage
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

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 https://github.com/spring-projects/spring-hateoas/issues/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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.