|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * |
| 6 | + * * * * * Copyright 2019-2022 the original author or authors. |
| 7 | + * * * * * |
| 8 | + * * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * * * * * you may not use this file except in compliance with the License. |
| 10 | + * * * * * You may obtain a copy of the License at |
| 11 | + * * * * * |
| 12 | + * * * * * https://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * * * * * |
| 14 | + * * * * * Unless required by applicable law or agreed to in writing, software |
| 15 | + * * * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * * * * * See the License for the specific language governing permissions and |
| 18 | + * * * * * limitations under the License. |
| 19 | + * * * * |
| 20 | + * * * |
| 21 | + * * |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +package org.springdoc.core; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Optional; |
| 29 | +import java.util.Properties; |
| 30 | + |
| 31 | +import org.apache.commons.lang3.StringUtils; |
| 32 | + |
| 33 | +import org.springframework.beans.factory.InitializingBean; |
| 34 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 35 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
| 36 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 37 | +import org.springframework.context.annotation.Configuration; |
| 38 | +import org.springframework.context.annotation.Lazy; |
| 39 | +import org.springframework.core.io.ClassPathResource; |
| 40 | +import org.springframework.core.io.Resource; |
| 41 | +import org.springframework.core.io.support.PropertiesLoaderUtils; |
| 42 | +import org.springframework.util.AntPathMatcher; |
| 43 | + |
| 44 | +/** |
| 45 | + * The type Spring doc Native Configuration. |
| 46 | + * @author bnasslahsen |
| 47 | + */ |
| 48 | +@Lazy(false) |
| 49 | +@ConditionalOnExpression("${springdoc.api-docs.enabled:true}") |
| 50 | +@ConditionalOnWebApplication |
| 51 | +@Configuration(proxyBeanMethods = false) |
| 52 | +@ConditionalOnBean(SpringDocConfiguration.class) |
| 53 | +public class SpringDocUIConfiguration implements InitializingBean { |
| 54 | + |
| 55 | + /** |
| 56 | + * The constant SPRINGDOC_CONFIG_PROPERTIES. |
| 57 | + */ |
| 58 | + public static final String SPRINGDOC_CONFIG_PROPERTIES = "springdoc.config.properties"; |
| 59 | + |
| 60 | + /** |
| 61 | + * The constant SPRINGDOC_SWAGGERUI_VERSION. |
| 62 | + */ |
| 63 | + private static final String SPRINGDOC_SWAGGER_UI_VERSION = "springdoc.swagger-ui.version"; |
| 64 | + |
| 65 | + /** |
| 66 | + * The Swagger ui config properties. |
| 67 | + */ |
| 68 | + private final Optional<SwaggerUiConfigProperties> optionalSwaggerUiConfigProperties; |
| 69 | + |
| 70 | + /** |
| 71 | + * Instantiates a new Spring doc hints. |
| 72 | + * |
| 73 | + * @param optionalSwaggerUiConfigProperties the swagger ui config properties |
| 74 | + */ |
| 75 | + public SpringDocUIConfiguration(Optional<SwaggerUiConfigProperties> optionalSwaggerUiConfigProperties) { |
| 76 | + this.optionalSwaggerUiConfigProperties = optionalSwaggerUiConfigProperties; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void afterPropertiesSet() { |
| 81 | + optionalSwaggerUiConfigProperties.ifPresent(swaggerUiConfigProperties -> { |
| 82 | + if (StringUtils.isEmpty(swaggerUiConfigProperties.getVersion())) { |
| 83 | + try { |
| 84 | + Resource resource = new ClassPathResource(AntPathMatcher.DEFAULT_PATH_SEPARATOR + SPRINGDOC_CONFIG_PROPERTIES); |
| 85 | + Properties props = PropertiesLoaderUtils.loadProperties(resource); |
| 86 | + swaggerUiConfigProperties.setVersion(props.getProperty(SPRINGDOC_SWAGGER_UI_VERSION)); |
| 87 | + } |
| 88 | + catch (IOException e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments