Skip to content

Commit f876fd9

Browse files
committed
Add title property to GroupedOpenApi class for displaying a Human readable group name. Fixes #1596.
1 parent 00744d7 commit f876fd9

File tree

10 files changed

+95
-22
lines changed

10 files changed

+95
-22
lines changed

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.util.Objects;
2525
import java.util.Set;
2626

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

2931
import static org.springdoc.core.Constants.GROUP_NAME_NOT_NULL;
3032

@@ -672,9 +674,15 @@ public static class SwaggerUrl {
672674
/**
673675
* The Name.
674676
*/
675-
@JsonProperty("name")
677+
@JsonIgnore
676678
private String name;
677679

680+
/**
681+
* The Display name.
682+
*/
683+
@JsonProperty("name")
684+
private String displayName;
685+
678686
/**
679687
* Instantiates a new Swagger url.
680688
*/
@@ -686,21 +694,31 @@ public SwaggerUrl() {
686694
*
687695
* @param group the group
688696
* @param url the url
697+
* @param displayName the display name
689698
*/
690-
public SwaggerUrl(String group, String url) {
699+
public SwaggerUrl(String group, String url, String displayName) {
691700
Objects.requireNonNull(group, GROUP_NAME_NOT_NULL);
692701
this.url = url;
693702
this.name = group;
703+
this.displayName = StringUtils.defaultIfEmpty(displayName, this.name);
694704
}
695705

696706
/**
697-
* Instantiates a new Swagger url.
707+
* Gets display name.
698708
*
699-
* @param group the group
709+
* @return the display name
700710
*/
701-
public SwaggerUrl(String group) {
702-
Objects.requireNonNull(group, GROUP_NAME_NOT_NULL);
703-
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;
704722
}
705723

706724
/**

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Objects;
2727

28+
import org.apache.commons.lang3.StringUtils;
2829
import org.springdoc.core.customizers.OpenApiCustomiser;
2930
import org.springdoc.core.customizers.OperationCustomizer;
3031
import org.springdoc.core.filters.OpenApiMethodFilter;
@@ -94,6 +95,11 @@ public class GroupedOpenApi {
9495
*/
9596
private final List<OpenApiMethodFilter> openApiMethodFilters;
9697

98+
/**
99+
* The Display name.
100+
*/
101+
private final String displayName;
102+
97103
/**
98104
* Instantiates a new Grouped open api.
99105
*
@@ -108,6 +114,7 @@ private GroupedOpenApi(Builder builder) {
108114
this.headersToMatch = builder.headersToMatch;
109115
this.packagesToExclude = builder.packagesToExclude;
110116
this.pathsToExclude = builder.pathsToExclude;
117+
this.displayName = StringUtils.defaultIfEmpty(builder.displayName, this.group);
111118
this.openApiCustomisers = Objects.requireNonNull(builder.openApiCustomisers);
112119
this.operationCustomizers = Objects.requireNonNull(builder.operationCustomizers);
113120
this.openApiMethodFilters = Objects.requireNonNull(builder.methodFilters);
@@ -232,6 +239,15 @@ public List<OpenApiMethodFilter> getOpenApiMethodFilters() {
232239
return openApiMethodFilters;
233240
}
234241

242+
/**
243+
* Gets display name.
244+
*
245+
* @return the display name
246+
*/
247+
public String getDisplayName() {
248+
return displayName;
249+
}
250+
235251
/**
236252
* The type Builder.
237253
* @author bnasslahsen
@@ -292,6 +308,11 @@ public static class Builder {
292308
*/
293309
private List<String> consumesToMatch;
294310

311+
/**
312+
* The Display name.
313+
*/
314+
private String displayName;
315+
295316
/**
296317
* Instantiates a new Builder.
297318
*/
@@ -420,6 +441,17 @@ public Builder addOpenApiMethodFilter(OpenApiMethodFilter methodFilter) {
420441
return this;
421442
}
422443

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+
423455
/**
424456
* Build grouped open api.
425457
*

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

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

1084+
/**
1085+
* The Display name.
1086+
*/
1087+
private String displayName;
1088+
10841089
/**
10851090
* Instantiates a new Group config.
10861091
*/
@@ -1098,10 +1103,12 @@ public GroupConfig() {
10981103
* @param producesToMatch the produces to match
10991104
* @param consumesToMatch the consumes to match
11001105
* @param headersToMatch the headers to match
1106+
* @param displayName the display name
11011107
*/
11021108
public GroupConfig(String group, List<String> pathsToMatch, List<String> packagesToScan,
11031109
List<String> packagesToExclude, List<String> pathsToExclude,
1104-
List<String> producesToMatch,List<String> consumesToMatch,List<String> headersToMatch) {
1110+
List<String> producesToMatch, List<String> consumesToMatch, List<String> headersToMatch,
1111+
String displayName) {
11051112
this.pathsToMatch = pathsToMatch;
11061113
this.pathsToExclude = pathsToExclude;
11071114
this.packagesToExclude = packagesToExclude;
@@ -1110,6 +1117,7 @@ public GroupConfig(String group, List<String> pathsToMatch, List<String> package
11101117
this.producesToMatch = producesToMatch;
11111118
this.consumesToMatch = consumesToMatch;
11121119
this.headersToMatch = headersToMatch;
1120+
this.displayName = displayName;
11131121
}
11141122

11151123
/**
@@ -1255,5 +1263,23 @@ public List<String> getProducesToMatch() {
12551263
public void setProducesToMatch(List<String> producesToMatch) {
12561264
this.producesToMatch = producesToMatch;
12571265
}
1266+
1267+
/**
1268+
* Gets display name.
1269+
*
1270+
* @return the display name
1271+
*/
1272+
public String getDisplayName() {
1273+
return displayName;
1274+
}
1275+
1276+
/**
1277+
* Sets display name.
1278+
*
1279+
* @param displayName the display name
1280+
*/
1281+
public void setDisplayName(String displayName) {
1282+
this.displayName = displayName;
1283+
}
12581284
}
12591285
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ public SwaggerUiConfigParameters(SwaggerUiConfigProperties swaggerUiConfig) {
183183
*
184184
* @param group the group
185185
*/
186-
public void addGroup(String group) {
187-
SwaggerUrl swaggerUrl = new SwaggerUrl(group);
186+
public void addGroup(String group, String displayName) {
187+
SwaggerUrl swaggerUrl = new SwaggerUrl(group, null, displayName);
188188
urls.add(swaggerUrl);
189189
}
190190

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

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

434434
}

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

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

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

springdoc-openapi-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-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
@@ -20,16 +20,12 @@
2020

2121
import org.springdoc.core.GroupedOpenApi;
2222

23-
import org.springframework.boot.SpringApplication;
2423
import org.springframework.boot.autoconfigure.SpringBootApplication;
2524
import org.springframework.context.annotation.Bean;
2625

2726
@SpringBootApplication
2827
public class SpringDocTestApp {
2928

30-
public static void main(String[] args) {
31-
SpringApplication.run(SpringDocTestApp.class, args);
32-
}
3329

3430
@Bean
3531
public GroupedOpenApi storeOpenApi() {
@@ -45,6 +41,7 @@ public GroupedOpenApi groupOpenApi() {
4541
String paths[] = { "/pet/**" };
4642
return GroupedOpenApi.builder()
4743
.group("pets")
44+
.displayName("The pets")
4845
.pathsToMatch(paths)
4946
.build();
5047
}

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

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ public MultipleOpenApiResource(List<GroupedOpenApi> groupedOpenApis,
121121
}
122122

123123
@Override
124-
public void afterPropertiesSet() {
124+
public void afterPropertiesSet() {
125125
if (springDocConfigProperties.getApiDocs().isResolveSchemaProperties()) {
126126
OpenApiCustomiser propertiesResolverForSchemaCustomizer = (OpenApiCustomiser) applicationContext.getBean("propertiesResolverForSchema");
127127
this.groupedOpenApis.forEach(groupedOpenApi -> groupedOpenApi.addOpenApiCustomizer(propertiesResolverForSchemaCustomizer));
128128
}
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
}
@@ -152,7 +152,7 @@ private OpenApiResource buildWebMvcOpenApiResource(GroupedOpenApi item) {
152152
Optional.of(item.getOperationCustomizers()),
153153
Optional.of(item.getOpenApiCustomisers()),
154154
Optional.of(item.getOpenApiMethodFilters()),
155-
springDocConfigProperties,springDocProviders
155+
springDocConfigProperties, springDocProviders
156156

157157
);
158158
else
@@ -164,7 +164,7 @@ private OpenApiResource buildWebMvcOpenApiResource(GroupedOpenApi item) {
164164
Optional.of(item.getOperationCustomizers()),
165165
Optional.of(item.getOpenApiCustomisers()),
166166
Optional.of(item.getOpenApiMethodFilters()),
167-
springDocConfigProperties,springDocProviders
167+
springDocConfigProperties, springDocProviders
168168
);
169169
}
170170

0 commit comments

Comments
 (0)