Skip to content
Open
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 @@ -118,17 +118,27 @@ HateoasHalProvider hateoasHalProvider(ObjectMapperProvider objectMapperProvider)
}

/**
* Collection model content converter collection model content converter.
*
* @param halProvider the hal provider
* @param linkRelationProvider the link relation provider
* @return the collection model content converter
* Creates CollectionModelContentConverter only when a LinkRelationProvider bean exists.
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
CollectionModelContentConverter collectionModelContentConverter(HateoasHalProvider halProvider, LinkRelationProvider linkRelationProvider) {
return halProvider.isHalEnabled() ? new CollectionModelContentConverter(linkRelationProvider) : null;
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(LinkRelationProvider.class)
static class CollectionModelContentConverterConfiguration {

/**
* Collection model content converter collection model content converter.
*
* @param halProvider the hal provider
* @param linkRelationProvider the link relation provider
* @return the collection model content converter
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
CollectionModelContentConverter collectionModelContentConverter(
HateoasHalProvider halProvider,
LinkRelationProvider linkRelationProvider) {
return halProvider.isHalEnabled() ? new CollectionModelContentConverter(linkRelationProvider) : null;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.webmvc.autoconfigure.WebMvcAutoConfiguration;
import org.springframework.hateoas.config.HateoasConfiguration;
import org.springframework.hateoas.server.LinkRelationProvider;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -92,6 +93,7 @@ void linksSchemaCustomizerShouldNotBeRegisteredIfBeanWithSameNameAlreadyExists()
@Test
void halBeansShouldBeRegisteredWhenHateoasPropertiesIsPresent() {
hateoasContextRunner()
.withBean(LinkRelationProvider.class, () -> mock(LinkRelationProvider.class)) // ← add this
.run(context -> {
assertThat(context).hasNotFailed();
assertThat(context).hasSingleBean(HateoasHalProvider.class);
Expand All @@ -111,6 +113,7 @@ void halBeansShouldBeRegisteredWhenHateoasPropertiesIsPresent() {
void halBeansShouldBeRegisteredWhenHateoasPropertiesIsAbsent() {
hateoasContextRunner()
.withClassLoader(new FilteredClassLoader(HateoasProperties.class))
.withBean(LinkRelationProvider.class, () -> mock(LinkRelationProvider.class)) // ← add this
.run(context -> {
assertThat(context).hasNotFailed();
assertThat(context).hasSingleBean(HateoasHalProvider.class);
Expand All @@ -134,4 +137,29 @@ private WebApplicationContextRunner hateoasContextRunner() {
SpringDocHateoasConfiguration.class
));
}

/**
* LinkRelationProvider class may be on the classpath while no bean of that type
* is present. The application context must still start and
* CollectionModelContentConverter must not be created.
*/
@Test
void collectionModelContentConverterShouldNotBeCreatedWhenLinkRelationProviderBeanIsAbsent() {
new WebApplicationContextRunner()
.withPropertyValues(
"springdoc.api-docs.enabled=true",
"springdoc.enable-hateoas=true"
)
.withConfiguration(AutoConfigurations.of(
WebMvcAutoConfiguration.class,
// deliberately omit HateoasConfiguration → no LinkRelationProvider bean
SpringDocConfiguration.class,
SpringDocConfigProperties.class,
SpringDocHateoasConfiguration.class
))
.run(context -> {
assertThat(context).hasNotFailed();
assertThat(context).doesNotHaveBean(CollectionModelContentConverter.class);
});
}
}