首页 > 解决方案 > 在 Swagger 上钻取模型对象

问题描述

我的应用程序是一个 restful api,它与 Swagger 和 OpenAPI 集成。我已经使用 OpenAPI YAML 文件生成了所有 Java 存根,一切正常。但是,当我尝试在 Swagger 上向下钻取模型对象时,它无法找到某些对象,尽管项目的一部分编译得很好。

如下图所示,向下钻取无法找到配置对象。

关于如何解决这个问题的任何想法。

在此处输入图像描述

编辑:

我有一个安静的网络服务,我使用 openapi-generator 插件从 YAML 文件生成所有 java 存根 [数据传输对象]。该插件会自动生成一个类 OpenAPIDocumentationConfig,以下是该类的详细信息。完成此设置后,模型会在 Swagger UI 中自动生成。还想补充一点,我正在使用 OpenAPI 3.0,但我需要将对象定义拆分为多个文件。所以我使用定义来指代它们,因为我不相信组件模式可以拆分为多个文件。

@Configuration
@EnableSwagger2
public class OpenAPIDocumentationConfig {

ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("ABC Service")
        .description("ABC Service")
        .license("")
        .licenseUrl("http://unlicense.org")
        .termsOfServiceUrl("")
        .version("1.0.0")
        .contact(new Contact("","", "xyz@abc.com"))
        .build();
}

@Bean
public Docket customImplementation(ServletContext servletContext, @Value("${openapi.studioVALService.base-path:}") String basePath) {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
                .apis(RequestHandlerSelectors.basePackage("com.x.y.z"))
                .build()
            .pathProvider(new BasePathAwareRelativePathProvider(servletContext, basePath))
            .directModelSubstitute(java.time.LocalDate.class, java.sql.Date.class)
            .directModelSubstitute(java.time.OffsetDateTime.class, java.util.Date.class)
            .apiInfo(apiInfo());
}

class BasePathAwareRelativePathProvider extends RelativePathProvider {
    private String basePath;

    public BasePathAwareRelativePathProvider(ServletContext servletContext, String basePath) {
        super(servletContext);
        this.basePath = basePath;
    }

    @Override
    protected String applicationPath() {
        return  Paths.removeAdjacentForwardSlashes(UriComponentsBuilder.fromPath(super.applicationPath()).path(basePath).build().toString());
    }

    @Override
    public String getOperationPath(String operationPath) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath("/");
        return Paths.removeAdjacentForwardSlashes(
                uriComponentsBuilder.path(operationPath.replaceFirst("^" + basePath, "")).build().toString());
    }
}

}

编辑2:

我将所有定义移至组件和模式,但它们仍然拆分为多个文件,并且跨文件引用组件,但我仍然得到相同的错误。

标签: restswaggeropenapi

解决方案


如果您使用的是 OpenAPI 3,您应该将想要重用的模式放入组件中。要引用它,您必须使用以下引用:

$ref: "#/components/schemas/EquityOptionConfigurationDO"

推荐阅读