Skip to content

Commit 5a2fdcb

Browse files
committed
Changes report: Add title property to GroupedOpenApi class for displaying a Human readable group name. Fixes #1596
1 parent 2d7d5ee commit 5a2fdcb

File tree

9 files changed

+112
-43
lines changed

9 files changed

+112
-43
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/models/GroupedOpenApi.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
/*
22
*
33
* *
4+
* * * Copyright 2019-2020 the original author or authors.
45
* * *
5-
* * * * Copyright 2019-2022 the original author or authors.
6-
* * * *
7-
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8-
* * * * you may not use this file except in compliance with the License.
9-
* * * * You may obtain a copy of the License at
10-
* * * *
11-
* * * * https://www.apache.org/licenses/LICENSE-2.0
12-
* * * *
13-
* * * * Unless required by applicable law or agreed to in writing, software
14-
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15-
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16-
* * * * See the License for the specific language governing permissions and
17-
* * * * limitations under the License.
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
189
* * *
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.
1917
* *
2018
*
2119
*/
@@ -27,6 +25,7 @@
2725
import java.util.List;
2826
import java.util.Objects;
2927

28+
import org.apache.commons.lang3.StringUtils;
3029
import org.springdoc.core.customizers.OpenApiCustomizer;
3130
import org.springdoc.core.customizers.OperationCustomizer;
3231
import org.springdoc.core.filters.OpenApiMethodFilter;
@@ -96,6 +95,11 @@ public class GroupedOpenApi {
9695
*/
9796
private final List<OpenApiMethodFilter> openApiMethodFilters;
9897

98+
/**
99+
* The Display name.
100+
*/
101+
private final String displayName;
102+
99103
/**
100104
* Instantiates a new Grouped open api.
101105
*
@@ -110,6 +114,7 @@ private GroupedOpenApi(Builder builder) {
110114
this.headersToMatch = builder.headersToMatch;
111115
this.packagesToExclude = builder.packagesToExclude;
112116
this.pathsToExclude = builder.pathsToExclude;
117+
this.displayName = StringUtils.defaultIfEmpty(builder.displayName, this.group);
113118
this.openApiCustomizers = Objects.requireNonNull(builder.openApiCustomizers);
114119
this.operationCustomizers = Objects.requireNonNull(builder.operationCustomizers);
115120
this.openApiMethodFilters = Objects.requireNonNull(builder.methodFilters);
@@ -234,6 +239,15 @@ public List<OpenApiMethodFilter> getOpenApiMethodFilters() {
234239
return openApiMethodFilters;
235240
}
236241

242+
/**
243+
* Gets display name.
244+
*
245+
* @return the display name
246+
*/
247+
public String getDisplayName() {
248+
return displayName;
249+
}
250+
237251
/**
238252
* The type Builder.
239253
* @author bnasslahsen
@@ -294,6 +308,11 @@ public static class Builder {
294308
*/
295309
private List<String> consumesToMatch;
296310

311+
/**
312+
* The Display name.
313+
*/
314+
private String displayName;
315+
297316
/**
298317
* Instantiates a new Builder.
299318
*/
@@ -422,6 +441,17 @@ public Builder addOpenApiMethodFilter(OpenApiMethodFilter methodFilter) {
422441
return this;
423442
}
424443

444+
/**
445+
* Display name builder.
446+
*
447+
* @param displayName the display name
448+
* @return the builder
449+
*/
450+
public Builder displayName(String displayName) {
451+
this.displayName = displayName;
452+
return this;
453+
}
454+
425455
/**
426456
* Build grouped open api.
427457
*

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/properties/AbstractSwaggerUiConfigProperties.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
/*
22
*
33
* *
4+
* * * Copyright 2019-2020 the original author or authors.
45
* * *
5-
* * * * Copyright 2019-2022 the original author or authors.
6-
* * * *
7-
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8-
* * * * you may not use this file except in compliance with the License.
9-
* * * * You may obtain a copy of the License at
10-
* * * *
11-
* * * * https://www.apache.org/licenses/LICENSE-2.0
12-
* * * *
13-
* * * * Unless required by applicable law or agreed to in writing, software
14-
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15-
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16-
* * * * See the License for the specific language governing permissions and
17-
* * * * limitations under the License.
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
189
* * *
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.
1917
* *
2018
*
2119
*/
@@ -26,7 +24,9 @@
2624
import java.util.Objects;
2725
import java.util.Set;
2826

27+
import com.fasterxml.jackson.annotation.JsonIgnore;
2928
import com.fasterxml.jackson.annotation.JsonProperty;
29+
import org.apache.commons.lang3.StringUtils;
3030

3131
import static org.springdoc.core.utils.Constants.GROUP_NAME_NOT_NULL;
3232

@@ -674,9 +674,15 @@ public static class SwaggerUrl {
674674
/**
675675
* The Name.
676676
*/
677-
@JsonProperty("name")
677+
@JsonIgnore
678678
private String name;
679679

680+
/**
681+
* The Display name.
682+
*/
683+
@JsonProperty("name")
684+
private String displayName;
685+
680686
/**
681687
* Instantiates a new Swagger url.
682688
*/
@@ -688,21 +694,31 @@ public SwaggerUrl() {
688694
*
689695
* @param group the group
690696
* @param url the url
697+
* @param displayName the display name
691698
*/
692-
public SwaggerUrl(String group, String url) {
699+
public SwaggerUrl(String group, String url, String displayName) {
693700
Objects.requireNonNull(group, GROUP_NAME_NOT_NULL);
694701
this.url = url;
695702
this.name = group;
703+
this.displayName = StringUtils.defaultIfEmpty(displayName, this.name);
696704
}
697705

698706
/**
699-
* Instantiates a new Swagger url.
707+
* Gets display name.
700708
*
701-
* @param group the group
709+
* @return the display name
702710
*/
703-
public SwaggerUrl(String group) {
704-
Objects.requireNonNull(group, GROUP_NAME_NOT_NULL);
705-
this.name = group;
711+
public String getDisplayName() {
712+
return displayName;
713+
}
714+
715+
/**
716+
* Sets display name.
717+
*
718+
* @param displayName the display name
719+
*/
720+
public void setDisplayName(String displayName) {
721+
this.displayName = displayName;
706722
}
707723

708724
/**

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,11 @@ public static class GroupConfig {
12471247
*/
12481248
private List<String> consumesToMatch;
12491249

1250+
/**
1251+
* The Display name.
1252+
*/
1253+
private String displayName;
1254+
12501255
/**
12511256
* Instantiates a new Group config.
12521257
*/
@@ -1264,10 +1269,12 @@ public GroupConfig() {
12641269
* @param producesToMatch the produces to match
12651270
* @param consumesToMatch the consumes to match
12661271
* @param headersToMatch the headers to match
1272+
* @param displayName the display name
12671273
*/
12681274
public GroupConfig(String group, List<String> pathsToMatch, List<String> packagesToScan,
12691275
List<String> packagesToExclude, List<String> pathsToExclude,
1270-
List<String> producesToMatch, List<String> consumesToMatch, List<String> headersToMatch) {
1276+
List<String> producesToMatch, List<String> consumesToMatch, List<String> headersToMatch,
1277+
String displayName) {
12711278
this.pathsToMatch = pathsToMatch;
12721279
this.pathsToExclude = pathsToExclude;
12731280
this.packagesToExclude = packagesToExclude;
@@ -1276,6 +1283,7 @@ public GroupConfig(String group, List<String> pathsToMatch, List<String> package
12761283
this.producesToMatch = producesToMatch;
12771284
this.consumesToMatch = consumesToMatch;
12781285
this.headersToMatch = headersToMatch;
1286+
this.displayName=displayName;
12791287
}
12801288

12811289
/**
@@ -1421,5 +1429,23 @@ public List<String> getProducesToMatch() {
14211429
public void setProducesToMatch(List<String> producesToMatch) {
14221430
this.producesToMatch = producesToMatch;
14231431
}
1432+
1433+
/**
1434+
* Gets display name.
1435+
*
1436+
* @return the display name
1437+
*/
1438+
public String getDisplayName() {
1439+
return displayName;
1440+
}
1441+
1442+
/**
1443+
* Sets display name.
1444+
*
1445+
* @param displayName the display name
1446+
*/
1447+
public void setDisplayName(String displayName) {
1448+
this.displayName = displayName;
1449+
}
14241450
}
14251451
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/properties/SwaggerUiConfigProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ public void setSyntaxHighlight(SyntaxHighlight syntaxHighlight) {
432432
* @return the set
433433
*/
434434
public Set<SwaggerUrl> cloneUrls(){
435-
return this.urls.stream().map(swaggerUrl -> new SwaggerUrl(swaggerUrl.getName(), swaggerUrl.getUrl())).collect(Collectors.toSet());
435+
return this.urls.stream().map(swaggerUrl -> new SwaggerUrl(swaggerUrl.getName(), swaggerUrl.getUrl(), swaggerUrl.getDisplayName())).collect(Collectors.toSet());
436436
}
437437

438438
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public AbstractSwaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringD
8888
}
8989

9090
protected void init() {
91-
springDocConfigProperties.getGroupConfigs().forEach(groupConfig -> swaggerUiConfigParameters.addGroup(groupConfig.getGroup()));
91+
springDocConfigProperties.getGroupConfigs().forEach(groupConfig -> swaggerUiConfigParameters.addGroup(groupConfig.getGroup(), groupConfig.getDisplayName()));
9292
calculateUiRootPath();
9393
}
9494

springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/api/MultipleOpenApiResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void afterPropertiesSet() {
129129
this.groupedOpenApiResources = groupedOpenApis.stream()
130130
.collect(Collectors.toMap(GroupedOpenApi::getGroup, item ->
131131
{
132-
GroupConfig groupConfig = new GroupConfig(item.getGroup(), item.getPathsToMatch(), item.getPackagesToScan(), item.getPackagesToExclude(), item.getPathsToExclude(), item.getProducesToMatch(), item.getConsumesToMatch(),item.getHeadersToMatch());
132+
GroupConfig groupConfig = new GroupConfig(item.getGroup(), item.getPathsToMatch(), item.getPackagesToScan(), item.getPackagesToExclude(), item.getPathsToExclude(), item.getProducesToMatch(), item.getConsumesToMatch(), item.getHeadersToMatch(),item.getDisplayName());
133133
springDocConfigProperties.addGroupConfig(groupConfig);
134134
return buildWebFluxOpenApiResource(item);
135135
}

springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/api/MultipleOpenApiResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void afterPropertiesSet() {
129129
this.groupedOpenApiResources = groupedOpenApis.stream()
130130
.collect(Collectors.toMap(GroupedOpenApi::getGroup, item ->
131131
{
132-
GroupConfig groupConfig = new GroupConfig(item.getGroup(), item.getPathsToMatch(), item.getPackagesToScan(), item.getPackagesToExclude(), item.getPathsToExclude(), item.getProducesToMatch(), item.getConsumesToMatch(), item.getHeadersToMatch());
132+
GroupConfig groupConfig = new GroupConfig(item.getGroup(), item.getPathsToMatch(), item.getPackagesToScan(), item.getPackagesToExclude(), item.getPathsToExclude(), item.getProducesToMatch(), item.getConsumesToMatch(), item.getHeadersToMatch(),item.getDisplayName());
133133
springDocConfigProperties.addGroupConfig(groupConfig);
134134
return buildWebMvcOpenApiResource(item);
135135
}

springdoc-openapi-starter-webmvc-ui/src/test/java/test/org/springdoc/ui/app4/SpringDocApp4Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void swagger_config_for_multiple_groups() throws Exception {
4040
.andExpect(jsonPath("urls[0].url", equalTo("/v3/api-docs/stores")))
4141
.andExpect(jsonPath("urls[0].name", equalTo("stores")))
4242
.andExpect(jsonPath("urls[1].url", equalTo("/v3/api-docs/pets")))
43-
.andExpect(jsonPath("urls[1].name", equalTo("pets")))
43+
.andExpect(jsonPath("urls[1].name", equalTo("The pets")))
4444
.andExpect(jsonPath("$['urls.primaryName']", equalTo("pets")));
4545
}
4646
}

springdoc-openapi-starter-webmvc-ui/src/test/java/test/org/springdoc/ui/app4/SpringDocTestApp.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
@SpringBootApplication
2929
public class SpringDocTestApp {
3030

31-
public static void main(String[] args) {
32-
SpringApplication.run(SpringDocTestApp.class, args);
33-
}
34-
3531
@Bean
3632
public GroupedOpenApi storeOpenApi() {
3733
String paths[] = { "/store/**" };
@@ -46,6 +42,7 @@ public GroupedOpenApi groupOpenApi() {
4642
String paths[] = { "/pet/**" };
4743
return GroupedOpenApi.builder()
4844
.group("pets")
45+
.displayName("The pets")
4946
.pathsToMatch(paths)
5047
.build();
5148
}

0 commit comments

Comments
 (0)