【问题标题】:Weird "Could not resolve reference: undefined Not Found" messages in Swagger UI 3Swagger UI 3 中奇怪的“无法解析参考:未定义未找到”消息
【发布时间】:2021-04-11 12:17:56
【问题描述】:

在将 Alfresco 的 REST API Explorer 从 Swagger UI 2 迁移到 Swagger UI 3 (3.38.0) 时,单个 API 定义引发了两个无法解析引用:未找到未定义 错误:

paths./search.post.parameters.0.schema.properties.pivots.items.properties.pivots.items.$ref

paths./search.post.responses.200.schema.properties.list.properties.context.properties.request.properties.pivots.items.properties.pivots.items.$ref
  1. 所有 API 定义在 Swagger UI 2 中都可以正常工作
  2. 所有 API 定义,但在 Swagger UI 3 中都可以正常工作
  3. this API definition 的 YAML 在结构上似乎与 the other API definitions 的 YAML 相同
  4. Swagger 验证器告诉我 YAML 有效:

我经历了很多不同的 StackOverflow Q&A 和 GitHub 问题,并带有类似的错误消息,但它们大多与 YAML 无效或 $ref 的兄弟姐妹不受支持有关,而这里似乎并非如此.

这是 Swagger UI 3 的误报,还是 API 定义本身有问题?

我可以做些什么来避免收到这些消息吗?




如果有人想要 SSCCE:

然后选择Search API 定义并单击带有/search API 的绿色行:

【问题讨论】:

  • 解析器有时会因深度嵌套的模式、长 allOf+$ref 链和/或循环引用而窒息。以下是 Swagger 存储库中的类似问题:github.com/swagger-api/swagger-js/issues/1570github.com/swagger-api/swagger-ui/issues/5726github.com/swagger-api/swagger-ui/issues/5820。很可能您的定义很好,但您看到了其中一个错误。
  • 谢谢你 Helen... 这是我的感觉,但我想要一些专家的意见。如果您将您的评论变成答案,我会接受并在到期前分配赏金。
  • 顺便说一下,在打开每个页面时,API 定义都会通过 GET 正确检索。仅在此页面中且仅在触发此错误后,GET 为两个,通常的一个以 304 结尾,一个无效的以 404 结尾(它在 URL 中有 .../definitions/definitions/... 而不是只有 .../definitions/...)。这是为什么呢?
  • 这是解析器错误的一部分/症状。

标签: rest yaml swagger swagger-ui swagger-2.0


【解决方案1】:

您的 API 定义很好。此错误是 Swagger UI 的 $ref 解析器的错误/限制 - 有时它会在长 $ref + allOf/oneOf/anyOf 链、递归模式、循环引用或它们的组合上失败。

在您的示例 (alfresco-search.yaml) 中,错误是由 RequestPivot 架构中的递归触发的:

  RequestPivot:
    description: A list of pivots.
    type: object
    properties:
      key:
        description: A key corresponding to a matching field facet label or stats.
        type: string
      pivots:
        type: array
        items:
          $ref: '#/definitions/RequestPivot'

您可以跟踪 Swagger 存储库中的相关问题:
https://github.com/swagger-api/swagger-js/issues/1570
https://github.com/swagger-api/swagger-ui/issues/5726
https://github.com/swagger-api/swagger-ui/issues/5820


与此同时,您可以隐藏红色的“错误”块 - 或者通过在您的 index.html 中添加一段 CSS 代码

.errors-wrapper {
    display: none !important;
}

或使用this plugin 完全删除“错误”块(即不将其添加到 DOM)。插件代码需要在SwaggerUIBundle构造函数之前添加,然后插件名称需要包含在构造函数的plugins列表中。

// index.html

<script>
window.onload = function() {

  // hide the entire Errors container
  const HideAllErrorsPlugin = () => {
    return {
      wrapComponents: {
        errors: () => () => null
      }
    }
  }

  const ui = SwaggerUIBundle({
    urls: ...,
    ...
    plugins: [
      HideAllErrorsPlugin,                // <---------------
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    ...
  })

}

我还建议将自定义 example 添加到 RequestPivot 架构和/或 SearchRequest.pivots 属性以修复/解决 @ 中自动生成的请求/响应示例中的 "pivots": [null] 值987654342@.

  RequestPivot:
    description: A list of pivots.
    type: object
    ...
    example:     # <--- Custom example
      key: MyKey
      pivots:
        - key: AnotherKey
          pivots: []

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多