有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java开放Api$ref以使用url

我在使用OpenAPIv3$ref调用URL时遇到问题

我编写了一个开放式api v3规范,以便为REST应用程序提供文档。我使用$ref来验证此文档的输入参数。 这些$ref指向url中的json模式

这是apen api文档的一个示例:

(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"
    }

我复制并粘贴了文件。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"
    }
  }
}

在这个改变之后,斯威格抛出了这个错误

“无法解析指针#/properties/profile”

我的问题是。 当模式使用其中一个时,是否可以继续使用#/properties/profile作为id? 如果json模式正在使用其中一个,我如何制作开放api来验证输入?我是否必须使用另一条路径,而不是#/properties/profile


共 (1) 个答案

  1. # 1 楼答案

    请记住,开放式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'