如果Swagger UI是从SwaggerUrl中推导出分组的显示名称,那么springdoc.group-configs[_].display-name的作用是什么?
我在试图理解Springdoc中分组(Groups)与URL之间的关系。
例如,我们可能有如下的Springdoc配置:
springdoc.swagger-ui.urls[0].url=${springdoc.swagger-ui.url}/myapp-api.yaml
springdoc.swagger-ui.urls[0].name=randomName
springdoc.group-configs[0].display-name=MyApp API
springdoc.group-configs[0].group=myapp-api.yaml
springdoc.group-configs[0].paths-to-match=${paths.to.match}
如果没有在任何地方使用,我就不理解 springdoc.group-configs[0].display-name 的用处。它应该在什么地方显示?在UI中,似乎有另一个属性来控制这个。具体来说,SwaggerUrl 对象用于在UI中构造该组的 displayName。下面来自 AbstractSwaggerUiConfigProperties 的片段说明了这一点:
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);
}
因此,最终显示名称只是该URL的名称("randomName" 而不是 "MyApp API"),即 springdoc.swagger-ui.urls[0].name,除非为所有URL设置了一个通用显示名。这种行为似乎有点不寻常,因为对于所有URL只有一个 displayName 属性,这意味着不能为每一个单独配置。
那么,属性 springdoc.group-configs[0].display-name 的作用是什么?在实际应用或显示在哪儿?因为Swagger UI看起来是从 SwaggerUrl 配置中派生分组显示名称的?
更一般地说,URL与 Groups之间的关系是什么?
解决方案
混淆来自Springdoc中的两件事:分组(groups)和Swagger UI的 URL。
1. springdoc.group-configs
这定义了Springdoc自动生成的OpenAPI组。每个组会创建一个类似这样的端点:
/v3/api-docs/{group}
示例:
springdoc.group-configs[0].group=myapp
springdoc.group-configs[0].display-name=MyApp API
springdoc.group-configs[0].paths-to-match=/api/**
2. springdoc.swagger-ui.urls
这手动定义了Swagger UI应该加载哪些OpenAPI规范。
示例:
springdoc.swagger-ui.urls[0].url=/myapp-api.yaml
springdoc.swagger-ui.urls[0].name=randomName
当你手动定义这些时,Springdoc将不再从分组生成URL,因此UI将使用 urls[].name(randomName)而不是 group-configs.display-name。