使用 swagger-parser API 解析外部 $ref

Resolve external $ref using swagger-parser API

我正在尝试创建 API 在参数和响应中包含外部文件引用的资源,并尝试使用 swagger 解析这些引用。 (支持导入带有外部引用的 OpenAPI 定义)。

为此,我将 YAML 文件作为文件存档,将有一个主 main.YAML 文件,并从中引用其他文件。

    OpenAPIV3Parser openAPIV3Parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    OpenAPI openAPI = openAPIV3Parser.read(extractedLocation + "/main.yaml", null, options);
    String openAPIContent = Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI);
    APIDefinitionValidationResponse  apiDefinitionValidationResponse  = new APIDefinitionValidationResponse (); 
    apiDefinitionValidationResponse = OASParserUtil.validateAPIDefinition(openAPIContent, returnContent);

我尝试使用此代码片段,但是当 YAML 文件中有 $refapiDefinitionValidationResponse 会抛出错误。如果没有 $ref,则 apiDefinitionValidationResponse 成功并创建 api。

所以我怀疑将数据提供给OASParserUtil.validateAPIDefinition方法有问题(validateAPIDefinition方法没有问题并且已经过验证和测试)

有人可以帮我解决这个问题吗?

生成的 YAML 文件有 extensions{}

调试日志中的错误消息:

attribute info.license.extensions is unexpected
attribute info.extensions is unexpected
attribute components.schemas.ErrorListItem.extensions is unexpected
attribute components.schemas.MenuItem.extensions is unexpected
attribute components.schemas.Order.extensions is unexpected

我可以从错误消息和您的结果 yaml 中看出,转换步骤将一些 extensions: {} 行添加到最终的 yaml 中。

OpenAPI 规范不允许在它抱怨的地方使用 extensions 属性。

看来你的 yaml 序列化太简单了。从 openapi-generator 查看 SerializerUtils 他们有更多的配置。

额外的模块只负责序列化 OpenAPI 对象的有趣部分。