OAS 3 This page applies to OpenAPI 3 – the latest version of the OpenAPI Specification.
When you document an API, it is common to have some features which you use across several of API resources. In that case, you can create a snippet for such elements in order to use them multiple times when you need it. With OpenAPI 3.0, you can reference a definition hosted on any location. It can be the same server, or another one – for example, GitHub, SwaggerHub, and so on. To reference a definition, use the
$ref: 'reference to definition'
For example, suppose you have the following schema object, which you want to use inside your response:
To refer that object, you need to add
with the corresponding path to your response:
"description": "The response",
description: The response
The value of
uses the JSON Reference
notation, and the portion starting with
uses the JSON Pointer
notation. This notation lets you specify the target file or a specific part of a file you want to reference. In the previous example,
means the resolving starts from the root of the current document, and then finds the values of
one after another.
According to RFC3986
string value (JSON Reference
) should contain a URI, which identifies the location of the JSON value you are referencing to. If the string value does not conform URI syntax rules, it causes an error during the resolving. Any members other than
in a JSON Reference object are ignored. Check this list for example values of a JSON reference in specific cases:
- Local Reference –
# means go to the root of the current document and then find elements
myElement one after one.
- Remote Reference –
$ref: 'document.json' Uses the whole document located on the same server and in the same location.
- The element of the document located on the same server –
- The element of the document located in the parent folder –
- The element of the document located in another folder –
- URL Reference –
$ref: 'http://path/to/your/resource' Uses the whole document located on the different server.
- The specific element of the document stored on the different server –
- The document on the different server, which uses the same protocol (for example, HTTP or HTTPS) –
: When using local references such as
in YAML, enclose the value in quotes:
. Otherwise it will be treated as a comment.
are special characters in JSON Pointers, and need to be escaped when used literally (for example, in path names).
For example, to refer to the path
, you would use:
Places Where $ref Can Be Used
A common misconception is that
is allowed anywhere in an OpenAPI specification file. Actually
is only allowed in places where the OpenAPI 3.0 Specification
explicitly states that the value may be a reference
. For example,
cannot be used in the
section and directly under
However, you can
individual paths, like so:
$ref and Sibling Elements
Any sibling elements of a
are ignored. This is because
works by replacing itself and everything on its level with the definition it is pointing at. Consider this example:
description: Date schema extended with a `default` value... Or not?
In the second schema, the
properties are ignored, so this schema ends up exactly the same as the referenced
Did not find what you were looking for? Ask the community
Found a mistake? Let us know