Skip to content

Commit cf77e88

Browse files
author
bnasslahsen
committed
Review PR: Add pre-loading setting to load OpenAPI early. Fixes #854.
1 parent 5bb12ec commit cf77e88

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import java.util.concurrent.Executors;
4141
import java.util.stream.Collectors;
4242

43+
import javax.annotation.PostConstruct;
44+
4345
import com.fasterxml.jackson.annotation.JsonView;
4446
import com.fasterxml.jackson.databind.ObjectMapper;
4547
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
@@ -201,10 +203,8 @@ protected AbstractOpenApiResource(String groupName, ObjectFactory<OpenAPIBuilder
201203
operationCustomizers.get().removeIf(Objects::isNull);
202204
this.operationCustomizers = operationCustomizers;
203205
this.actuatorProvider = actuatorProvider;
204-
if (springDocConfigProperties.isPreLoadingEnabled()) {
205-
Executors.newSingleThreadExecutor()
206-
.execute(this::getOpenApi);
207-
}
206+
if (springDocConfigProperties.isPreLoadingEnabled())
207+
Executors.newSingleThreadExecutor().execute(this::getOpenApi);
208208
}
209209

210210
/**
@@ -231,13 +231,13 @@ public static void addHiddenRestControllers(Class<?>... classes) {
231231
* @param classes the classes
232232
*/
233233
public static void addHiddenRestControllers(String... classes) {
234-
Set<Class<?>> hiddenClasses =new HashSet<>();
234+
Set<Class<?>> hiddenClasses = new HashSet<>();
235235
for (String aClass : classes) {
236236
try {
237237
hiddenClasses.add(Class.forName(aClass));
238238
}
239239
catch (ClassNotFoundException e) {
240-
LOGGER.warn("The following class doesn't exist and cannot be hidden: {}", aClass);
240+
LOGGER.warn("The following class doesn't exist and cannot be hidden: {}", aClass);
241241
}
242242
}
243243
HIDDEN_REST_CONTROLLERS.addAll(hiddenClasses);
@@ -262,8 +262,8 @@ protected synchronized OpenAPI getOpenApi() {
262262
Map<String, Object> findControllerAdvice = openAPIBuilder.getControllerAdviceMap();
263263
// calculate generic responses
264264
openApi = openAPIBuilder.getCalculatedOpenAPI();
265-
if (springDocConfigProperties.isOverrideWithGenericResponse() && !CollectionUtils.isEmpty(findControllerAdvice)){
266-
if(!CollectionUtils.isEmpty(mappingsMap))
265+
if (springDocConfigProperties.isOverrideWithGenericResponse() && !CollectionUtils.isEmpty(findControllerAdvice)) {
266+
if (!CollectionUtils.isEmpty(mappingsMap))
267267
findControllerAdvice.putAll(mappingsMap);
268268
responseBuilder.buildGenericResponse(openApi.getComponents(), findControllerAdvice);
269269
}
@@ -969,7 +969,7 @@ protected void initOpenAPIBuilder() {
969969
* @return the yaml mapper
970970
*/
971971
protected ObjectMapper getYamlMapper() {
972-
ObjectMapper objectMapper = Yaml.mapper();
972+
ObjectMapper objectMapper = Yaml.mapper();
973973
YAMLFactory factory = (YAMLFactory) objectMapper.getFactory();
974974
factory.configure(Feature.USE_NATIVE_TYPE_ID, false);
975975
return objectMapper;

springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ else if (calculatedOpenAPI.getInfo() == null) {
225225
* @return the open api
226226
*/
227227
public OpenAPI updateServers(OpenAPI openAPI) {
228-
if (!isServersPresent) // default server value
228+
if (!isServersPresent && serverBaseUrl!=null) // default server value
229229
{
230230
Server server = new Server().url(serverBaseUrl).description(DEFAULT_SERVER_DESCRIPTION);
231231
List<Server> servers = new ArrayList<>();

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocConfigProperties.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public class SpringDocConfigProperties {
133133
/**
134134
* Allow for pre-loading OpenAPI
135135
*/
136-
private boolean preLoadingEnabled = false;
136+
private boolean preLoadingEnabled;
137137

138138
/**
139139
* Is use fqn boolean.
@@ -477,10 +477,20 @@ public void setWriterWithDefaultPrettyPrinter(boolean writerWithDefaultPrettyPri
477477
this.writerWithDefaultPrettyPrinter = writerWithDefaultPrettyPrinter;
478478
}
479479

480+
/**
481+
* Sets pre loading enabled.
482+
*
483+
* @param preLoadingEnabled the pre loading enabled
484+
*/
480485
public void setPreLoadingEnabled(boolean preLoadingEnabled) {
481486
this.preLoadingEnabled = preLoadingEnabled;
482487
}
483488

489+
/**
490+
* Is pre loading enabled boolean.
491+
*
492+
* @return the boolean
493+
*/
484494
public boolean isPreLoadingEnabled() {
485495
return preLoadingEnabled;
486496
}

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app18/SpringDocApp18Test.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import test.org.springdoc.api.AbstractSpringDocTest;
2222

2323
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
import org.springframework.test.context.TestPropertySource;
2425

26+
@TestPropertySource(properties = "springdoc.pre-loading-enabled=true")
2527
public class SpringDocApp18Test extends AbstractSpringDocTest {
2628

2729
@SpringBootApplication

0 commit comments

Comments
 (0)