Skip to content

Commit d43dbe7

Browse files
author
bnasslahsen
committed
Servers OpenAPI block resets after customizing with GroupedOpenApi. Fixes #695
1 parent 410bb8a commit d43dbe7

File tree

5 files changed

+47
-30
lines changed

5 files changed

+47
-30
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public static void addHiddenRestControllers(String... classes) {
145145
hiddenClasses.add(Class.forName(aClass));
146146
}
147147
catch (ClassNotFoundException e) {
148-
LOGGER.warn("The following class doesn't exist and cannot be hidden: "+ aClass);
148+
LOGGER.warn("The following class doesn't exist and cannot be hidden: " + aClass);
149149
}
150150
}
151151
HIDDEN_REST_CONTROLLERS.addAll(hiddenClasses);
@@ -169,6 +169,10 @@ protected synchronized OpenAPI getOpenApi() {
169169
getPaths(mappingsMap);
170170
// run the optional customisers
171171
openApiCustomisers.ifPresent(apiCustomisers -> apiCustomisers.forEach(openApiCustomiser -> openApiCustomiser.customise(openApi)));
172+
if (!CollectionUtils.isEmpty(openApi.getServers()))
173+
openAPIBuilder.setServersPresent(true);
174+
openAPIBuilder.updateServers(openApi);
175+
172176
if (springDocConfigProperties.isRemoveBrokenReferenceDefinitions())
173177
this.removeBrokenReferenceDefinitions(openApi);
174178
openAPIBuilder.setCachedOpenAPI(openApi);
@@ -177,7 +181,7 @@ protected synchronized OpenAPI getOpenApi() {
177181
Duration.between(start, Instant.now()).toMillis());
178182
}
179183
else {
180-
openApi = openAPIBuilder.calculateCachedOpenAPI();
184+
openApi = openAPIBuilder.updateServers(openAPIBuilder.getCachedOpenAPI());
181185
}
182186
return openApi;
183187
}
@@ -339,7 +343,7 @@ protected void calculatePath(RouterOperation routerOperation) {
339343
if (apiOperation != null)
340344
openAPI = operationParser.parse(apiOperation, operation, openAPI, methodAttributes);
341345

342-
String operationId = operationParser.getOperationId(operation.getOperationId(),openAPI);
346+
String operationId = operationParser.getOperationId(operation.getOperationId(), openAPI);
343347
operation.setOperationId(operationId);
344348

345349
fillParametersList(operation, queryParams, methodAttributes);

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,24 @@ else if (calculatedOpenAPI.getInfo() == null) {
151151
this.mappingsMap.putAll(context.getBeansWithAnnotation(RequestMapping.class));
152152
this.mappingsMap.putAll(context.getBeansWithAnnotation(Controller.class));
153153

154-
// default server value
155-
if (CollectionUtils.isEmpty(calculatedOpenAPI.getServers()) || !isServersPresent) {
156-
this.updateServers(calculatedOpenAPI);
157-
}
158154
// add security schemes
159155
this.calculateSecuritySchemes(calculatedOpenAPI.getComponents());
160156
openApiBuilderCustomisers.ifPresent(customisers -> customisers.forEach(customiser -> customiser.customise(this)));
161157
}
162158

163-
public void updateServers(OpenAPI openAPI) {
164-
Server server = new Server().url(serverBaseUrl).description(DEFAULT_SERVER_DESCRIPTION);
165-
List<Server> servers = new ArrayList();
166-
servers.add(server);
167-
openAPI.setServers(servers);
159+
public OpenAPI updateServers(OpenAPI openAPI) {
160+
if (!isServersPresent) // default server value
161+
{
162+
Server server = new Server().url(serverBaseUrl).description(DEFAULT_SERVER_DESCRIPTION);
163+
List<Server> servers = new ArrayList();
164+
servers.add(server);
165+
openAPI.setServers(servers);
166+
}
167+
return openAPI;
168168
}
169169

170-
public boolean isServersPresent() {
171-
return isServersPresent;
170+
public void setServersPresent(boolean serversPresent) {
171+
isServersPresent = serversPresent;
172172
}
173173

174174
public Operation buildTags(HandlerMethod handlerMethod, Operation operation, OpenAPI openAPI) {
@@ -443,12 +443,6 @@ public Map<String, Object> getControllerAdviceMap() {
443443
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
444444
}
445445

446-
public OpenAPI calculateCachedOpenAPI() {
447-
if (!this.isServersPresent())
448-
this.updateServers(cachedOpenAPI);
449-
return cachedOpenAPI;
450-
}
451-
452446
public OpenAPI getCachedOpenAPI() {
453447
return cachedOpenAPI;
454448
}

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app68/SpringDocTestApp.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@
1818

1919
package test.org.springdoc.api.app68;
2020

21+
import java.util.ArrayList;
22+
import java.util.List;
23+
2124
import io.swagger.v3.oas.models.Components;
2225
import io.swagger.v3.oas.models.OpenAPI;
2326
import io.swagger.v3.oas.models.Operation;
2427
import io.swagger.v3.oas.models.info.Info;
2528
import io.swagger.v3.oas.models.info.License;
2629
import io.swagger.v3.oas.models.security.SecurityScheme;
30+
import io.swagger.v3.oas.models.servers.Server;
2731
import org.apache.commons.lang3.StringUtils;
2832
import org.springdoc.core.Constants;
2933
import org.springdoc.core.GroupedOpenApi;
34+
import org.springdoc.core.customizers.OpenApiCustomiser;
3035
import org.springdoc.core.customizers.OperationCustomizer;
3136

3237
import org.springframework.boot.SpringApplication;
@@ -52,11 +57,25 @@ public GroupedOpenApi storeOpenApi() {
5257
public GroupedOpenApi userOpenApi() {
5358
return GroupedOpenApi.builder()
5459
.setGroup("users")
55-
.packagesToScan("test.org.springdoc.api.app68.api.user")
60+
.packagesToScan("test.org.springdoc.api.app68.api.user").addOpenApiCustomiser(serverOpenApiCustomiser1())
5661
.addOperationCustomizer(operationCustomizer())
5762
.build();
5863
}
5964

65+
public OpenApiCustomiser serverOpenApiCustomiser1() {
66+
Server server = new Server().url("http://toto.v1.com").description("myserver1");
67+
List<Server> servers = new ArrayList<>();
68+
servers.add(server);
69+
return openApi -> openApi.setServers(servers);
70+
}
71+
72+
public OpenApiCustomiser serverOpenApiCustomiser2() {
73+
Server server = new Server().url("http://toto.v2.com").description("myserver2");
74+
List<Server> servers = new ArrayList<>();
75+
servers.add(server);
76+
return openApi -> openApi.setServers(servers);
77+
}
78+
6079
OperationCustomizer operationCustomizer() {
6180
return (Operation operation, HandlerMethod handlerMethod) -> {
6281
CustomizedOperation annotation = handlerMethod.getMethodAnnotation(CustomizedOperation.class);
@@ -71,7 +90,7 @@ OperationCustomizer operationCustomizer() {
7190
public GroupedOpenApi petOpenApi() {
7291
return GroupedOpenApi.builder()
7392
.setGroup("pets")
74-
.pathsToMatch("/pet/**")
93+
.pathsToMatch("/pet/**").addOpenApiCustomiser(serverOpenApiCustomiser2())
7594
.build();
7695
}
7796

springdoc-openapi-webmvc-core/src/test/resources/results/app68-2.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
},
1313
"servers": [
1414
{
15-
"url": "http://localhost",
16-
"description": "Generated server url"
15+
"url": "http://toto.v1.com",
16+
"description": "myserver1"
1717
}
1818
],
1919
"tags": [
@@ -23,13 +23,13 @@
2323
}
2424
],
2525
"paths": {
26-
"/user/createWithArray": {
26+
"/user/createWithList": {
2727
"post": {
2828
"tags": [
2929
"user"
3030
],
3131
"summary": "Creates list of users with given input array",
32-
"operationId": "createUsersWithArrayInput",
32+
"operationId": "createUsersWithListInput",
3333
"requestBody": {
3434
"description": "List of user object",
3535
"content": {
@@ -64,13 +64,13 @@
6464
}
6565
}
6666
},
67-
"/user/createWithList": {
67+
"/user/createWithArray": {
6868
"post": {
6969
"tags": [
7070
"user"
7171
],
7272
"summary": "Creates list of users with given input array",
73-
"operationId": "createUsersWithListInput",
73+
"operationId": "createUsersWithArrayInput",
7474
"requestBody": {
7575
"description": "List of user object",
7676
"content": {

springdoc-openapi-webmvc-core/src/test/resources/results/app68-3.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
},
1313
"servers": [
1414
{
15-
"url": "http://localhost",
16-
"description": "Generated server url"
15+
"url": "http://toto.v2.com",
16+
"description": "myserver2"
1717
}
1818
],
1919
"tags": [

0 commit comments

Comments
 (0)