Skip to content

Commit

Permalink
Add title property to GroupedOpenApi class for displaying a Human rea…
Browse files Browse the repository at this point in the history
…dable group name. Fixes #1596.
  • Loading branch information
bnasslahsen committed Apr 6, 2022
1 parent 00744d7 commit f876fd9
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import java.util.Objects;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;

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

Expand Down Expand Up @@ -672,9 +674,15 @@ public static class SwaggerUrl {
/**
* The Name.
*/
@JsonProperty("name")
@JsonIgnore
private String name;

/**
* The Display name.
*/
@JsonProperty("name")
private String displayName;

/**
* Instantiates a new Swagger url.
*/
Expand All @@ -686,21 +694,31 @@ public SwaggerUrl() {
*
* @param group the group
* @param url the url
* @param displayName the display name
*/
public SwaggerUrl(String group, String url) {
public SwaggerUrl(String group, String url, String displayName) {
Objects.requireNonNull(group, GROUP_NAME_NOT_NULL);
this.url = url;
this.name = group;
this.displayName = StringUtils.defaultIfEmpty(displayName, this.name);
}

/**
* Instantiates a new Swagger url.
* Gets display name.
*
* @param group the group
* @return the display name
*/
public SwaggerUrl(String group) {
Objects.requireNonNull(group, GROUP_NAME_NOT_NULL);
this.name = group;
public String getDisplayName() {
return displayName;
}

/**
* Sets display name.
*
* @param displayName the display name
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.customizers.OpenApiCustomiser;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springdoc.core.filters.OpenApiMethodFilter;
Expand Down Expand Up @@ -94,6 +95,11 @@ public class GroupedOpenApi {
*/
private final List<OpenApiMethodFilter> openApiMethodFilters;

/**
* The Display name.
*/
private final String displayName;

/**
* Instantiates a new Grouped open api.
*
Expand All @@ -108,6 +114,7 @@ private GroupedOpenApi(Builder builder) {
this.headersToMatch = builder.headersToMatch;
this.packagesToExclude = builder.packagesToExclude;
this.pathsToExclude = builder.pathsToExclude;
this.displayName = StringUtils.defaultIfEmpty(builder.displayName, this.group);
this.openApiCustomisers = Objects.requireNonNull(builder.openApiCustomisers);
this.operationCustomizers = Objects.requireNonNull(builder.operationCustomizers);
this.openApiMethodFilters = Objects.requireNonNull(builder.methodFilters);
Expand Down Expand Up @@ -232,6 +239,15 @@ public List<OpenApiMethodFilter> getOpenApiMethodFilters() {
return openApiMethodFilters;
}

/**
* Gets display name.
*
* @return the display name
*/
public String getDisplayName() {
return displayName;
}

/**
* The type Builder.
* @author bnasslahsen
Expand Down Expand Up @@ -292,6 +308,11 @@ public static class Builder {
*/
private List<String> consumesToMatch;

/**
* The Display name.
*/
private String displayName;

/**
* Instantiates a new Builder.
*/
Expand Down Expand Up @@ -420,6 +441,17 @@ public Builder addOpenApiMethodFilter(OpenApiMethodFilter methodFilter) {
return this;
}

/**
* Display name builder.
*
* @param displayName the display name
* @return the builder
*/
public Builder displayName(String displayName) {
this.displayName = displayName;
return this;
}

/**
* Build grouped open api.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,11 @@ public static class GroupConfig {
*/
private List<String> consumesToMatch;

/**
* The Display name.
*/
private String displayName;

/**
* Instantiates a new Group config.
*/
Expand All @@ -1098,10 +1103,12 @@ public GroupConfig() {
* @param producesToMatch the produces to match
* @param consumesToMatch the consumes to match
* @param headersToMatch the headers to match
* @param displayName the display name
*/
public GroupConfig(String group, List<String> pathsToMatch, List<String> packagesToScan,
List<String> packagesToExclude, List<String> pathsToExclude,
List<String> producesToMatch,List<String> consumesToMatch,List<String> headersToMatch) {
List<String> producesToMatch, List<String> consumesToMatch, List<String> headersToMatch,
String displayName) {
this.pathsToMatch = pathsToMatch;
this.pathsToExclude = pathsToExclude;
this.packagesToExclude = packagesToExclude;
Expand All @@ -1110,6 +1117,7 @@ public GroupConfig(String group, List<String> pathsToMatch, List<String> package
this.producesToMatch = producesToMatch;
this.consumesToMatch = consumesToMatch;
this.headersToMatch = headersToMatch;
this.displayName = displayName;
}

/**
Expand Down Expand Up @@ -1255,5 +1263,23 @@ public List<String> getProducesToMatch() {
public void setProducesToMatch(List<String> producesToMatch) {
this.producesToMatch = producesToMatch;
}

/**
* Gets display name.
*
* @return the display name
*/
public String getDisplayName() {
return displayName;
}

/**
* Sets display name.
*
* @param displayName the display name
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ public SwaggerUiConfigParameters(SwaggerUiConfigProperties swaggerUiConfig) {
*
* @param group the group
*/
public void addGroup(String group) {
SwaggerUrl swaggerUrl = new SwaggerUrl(group);
public void addGroup(String group, String displayName) {
SwaggerUrl swaggerUrl = new SwaggerUrl(group, null, displayName);
urls.add(swaggerUrl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public void setSyntaxHighlight(SyntaxHighlight syntaxHighlight) {
* @return the set
*/
public Set<SwaggerUrl> cloneUrls(){
return this.urls.stream().map(swaggerUrl -> new SwaggerUrl(swaggerUrl.getName(), swaggerUrl.getUrl())).collect(Collectors.toSet());
return this.urls.stream().map(swaggerUrl -> new SwaggerUrl(swaggerUrl.getName(), swaggerUrl.getUrl(), swaggerUrl.getDisplayName())).collect(Collectors.toSet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public AbstractSwaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringD
}

protected void init() {
springDocConfigProperties.getGroupConfigs().forEach(groupConfig -> swaggerUiConfigParameters.addGroup(groupConfig.getGroup()));
springDocConfigProperties.getGroupConfigs().forEach(groupConfig -> swaggerUiConfigParameters.addGroup(groupConfig.getGroup(), groupConfig.getDisplayName()));
calculateUiRootPath();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void swagger_config_for_multiple_groups() throws Exception {
.andExpect(jsonPath("urls[0].url", equalTo("/v3/api-docs/stores")))
.andExpect(jsonPath("urls[0].name", equalTo("stores")))
.andExpect(jsonPath("urls[1].url", equalTo("/v3/api-docs/pets")))
.andExpect(jsonPath("urls[1].name", equalTo("pets")))
.andExpect(jsonPath("urls[1].name", equalTo("The pets")))
.andExpect(jsonPath("$['urls.primaryName']", equalTo("pets")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@

import org.springdoc.core.GroupedOpenApi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringDocTestApp {

public static void main(String[] args) {
SpringApplication.run(SpringDocTestApp.class, args);
}

@Bean
public GroupedOpenApi storeOpenApi() {
Expand All @@ -45,6 +41,7 @@ public GroupedOpenApi groupOpenApi() {
String paths[] = { "/pet/**" };
return GroupedOpenApi.builder()
.group("pets")
.displayName("The pets")
.pathsToMatch(paths)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void afterPropertiesSet() {
this.groupedOpenApiResources = groupedOpenApis.stream()
.collect(Collectors.toMap(GroupedOpenApi::getGroup, item ->
{
GroupConfig groupConfig = new GroupConfig(item.getGroup(), item.getPathsToMatch(), item.getPackagesToScan(), item.getPackagesToExclude(), item.getPathsToExclude(), item.getProducesToMatch(), item.getConsumesToMatch(),item.getHeadersToMatch());
GroupConfig groupConfig = new GroupConfig(item.getGroup(), item.getPathsToMatch(), item.getPackagesToScan(), item.getPackagesToExclude(), item.getPathsToExclude(), item.getProducesToMatch(), item.getConsumesToMatch(), item.getHeadersToMatch(),item.getDisplayName());
springDocConfigProperties.addGroupConfig(groupConfig);
return buildWebFluxOpenApiResource(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ public MultipleOpenApiResource(List<GroupedOpenApi> groupedOpenApis,
}

@Override
public void afterPropertiesSet() {
public void afterPropertiesSet() {
if (springDocConfigProperties.getApiDocs().isResolveSchemaProperties()) {
OpenApiCustomiser propertiesResolverForSchemaCustomizer = (OpenApiCustomiser) applicationContext.getBean("propertiesResolverForSchema");
this.groupedOpenApis.forEach(groupedOpenApi -> groupedOpenApi.addOpenApiCustomizer(propertiesResolverForSchemaCustomizer));
}
this.groupedOpenApiResources = groupedOpenApis.stream()
.collect(Collectors.toMap(GroupedOpenApi::getGroup, item ->
{
GroupConfig groupConfig = new GroupConfig(item.getGroup(), item.getPathsToMatch(), item.getPackagesToScan(), item.getPackagesToExclude(), item.getPathsToExclude(), item.getProducesToMatch(), item.getConsumesToMatch(), item.getHeadersToMatch());
GroupConfig groupConfig = new GroupConfig(item.getGroup(), item.getPathsToMatch(), item.getPackagesToScan(), item.getPackagesToExclude(), item.getPathsToExclude(), item.getProducesToMatch(), item.getConsumesToMatch(), item.getHeadersToMatch(), item.getDisplayName());
springDocConfigProperties.addGroupConfig(groupConfig);
return buildWebMvcOpenApiResource(item);
}
Expand All @@ -152,7 +152,7 @@ private OpenApiResource buildWebMvcOpenApiResource(GroupedOpenApi item) {
Optional.of(item.getOperationCustomizers()),
Optional.of(item.getOpenApiCustomisers()),
Optional.of(item.getOpenApiMethodFilters()),
springDocConfigProperties,springDocProviders
springDocConfigProperties, springDocProviders

);
else
Expand All @@ -164,7 +164,7 @@ private OpenApiResource buildWebMvcOpenApiResource(GroupedOpenApi item) {
Optional.of(item.getOperationCustomizers()),
Optional.of(item.getOpenApiCustomisers()),
Optional.of(item.getOpenApiMethodFilters()),
springDocConfigProperties,springDocProviders
springDocConfigProperties, springDocProviders
);
}

Expand Down

0 comments on commit f876fd9

Please sign in to comment.