Skip to content

Commit

Permalink
Merge pull request #291 from xenit-eu/always-apply-mediatype-customizers
Browse files Browse the repository at this point in the history
Ensure that HalConfiguration MediaTypeConfigurationCustomizers are always run
  • Loading branch information
vierbergenlars authored Nov 25, 2024
2 parents 56861a7 + 5a0d836 commit 7d21fc5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 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())
;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ public class ChangeEventPublicationIntegrationTest {
href: "http://localhost/customers/${#customerId}/orders"
}
],
"cg:content": {
name: "content",
href: "http://localhost/customers/${#customerId}/content"
},
"cg:content": [
{
name: "content",
href: "http://localhost/customers/${#customerId}/content"
}
],
"curies": [
{
name: "d",
Expand Down

0 comments on commit 7d21fc5

Please sign in to comment.