Skip to content

Commit

Permalink
Merge branch 'mathieu-amblard-main'
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Dec 2, 2023
2 parents c9cff14 + ff3e00d commit 6f0a605
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ CollectionModelContentConverter collectionModelContentConverter(HateoasHalProvid
* @param halProvider the hal provider
* @param springDocConfigProperties the spring doc config properties
* @param objectMapperProvider the object mapper provider
* @return the open api customiser
* @return the open api customizer
* @see org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider) org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider)org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider)org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider)org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider)org.springframework.hateoas.mediatype.hal.Jackson2HalModule.HalLinkListSerializer#serialize(Links, JsonGenerator, SerializerProvider)
*/
@Bean(Constants.LINKS_SCHEMA_CUSTOMISER)
@ConditionalOnMissingBean
@ConditionalOnMissingBean(name = Constants.LINKS_SCHEMA_CUSTOMISER)
@Lazy(false)
GlobalOpenApiCustomizer linksSchemaCustomizer(HateoasHalProvider halProvider, SpringDocConfigProperties springDocConfigProperties,
ObjectMapperProvider objectMapperProvider) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.springdoc.core.configuration;

import org.junit.jupiter.api.Test;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.customizers.OpenApiHateoasLinksCustomizer;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.hateoas.config.HateoasConfiguration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.springdoc.core.utils.Constants.LINKS_SCHEMA_CUSTOMISER;

class SpringDocHateoasConfigurationTest {

@Test
void linksSchemaCustomizerShouldBeRegistered() {
new WebApplicationContextRunner()
.withPropertyValues(
"springdoc.api-docs.enabled=true",
"springdoc.enable-hateoas=true"
)
.withConfiguration(AutoConfigurations.of(
WebMvcAutoConfiguration.class,
HateoasConfiguration.class,
SpringDocConfiguration.class,
SpringDocConfigProperties.class,
SpringDocHateoasConfiguration.class
))
.run(context -> {
assertThat(context).getBeanNames(GlobalOpenApiCustomizer.class)
.hasSize(1)
.containsExactly(LINKS_SCHEMA_CUSTOMISER);
assertThat(context.getBean(LINKS_SCHEMA_CUSTOMISER)).isExactlyInstanceOf(OpenApiHateoasLinksCustomizer.class);
});
}

@Test
void linksSchemaCustomizerShouldBeRegisteredWithMultipleGlobalOpenApiCustomizer() {
new WebApplicationContextRunner()
.withPropertyValues(
"springdoc.api-docs.enabled=true",
"springdoc.enable-hateoas=true"
)
.withConfiguration(AutoConfigurations.of(
WebMvcAutoConfiguration.class,
HateoasConfiguration.class,
SpringDocConfiguration.class,
SpringDocConfigProperties.class,
SpringDocHateoasConfiguration.class
))
.withBean("globalOpenApiCustomizer", GlobalOpenApiCustomizer.class, () -> mock(GlobalOpenApiCustomizer.class))
.run(context -> {
assertThat(context).getBeanNames(GlobalOpenApiCustomizer.class)
.hasSize(2)
.containsExactlyInAnyOrder(LINKS_SCHEMA_CUSTOMISER, "globalOpenApiCustomizer");
});
}

@Test
void linksSchemaCustomizerShouldNotBeRegisteredIfBeanWithSameNameAlreadyExists() {
new WebApplicationContextRunner()
.withPropertyValues(
"springdoc.api-docs.enabled=true",
"springdoc.enable-hateoas=true"
)
.withConfiguration(AutoConfigurations.of(
WebMvcAutoConfiguration.class,
HateoasConfiguration.class,
SpringDocConfiguration.class,
SpringDocConfigProperties.class,
SpringDocHateoasConfiguration.class
))
.withBean(LINKS_SCHEMA_CUSTOMISER, GlobalOpenApiCustomizer.class, () -> mock(GlobalOpenApiCustomizer.class))
.run(context -> {
assertThat(context).getBeanNames(GlobalOpenApiCustomizer.class)
.hasSize(1)
.containsExactly(LINKS_SCHEMA_CUSTOMISER);
assertThat(context.getBean(LINKS_SCHEMA_CUSTOMISER)).isNotExactlyInstanceOf(OpenApiHateoasLinksCustomizer.class);
});
}
}

0 comments on commit 6f0a605

Please sign in to comment.