ruby-on-rails 对其他Open API 3.0.2规范文件使用$ref会引发错误

8hhllhi2  于 2023-03-20  发布在  Ruby
关注(0)|答案(1)|浏览(133)

虽然我很精通JSON Schema规范,但我正在努力过渡到使用Open API 3.0.2规范,我在尝试干燥和组织我的定义时遇到了一个奇怪的问题。我试图将$ref用于嵌套文件夹中的其他json结构,但遇到了一个错误。我确信我缺少了一个规则或路径字符。我应该在信息块下添加$ref,看起来工作正常,它只是型号$ref
谢谢你的帮助!
我一直在遵循这些指南

我正在使用committee gem测试这些模式,错误为x1c 0d1x
已尝试将"$ref": "models/user.json" DRY解压缩到子文件夹

{
  "type": "object",
  "description": "A user account.",
  "required": [
    "id",
    "email"
  ],
  "additionalProperties": false,
  "properties": {
    "id": {
      "type": "integer",
      "description": "Unique identifier for the given record."
    },
    "email": {
      "type": "string",
      "description": "The email associated to the given user."
    },
    "first_name": {
      "type": "string",
      "description": "User's first name."
    },
    "last_name": {
      "type": "string",
      "description": "User's last name."
    },
    "full_name": {
      "type": "string",
      "description": "Full user name"
    }
  }
}

如何设置文件夹结构

common/
models/
requests/
api.json

api.json文件

{
  "openapi": "3.0.2",
  "components": {
    "schemas": {
      "error": {
        "properties": {
          "code": {
            "type": "string",
            "description": "Machine readable code for this specific error message."
          },
          "message": {
            "type": "string",
            "description": "Description of the error."
          }
        },
        "required": [
          "code",
          "message"
        ],
        "type": "object"
      },
      "user": {

        "type": "object",
        "description": "A user account.",
        "required": [
          "id",
          "email"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "integer",
            "description": "Unique identifier for the given record."
          },
          "email": {
            "type": "string",
            "description": "The email associated to the given user."
          },
          "first_name": {
            "type": "string",
            "description": "User's first name."
          },
          "last_name": {
            "type": "string",
            "description": "User's last name."
          },
          "full_name": {
            "type": "string",
            "description": "Full user name"
          }
        }

      },
      "user_input": {
        "properties": {
          "email": {
            "type": "string"
          },
          "first_name": {
            "type": "string",
            "description": "User's first name."
          },
          "last_name": {
            "type": "string",
            "description": "User's last name."
          }
        },
        "required": [
          "email"
        ],
        "type": "object"
      }
    }
  },
  "info": { "$ref": "common/info.json" },
  "paths": {
    "/users": {
      "get": {
        "description": "Returns all the users in the system.",
        "operationId": "get--users",
        "parameters": [],
        "responses": {
          "200": {
            "description": "[com.test.test]",
            "content": {
              "application/json": {
                "schema": {
                  "items": {
                    "$ref": "models/user.json"
                  },
                  "type": "array"
                }
              }
            },
            "headers": {}
          }
        },
        "tags": [
          "user"
        ]
      },
      "post": {
        "description": "Creates a new user.",
        "operationId": "post--users",
        "parameters": [],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/user_input"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "com.test.test",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "models/user.json"
                }
              }
            },
            "headers": {}
          },
          "409": {
            "description": "[com.test.test]",
            "content": {
              "application/json": {
                "schema": {
                  "items": {
                    "$ref": "#/components/schemas/error"
                  },
                  "type": "array"
                }
              }
            },
            "headers": {}
          }
        },
        "tags": [
          "user"
        ]
      }
    },
    "/users/{id}": {
      "get": {
        "description": "Returns the given user.",
        "operationId": "get--users-id",
        "parameters": [
          {
            "deprecated": false,
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            },
            "name": "id"
          }
        ],
        "responses": {
          "200": {
            "description": "com.test.test",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "models/user.json"
                }
              }
            },
            "headers": {}
          },
          "404": {
            "description": "unit",
            "content": {
              "application/json": {
                "schema": {
                  "type": "integer",
                  "nullable": true
                }
              }
            },
            "headers": {}
          }
        },
        "tags": [
          "user"
        ]
      }
    }
  },
  "servers": [
    {
      "url": "http://bobs.ngrok.io"
    }
  ]
}
7z5jn7bk

7z5jn7bk1#

不能使用$ref来指向info对象,它必须是内联的。

相关问题