Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that HalConfiguration MediaTypeConfigurationCustomizers are always run #291

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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