[英]Swagger/Openapi - How to document imported modules in a Spring boot project?
我有一个身份验证模块,它被导入到我们的项目中,以提供与身份验证相关的 API。
AppConfig.java
@Configuration
@ComponentScan({"com.my.package.ldap.security"})
@EnableCaching
@EnableRetry
public class ApplicationConfig {
...
}
我已经在我的项目中配置了 Swagger/OpenAPI,我希望找到一种方法来管理这些导入的端点:
具体来说,我希望在 Example 对象的字段上设置顺序。 现在它默认按字母顺序排序。 这样做的原因是因为很多这些字段是“可选的”,我们每次都必须从示例对象中删除这些字段,以便对用户进行身份验证,这是浪费时间。
我试过用@JsonPropertyOrder
注释对象,但它没有改变:
@JsonPropertyOrder({
"domain",
"username",
"password"
})
有没有办法做到这一点?
我做了一个小的 POC。 它不是很漂亮或非常可扩展,但它确实按预期工作。 也许可以使其更灵活,重新使用元数据对象上的属性位置,但此示例不包括这一点。 通过这种方式,您可以循环定义和模型,手动完成框架目前无法完成的工作。
另外,请确保不要让它太重,因为每次有人打开 swagger 文档时都会执行它。 它是一个转换原始 Swagger API 定义结构的中间件。 它不会改变原来的。
@Order(SWAGGER_PLUGIN_ORDER)
public class PropertyOrderTransformationFilter implements WebMvcSwaggerTransformationFilter {
@Override
public Swagger transform(final SwaggerTransformationContext<HttpServletRequest> context) {
Swagger swagger = context.getSpecification();
Model model = swagger.getDefinitions().get("applicationUserDetails");
Map<String, Property> modelProperties = model.getProperties();
// Keep a reference to the property definitions
Property domainPropertyRef = modelProperties.get("domain");
Property usernamePropertyRef = modelProperties.get("username");
Property passwordPropertyRef = modelProperties.get("password");
// Remove all entries from the underlying linkedHashMap
modelProperties.clear();
// Add your own keys in a specific order
Map<String, Property> orderedPropertyMap = new LinkedHashMap<>();
orderedPropertyMap.put("domain", domainPropertyRef);
orderedPropertyMap.put("username", usernamePropertyRef);
orderedPropertyMap.put("password", passwordPropertyRef);
orderedPropertyMap.put("..rest..", otherPropertyRef);
model.setProperties(orderedPropertyMap);
return swagger;
}
@Override
public boolean supports(final DocumentationType documentationType) {
return SWAGGER_2.equals(documentationType);
}
}
@Configuration
class SwaggerConf {
@Bean
public PropertyOrderTransformationFilter propertyOrderTransformationFilter () {
return new PropertyOrderTransformationFilter ();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.