Skip to content

Question: $id keyword and dereferencing #54

@char0n

Description

@char0n

Hello everybody,

I have a question related to $id keyword and deferencing. Let's say I have the following OpenAPI 3.1 definition containing pair of top-level JSON Schema Object - User and UserProfile. On the following definition I'm using $anchor to reference UserProfile from User.profile property.

Using $anchor:

{
  "openapi": "3.1.0",
  "components": {
    "schemas": {
      "User": {
        "type": "object",
        "properties": {
          "login": {
            "type": "string"
          },
          "password": {
            "type": "string"
          },
          "profile": {
            "$ref": "#user-profile"
          }
        }
      },
      "UserProfile": {
        "$anchor": "user-profile",
        "properties": {
          "firstName": {
            "type": "string"
          },
          "lastName": {
            "type": "string"
          }
        }
      }
    }
  }
}

Now to my question; if I understand the JSON Spec correctly I can use $id keyword to achieve the same as $anchor?

Using $id:

{
  "openapi": "3.1.0",
  "components": {
    "schemas": {
      "User": {
        "type": "object",
        "properties": {
          "login": {
            "type": "string"
          },
          "password": {
            "type": "string"
          },
          "profile": {
            "$ref": "https://very-arbitrary-url.com/path"
          }
        }
      },
      "UserProfile": {
        "$id": "https://very-arbitrary-url.com/path",
        "properties": {
          "firstName": {
            "type": "string"
          },
          "lastName": {
            "type": "string"
          }
        }
      }
    }
  }
}

When dereferencing this definition, we first search for JSON Schema identified with $id=https://very-arbitrary-url.com/path, and only if not found in current document, we resolve the URL(https://very-arbitrary-url.com/path) and assume that the JSON Schema is the result of fetching this URL?

Thank you very much!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions