rest - 在 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:
我将所有定义移至组件和模式,但它们仍然拆分为多个文件,并且跨文件引用组件,但我仍然得到相同的错误。
解决方案
如果您使用的是 OpenAPI 3,您应该将想要重用的模式放入组件中。要引用它,您必须使用以下引用:
$ref: "#/components/schemas/EquityOptionConfigurationDO"
推荐阅读
- python - 将包含 Macro-B 的 Fanuc g 代码转换为基本语法
- c# - 处理异步异常
- oracle-adf - 将应用程序部署到开源服务器
- javascript - 如何隐藏日期时间 x 轴但仍然能够在 Highcharts 甘特图中显示绘图线?
- highcharts - Highcharts:Y轴最大值应该大于数据点
- ios - 带有数字键盘的 Flutter TextField,需要逗号而不是句点(仅限 iOS)
- redis - 为什么 Redis 中键的编码是“int”,而 lua 脚本中的类型是“字符串”?
- php - 为 Apache 服务器设置多个索引文件
- java - 如何停止使用相同连接连续执行while循环
- java - 如何从excel表中的几列中删除过滤器,而不是使用java apache poi的所有列