From c339d95dd1ff1dd193801a5c206660d237de97f1 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 10 Oct 2023 11:55:11 +0100 Subject: [PATCH] Refactor SpringDoc support a bit --- .../OpenApiAggregatorConfiguration.java | 55 +++++-------------- .../OpenApiAggregatorProperties.java | 25 +++++++++ 2 files changed, 39 insertions(+), 41 deletions(-) create mode 100644 src/main/java/org/springframework/openapi/aggregator/OpenApiAggregatorProperties.java diff --git a/src/main/java/org/springframework/openapi/aggregator/OpenApiAggregatorConfiguration.java b/src/main/java/org/springframework/openapi/aggregator/OpenApiAggregatorConfiguration.java index 69b8f81..cc0c337 100644 --- a/src/main/java/org/springframework/openapi/aggregator/OpenApiAggregatorConfiguration.java +++ b/src/main/java/org/springframework/openapi/aggregator/OpenApiAggregatorConfiguration.java @@ -15,25 +15,15 @@ */ package org.springframework.openapi.aggregator; -import java.util.List; -import java.util.Optional; - import org.springdoc.core.configuration.SpringDocConfiguration; -import org.springdoc.core.customizers.OpenApiBuilderCustomizer; -import org.springdoc.core.customizers.ServerBaseUrlCustomizer; -import org.springdoc.core.properties.SpringDocConfigProperties; -import org.springdoc.core.providers.JavadocProvider; import org.springdoc.core.service.OpenAPIService; -import org.springdoc.core.service.SecurityService; -import org.springdoc.core.utils.PropertyResolverUtils; import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -45,10 +35,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.v3.core.util.Json; -import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; -import io.swagger.v3.oas.models.Paths; -import io.swagger.v3.oas.models.info.Info; /** * Autoconfiguration for the OpenAPI aggregator. @@ -56,33 +43,19 @@ @Configuration @ConditionalOnBean(OpenApiAggregatorSpecs.class) @AutoConfigureBefore(SpringDocConfiguration.class) -@Import(SpringdocConfiguration.class) +@Import(SpringDocSpecConfiguration.class) +@EnableConfigurationProperties(OpenApiAggregatorProperties.class) public class OpenApiAggregatorConfiguration { /** * Create a new {@link OpenApiAggregator} instance. * @param specs the specs to use - * @param base the base to merge with, e.g. for common info + * @param properties the configuration, e.g. for common info * @return an aggregator */ @Bean - public OpenApiAggregator openApiAggregator(OpenApiAggregatorSpecs specs, - @Qualifier("spring.openapi.base") OpenAPI base) { - return new OpenApiAggregator(specs, base); - } - - /** - * Create a new {@link OpenAPI} instance and make it configurable via Spring Boot. - * @return a base instance - */ - @Bean("spring.openapi.base") - @ConfigurationProperties("spring.openapi.base") - public OpenAPI base() { - OpenAPI api = new OpenAPI(); - api.paths(new Paths()); - api.components(new Components()); - api.info(new Info().title("Gateway API").description("Gateway API").version("1.0.0")); - return api; + public OpenApiAggregator openApiAggregator(OpenApiAggregatorSpecs specs, OpenApiAggregatorProperties properties) { + return new OpenApiAggregator(specs, properties.getBase()); } /** @@ -102,16 +75,16 @@ public AggregatorEndpoint aggregatorEndpoint(OpenApiAggregator aggregator) { @Configuration @ConditionalOnClass(OpenAPIService.class) -class SpringdocConfiguration { +class SpringDocSpecConfiguration { + /** + * Create a new {@link OpenAPI} instance to inject into the SpringDoc spec generator. + * @param aggregator the aggregator to use + * @return an OpenAPI spec generated from the endpoints in this application + */ @Bean - OpenAPIService openAPIBuilder(OpenApiAggregator aggregator, SecurityService securityParser, - SpringDocConfigProperties springDocConfigProperties, PropertyResolverUtils propertyResolverUtils, - Optional> openApiBuilderCustomisers, - Optional> serverBaseUrlCustomisers, - Optional javadocProvider) { - return new OpenAPIService(Optional.of(aggregator.aggregate()), securityParser, springDocConfigProperties, - propertyResolverUtils, openApiBuilderCustomisers, serverBaseUrlCustomisers, javadocProvider); + OpenAPI openAPIBaseSpec(OpenApiAggregator aggregator) { + return aggregator.aggregate(); } } diff --git a/src/main/java/org/springframework/openapi/aggregator/OpenApiAggregatorProperties.java b/src/main/java/org/springframework/openapi/aggregator/OpenApiAggregatorProperties.java new file mode 100644 index 0000000..e06499d --- /dev/null +++ b/src/main/java/org/springframework/openapi/aggregator/OpenApiAggregatorProperties.java @@ -0,0 +1,25 @@ +package org.springframework.openapi.aggregator; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.Paths; +import io.swagger.v3.oas.models.info.Info; + +@ConfigurationProperties(prefix = "spring.openapi") +public class OpenApiAggregatorProperties { + + private OpenAPI base = new OpenAPI(); + + public OpenApiAggregatorProperties() { + base.paths(new Paths()); + base.components(new Components()); + base.info(new Info().title("Gateway API").description("Gateway API").version("1.0.0")); + } + + public OpenAPI getBase() { + return base; + } + +}