Swagger throws "Could not resolve reference"

11,233

The components section needs to be on the top level of the JSON file like this:

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

The error was that components were inside paths rather than on the top level.

Share:
11,233

Related videos on Youtube

Mark
Author by

Mark

Updated on June 04, 2022

Comments

  • Mark
    Mark almost 2 years

    As a part of my learning journey (with TestDriven.io) I'm learning about Swagger or OpenApi. I'm getting this error from Swagger: Could not resolve reference: Could not resolve pointer: /components/schemas/user does not exist in document that reference is clearly in the json file so why is it complaining ? (this text here is to pass site's minimum character limit in the post and has no relevance to the question itself)

    {
      "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"
                }
              }
            }
          }
        }
      }
    }
    
  • Daniel
    Daniel about 2 years
    do you have some documentation to reference your answer? I am having a similar issue here: stackoverflow.com/questions/71443870/…