Swagger/OpenAPI-如何在Spring Boot项目中记录导入的模块?

发布于 2025-02-08 19:30:58 字数 744 浏览 2 评论 0原文

我有一个身份验证模块,该模块在我们的项目中导入以提供与身份验证相关的API。

appconfig.java

@Configuration
@ComponentScan({"com.my.package.ldap.security"})
@EnableCaching
@EnableRetry
public class ApplicationConfig {
...
}

我在项目中配置了Swagger/OpenAPI,我希望找到一种管理这些导入的端点的方法:

”

具体而言,我希望在示例对象的字段。现在,默认情况下将其按字母顺序排序。 这样做的原因是因为许多这些字段都是“可选的”,我们必须每次从示例对象中删除这些字段,以便对用户进行浪费的时间来验证。

我已经尝试使用@jsonpropertyorder注释对象,但没有任何更改:

@JsonPropertyOrder({
    "domain",
    "username",
    "password"
})

有什么方法可以实现这一目标吗?

I have an authentication module which is imported inside our projects to provide authentication related APIs.

AppConfig.java

@Configuration
@ComponentScan({"com.my.package.ldap.security"})
@EnableCaching
@EnableRetry
public class ApplicationConfig {
...
}

I've configured Swagger/OpenAPI in my projects and I wish to find a way to manage these imported endpoints:

enter image description here

Specifically, I wish to set an order on the Example object's fields. Right now it is sorted alphabetically by default.
The reason for doing that is because a lot of these fields are "optional" and we have to remove these fields every time from the example object in order to authenticate a user which is a waste of time.

I've tried annotating the Object with @JsonPropertyOrder but it makes no change:

@JsonPropertyOrder({
    "domain",
    "username",
    "password"
})

Is there any way to achieve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2025-02-15 19:30:58

我做了一个小POC。它不是很漂亮或非常扩展,但确实可以按预期工作。也许可以使其更灵活,重新使用元数据对象上的属性位置,但是此示例不包括在内。这样,您可以循环定义和模型,手动完成该框架目前无法做的工作。

另外,请确保不要使它变得太重,因为每当有人打开摇摇晃晃的文档时,它都会被执行。这是一块中间件,可以改变原始的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 ();
  }
}

I made a small POC. It isn't pretty or very extendible, but it does work as intended. Perhaps one could make it more flexible, re-using the property position on the metadata object, but this example does not include that. This way you can loop definitions and models, manually doing the work that the framework fails to do at the moment.

Also, be sure not to make this too heavy because it will be executed every time someone opens up the swagger documentation. It's a piece of middleware that transforms the original Swagger API definition structure. It does not change the original one.


@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 ();
  }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文