Skip to content

Commit b2f298a

Browse files
committed
Getting 404 when trying to access swagger-ui of a native spring-boot app. Fixes #1394.
1 parent dde8f90 commit b2f298a

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

springdoc-openapi-native/src/main/java/org/springdoc/nativex/core/SpringDocHints.java

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2019-2020 the original author or authors.
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
121
package org.springdoc.nativex.core;
222

23+
import java.io.IOException;
24+
import java.util.Properties;
25+
326
import io.swagger.v3.core.converter.ModelConverter;
427
import io.swagger.v3.core.converter.ModelConverters;
528
import io.swagger.v3.core.filter.SpecFilter;
@@ -77,6 +100,7 @@
77100
import io.swagger.v3.oas.models.parameters.QueryParameter;
78101
import io.swagger.v3.oas.models.security.Scopes;
79102
import io.swagger.v3.oas.models.servers.ServerVariables;
103+
import org.apache.commons.lang3.StringUtils;
80104
import org.springdoc.api.AbstractOpenApiResource;
81105
import org.springdoc.api.mixins.SortedOpenAPIMixin;
82106
import org.springdoc.api.mixins.SortedSchemaMixin;
@@ -106,17 +130,26 @@
106130
import org.springdoc.core.converters.SchemaPropertyDeprecatingConverter;
107131
import org.springdoc.ui.AbstractSwaggerWelcome;
108132

133+
import org.springframework.beans.factory.InitializingBean;
109134
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
110135
import org.springframework.context.annotation.Configuration;
111136
import org.springframework.context.annotation.Lazy;
112137
import org.springframework.context.annotation.PropertySource;
138+
import org.springframework.core.io.ClassPathResource;
139+
import org.springframework.core.io.Resource;
140+
import org.springframework.core.io.support.PropertiesLoaderUtils;
113141
import org.springframework.nativex.hint.JdkProxyHint;
114142
import org.springframework.nativex.hint.ResourceHint;
115143
import org.springframework.nativex.hint.TypeAccess;
116144
import org.springframework.nativex.hint.TypeHint;
145+
import org.springframework.util.AntPathMatcher;
117146

118147
import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
119148

149+
/**
150+
* The type Spring doc hints.
151+
* @author bnasslahsen
152+
*/
120153
@JdkProxyHint(typeNames = "javax.servlet.http.HttpServletRequest")
121154
@JdkProxyHint(typeNames = "org.springframework.web.context.request.NativeWebRequest")
122155

@@ -263,5 +296,44 @@
263296
@Lazy(false)
264297
@Configuration(proxyBeanMethods = false)
265298
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
266-
public class SpringDocHints {}
299+
public class SpringDocHints implements InitializingBean {
300+
301+
/**
302+
* The Swagger ui config properties.
303+
*/
304+
private final SwaggerUiConfigProperties swaggerUiConfigProperties;
305+
306+
/**
307+
* The constant SPRINGDOC_CONFIG_PROPERTIES.
308+
*/
309+
private static final String SPRINGDOC_CONFIG_PROPERTIES = "springdoc.config.properties";
310+
311+
/**
312+
* The constant SPRINGDOC_SWAGGERUI_VERSION.
313+
*/
314+
private static final String SPRINGDOC_SWAGGERUI_VERSION= "springdoc.swagger-ui.version";
315+
316+
/**
317+
* Instantiates a new Spring doc hints.
318+
*
319+
* @param swaggerUiConfigProperties the swagger ui config properties
320+
*/
321+
public SpringDocHints(SwaggerUiConfigProperties swaggerUiConfigProperties) {
322+
this.swaggerUiConfigProperties = swaggerUiConfigProperties;
323+
}
324+
325+
@Override
326+
public void afterPropertiesSet() {
327+
if (StringUtils.isEmpty(swaggerUiConfigProperties.getVersion())) {
328+
try {
329+
Resource resource = new ClassPathResource(AntPathMatcher.DEFAULT_PATH_SEPARATOR + SPRINGDOC_CONFIG_PROPERTIES);
330+
Properties props = PropertiesLoaderUtils.loadProperties(resource);
331+
swaggerUiConfigProperties.setVersion(props.getProperty(SPRINGDOC_SWAGGERUI_VERSION));
332+
}
333+
catch (IOException e) {
334+
throw new RuntimeException(e);
335+
}
336+
}
337+
}
338+
}
267339

0 commit comments

Comments
 (0)