打开 Api $ref 以使用 url

Open Api $ref to use url

我在使用 open api v3 $ref 调用 URL

时遇到问题

我写了一个开放的 api v3 规范来为 REST 应用程序提供文档。我正在使用 $ref 来验证本文档的输入参数。 这些 $refs 指向 url.

中的 json 模式

这是 apen api 文档的示例:

(doc.yml)

  /myapi:
      get:
        description: Some Description
        parameters:
          - name: profile
            in: query
            description: Some description
            required: false
            schema:
              $ref: 'http://localhost:8089/refs#/properties/profile'

端点 http://localhost:8089/refs#/properties/profile 正在返回

        "$schema": "http://json-schema.org/draft-07/schema#",
        "type": "object",
        "title": "Some Example",
        "definitions": {},
        "properties": {
            "profile": {
                "default": "",
                "examples": [
                    "1.0.0"
                ],
                "pattern": "^(\d+\.\d+\.\d+)$",
                "type": "string",
                "title": "This is a version",
                "$id": "#/properties/profile"
            }
        },
        "$id": "myid1"
    }

我在 swagger 中复制并粘贴了 doc.yml,输入得到验证,一切正常。

由于端点发生变化 (http://localhost:8089/refs)

我收到了这样的回复:

  "oneOf": [
    {
      "$ref": "id1"
    }
  ],
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "Some Description",
  "type": "object",
  "$id": "someid",
  "definitions": {
    "id1": {
      "description": "Some description",
      "additionalProperties": false,
      "type": "object",
      "title": "Some Example",
      "properties": {
        "profile": {
          "default": "",
          "examples": [
            "1.0.0"
          ],
          "pattern": "^(\d+\.\d+\.\d+)$",
          "type": "string",
          "title": "The Version Schema",
          "$id": "#/properties/profile"
        }
      },
      "$id": "id1"
    }
  }
}

此更改后,Swagger 抛出此错误。

"Could not resolve pointer #/properties/profile"

我的问题是。 当模式使用 oneOf 时,是否可以继续使用 #/properties/profile 作为 id? 如果 json 架构使用的是 oneOf,我该如何打开 api 来验证输入?我是否必须使用其他路径而不是 #/properties/profile?

请记住 Open API 使用它自己的 JSON 架构 。它遗漏了一些东西,添加了一些东西,并修改了一些东西。目前OpenAPI不支持$id/id(参考:https://swagger.io/docs/specification/data-models/keywords/)。好消息是您没有以任何有意义的方式使用 $id,因此将其排除在外并不会改变您的情况。

您的 $ref: 'http://localhost:8089/refs#/properties/profile' 不再有效,因为该路径不再存在。文档的结构发生了变化,因此 JSON 指针片段必须使用新的结构。必须是 $ref: 'http://localhost:8089/refs#/definitions/id1/properties/profile'