Skip to content

Commit

Permalink
Refactor SpringDoc support a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyer committed Oct 10, 2023
1 parent 7120364 commit c339d95
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,44 +35,27 @@
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.
*/
@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());
}

/**
Expand All @@ -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<List<OpenApiBuilderCustomizer>> openApiBuilderCustomisers,
Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomisers,
Optional<JavadocProvider> javadocProvider) {
return new OpenAPIService(Optional.of(aggregator.aggregate()), securityParser, springDocConfigProperties,
propertyResolverUtils, openApiBuilderCustomisers, serverBaseUrlCustomisers, javadocProvider);
OpenAPI openAPIBaseSpec(OpenApiAggregator aggregator) {
return aggregator.aggregate();
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit c339d95

Please sign in to comment.