【问题标题】:Swagger throws "Could not resolve reference"Swagger 抛出“无法解析参考”
【发布时间】:2020-03-10 02:16:11
【问题描述】:

作为我学习之旅的一部分(使用 TestDriven.io),我正在学习 Swagger 或 OpenApi。我从 Swagger 那里得到这个错误:Could not resolve reference: Could not resolve pointer: /components/schemas/user does not exist in document 那个引用显然在 json 文件中,为什么它会抱怨呢? (这里的文字是为了在帖子中通过站点的最小字符限制,与问题本身无关)

{
  "openapi": "3.0.2",
  "info": {
    "version": "0.0.1",
    "title": "Users Service",
    "description": "Swagger spec for documenting the users service"
  },
  "servers": [
    {
      "url": "http://localhost"
    }
  ],
  "paths": {
    "/users/ping": {
      "get": {
        "summary": "Just a sanity check",
        "responses": {
          "200": {
            "description": "Will return 'pong!'"
          }
        }
      }
    },
    "/users": {
      "get": {
        "summary": "Returns all users",
        "responses": {
          "200": {
            "description": "user object"
          }
        }
      }
    },
    "/users/{id}": {
      "get": {
        "summary": "Returns a user based on a single user ID",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "ID of user to fetch",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "user object"
          }
        }
      }
    },
    "/auth/register": {
      "post": {
        "summary": "Creates a new user",
        "requestBody": {
          "description": "User to add",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/user-full"
              }
            }
          }
        }
      },

      "responses": {
        "200": {
          "description": "user object"
        }
      }
    },

    "/auth/login": {
      "post": {
        "summary": "Logs a user in",
        "requestBody": {
          "description": "User to log in",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/user"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Successfully logged in"
          }
        }
      }
    },
    "/auth/status": {
      "get": {
        "summary": "Returns the logged in user's status",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "user object"
          }
        }
      }
    },
    "/auth/logout": {
      "get": {
        "summary": "Logs a user out",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Successfully logged out"
          }
        }
      }
    },
    "components": {
      "securitySchemes": {
        "bearerAuth": {
          "type": "http",
          "scheme": "bearer"
        }
      },

      "schemas": {
        "user": {
          "properties": {
            "email": {
              "type": "string"
            },
            "password": {
              "type": "string"
            }
          }
        },
        "user-full": {
          "properties": {
            "username": {
              "type": "string"
            },
            "email": {
              "type": "string"
            },
            "password": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: swagger


    【解决方案1】:

    components 部分需要位于 JSON 文件的顶层,如下所示:

    {
      "openapi": "3.0.2",
      "components": {
        "securitySchemes": {
          "bearerAuth": {
            "type": "http",
            "scheme": "bearer"
          }
        },
        ...
      },
      ...
    }
    

    错误在于componentspaths 内部,而不是在顶层。

    【讨论】:

    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多