Skip to content

Commit

Permalink
Ensure that HalConfiguration MediaTypeConfigurationCustomizers are al…
Browse files Browse the repository at this point in the history
…ways run

spring-data-rest is not aware of the existence of MediaTypeConfigurationCustomizer, so it does not apply the configuration when pulling the HalConfiguration bean directly.
This leads to the configuration settings in HalConfiguration inconsistently being applied, depending on if a setting is used in spring-hateoas or spring-data-rest.

Always applying all customizers directly in a BeanPostProcessor ensures that the HalConfiguration bean itself has all the customizations directly baked-in,
and does not require all consumers to be aware of MediaTypeConfigurationCustomizer
  • Loading branch information
vierbergenlars committed Nov 18, 2024
1 parent 7a8b7bb commit 4af1c4d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.contentgrid.spring.data.rest.problem.ContentGridProblemDetailsConfiguration;
import com.contentgrid.spring.data.rest.validation.ContentGridSpringDataRestValidationConfiguration;
import com.contentgrid.spring.data.rest.webmvc.ContentGridSpringDataRestProfileConfiguration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand All @@ -22,6 +24,9 @@
import org.springframework.data.rest.webmvc.ContentGridRestProperties;
import org.springframework.data.rest.webmvc.ContentGridSpringDataRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.hateoas.mediatype.MediaTypeConfigurationCustomizer;
import org.springframework.hateoas.mediatype.MediaTypeConfigurationFactory;
import org.springframework.hateoas.mediatype.hal.HalConfiguration;

@AutoConfiguration
@ConditionalOnBean(RepositoryRestMvcConfiguration.class)
Expand Down Expand Up @@ -54,6 +59,21 @@ ContentGridRestProperties contentGridRestProperties() {
return new ContentGridRestProperties();
}

@Bean
static BeanPostProcessor contentGridApplyHalConfigurationCustomizers(
ObjectProvider<MediaTypeConfigurationCustomizer<HalConfiguration>> customizers
) {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof HalConfiguration halConfiguration) {
return new MediaTypeConfigurationFactory<>(() -> halConfiguration, customizers).getConfiguration();
}
return bean;
}
};
}

@ConditionalOnBean(CurieProviderCustomizer.class)
@Import({
ContentGridCurieConfiguration.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.contentgrid.spring.test.fixture.invoicing.InvoicingApplication;
Expand Down Expand Up @@ -233,4 +234,23 @@ void updateFormFieldForConstrainedAttributeOptions() throws Exception {
);
}

@Test
void relationAndContentIsAlwaysArray() throws Exception {
var createdCustomer = mockMvc.perform(
post("/customers").contentType(MediaType.APPLICATION_JSON).content("""
{
"vat": "BE123"
}
"""))
.andExpect(status().isCreated())
.andReturn()
.getResponse()
.getHeader("Location");

mockMvc.perform(get(createdCustomer).accept(MediaTypes.HAL_FORMS_JSON))
.andExpect(jsonPath("$._links['cg:content']").isArray())
.andExpect(jsonPath("$._links['cg:relation']").isArray())
;
}

}

0 comments on commit 4af1c4d

Please sign in to comment.