Skip to content

Commit 7bd8a15

Browse files
committed
Add $self for self-identifying documents
This adds `$self` as a way for a document to define its own URI for use in reference targets, and as the base URI for relative URI references in the document. This does not impact the resolution of relative API URLs.
1 parent d198ac3 commit 7bd8a15

File tree

2 files changed

+186
-3
lines changed

2 files changed

+186
-3
lines changed

src/oas.md

Lines changed: 181 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,22 +342,195 @@ Note that some URI fields are named `url` for historical reasons, but the descri
342342

343343
Unless specified otherwise, all fields that are URIs MAY be relative references as defined by [RFC3986](https://tools.ietf.org/html/rfc3986#section-4.2).
344344

345-
Relative references in [Schema Objects](#schema-object), including any that appear as `$id` values, use the nearest parent `$id` as a Base URI, as described by [JSON Schema Specification Draft 2020-12](https://www.ietf.org/archive/id/draft-bhutton-json-schema-01.html#section-8.2).
345+
#### Establishing the Base URI
346346

347-
Relative URI references in other Objects, and in Schema Objects where no parent schema contains an `$id`, MUST be resolved using the referring document's base URI, which is determined in accordance with [[RFC3986]] [Section 5.1.2 – 5.1.4](https://tools.ietf.org/html/rfc3986#section-5.1.2).
348-
In practice, this is usually the retrieval URI of the document, which MAY be determined based on either its current actual location or a user-supplied expected location.
347+
Relative URI references are resolved using the appropriate base URI, which MUST be determined in accordance with [[RFC3986]] [Section 5.1.1 – 5.1.4](https://tools.ietf.org/html/rfc3986#section-5.1.1) and, for Schema objects, [JSON Schema draft 2020-12 Section 8.2](https://www.ietf.org/archive/id/draft-bhutton-json-schema-01.html#section-8.2), as illustrated by the examles below.
348+
349+
The most common base URI source in the absence of the [OpenAPI Object's](#openapi-object) `$self` or the [Schema Object's](#schema-object) `$id` is the retrieval URI.
350+
Implementations MAY support document retrieval, although see the [Security Considerations](#security-considerations) sections for additional guidance.
351+
Even if retrieval is supported, it may be impossible due to network configuration or server unavailability (including the server hosting an older version while a new version is in development), or undesirable due to performance impacts.
352+
Therefore, all implementations SHOULD allow users to provide the intended retrieval URI for each document so that references can be resolved as if retrievals were performed.
353+
354+
##### Examples of Base URI Determination and Reference Resolution
355+
356+
###### Base URI Within Content
357+
358+
A base URI within the resource's content (RFC3986 Section 5.1.1) is the highest-precedence source of a base URI.
359+
For OpenAPI Documents, this source is the OpenAPI Object's `$self` field, while for Schema Objects that contain a `$id`, or are a subschema of a Schema Object containing a `$id`, the source is the `$id` field:
360+
361+
```YAML
362+
openapi: 3.2.0
363+
$self: https://example.com/openapi
364+
info:
365+
title: Example API
366+
version: 1.0
367+
components:
368+
requestBodies:
369+
Foo:
370+
content:
371+
application/json:
372+
schema:
373+
$ref: schemas/foo
374+
schemas:
375+
Foo:
376+
$id: https://example.com/api/schemas/foo
377+
properties:
378+
bar:
379+
$ref: bar
380+
Bar:
381+
$id: https://example.com/api/schemas/bar
382+
type: string
383+
```
384+
385+
In the example above, the `$ref` in the Request Body Object is resolved using `$self` as the base URI, producing `https://example.com/schemas/foo`.
386+
This matches the `$id` at `#/components/schemas/Foo/$id` so it points to that Schema Object.
387+
That Schema Object has a subschema with `$ref: bar`, which is resolved against the `$id` to produce `https://example.com/schemas/bar`, which matches the `$id` at `#/components/schemas/Bar/$id`.
388+
389+
Note that referring to a schema with a JSON Pointer that crosses a Schema Object with a `$id` [is not interoperable](https://www.ietf.org/archive/id/draft-bhutton-json-schema-01.html#name-json-pointer-fragments-and-).
390+
The JSON Schema specification does not address the case of using a pointer _to_ a Schema Object containing an `$id` without crossing into that Schema Object.
391+
Therefore it is RECOMMENDED that OAD authors use `$id` values to reference such schemas rather than JSON Pointers.
392+
393+
Note also that it is impossible for the reference at `#/components/schemas/Foo/properties/bar/$ref` to reference the schema at `#/components/schemas/Bar` using a JSON Pointer, as the JSON Pointer would be resolved relative to `https://example.com/schemas/foo`, not to the OpenAPI Document's base URI from `$self`.
394+
395+
396+
###### Base URI From Encapsulating Entity
397+
398+
If no base URI can be determined within the content, the next location to search is any encapsulating entity (RFC3986 Section 5.1.2).
399+
400+
This is common for Schema Objects encapsulated within an OpenAPI Document.
401+
An example of an OpenAPI Document itself being encapsulated in another entity would be a `multipart/related` archive ([[?RFC2557]]), such as the following `multipart/related; boundary="boundary-example"; type="application/openapi+yaml"` document.
402+
Note that this is purely an example, and support for such multipart documents or any other format that could encapsulate an OpenAPI Document is not a requirement of this specification.
403+
404+
```MULTIPART
405+
--boundary-example
406+
Content-Type: application/openapi+yaml
407+
Content-Location: https://inaccessible-domain.com/api/openapi.yaml
408+
409+
openapi: 3.2.0
410+
info:
411+
title: Example API
412+
version: 1.0
413+
externalDocs:
414+
url: docs.html
415+
components:
416+
requestBodies:
417+
Foo:
418+
content:
419+
application/json:
420+
schema:
421+
$ref: "#/components/api/schemas/Foo"
422+
schemas:
423+
Foo:
424+
properties:
425+
bar:
426+
$ref: schemas/bar
427+
428+
--boundary-example
429+
Content-Type: application/schema+json; schema=https://spec.openapis.org/oas/3.2/schema-base/YYYY-MM-DD
430+
Content-Location: https://example.com/api/schemas/bar
431+
432+
{
433+
"type": "string"
434+
}
435+
436+
--boundary-example
437+
Content-Type: text/html
438+
Content-Location: https://example.com/api/docs.html
439+
440+
<html>
441+
<head>
442+
<title>API Documentation</title>
443+
</head>
444+
<body>
445+
<p>Awesome documentation goes here</p>
446+
</body>
447+
</html>
448+
```
449+
450+
In this example, the URI for each part, which also serves as its base URI, comes from the part's `Content-Location` header as specified by RFC2557.
451+
Since the Schema Object at `#/components/schemas/Foo` does not contain an `$id`, the reference in its subschema uses the OpenAPI Document's base URI, which is taken from the `Content-Location` header of its part within the `multipart/related` format.
452+
The resulting reference to `https://example.com/schemas/bar` matches the `Content-Location` header of the second part, which allows the reference target to be located within the multipart archive.
453+
454+
Similarly, the `url` field of the [External Documentation Object](#external-documentation-object) is resolved against the base URI from `Content-Location`, producing `https://example.com/api/docs.html` which matches the `Content-Location` of the third part.
455+
456+
###### Base URI From the Retrieval URI
457+
458+
If no base URI is provided from either of the previous sources, the next source is the retrieval URI (RFC 3986 Section 5.1.3).
459+
460+
For this example, assume that the YAML OpenAPI Document was retrieved from `https://example.com/api/openapis.yaml` and the JSON Schema document from `https://example.com/api/schemas/foo`
461+
462+
Assume this document was retrieved from `https://example.com/api/openapis.yaml`:
463+
464+
```YAML
465+
openapi: 3.2.0
466+
info:
467+
title: Example API
468+
version: 1.0
469+
components:
470+
requestBodies:
471+
Foo:
472+
content:
473+
application/json:
474+
schema:
475+
$ref: schemas/foo
476+
```
477+
478+
Assume this document was retrieved from `https://example.com/api/schemas/foo`:
479+
480+
```JSON
481+
{
482+
"type": "object",
483+
"properties": {
484+
"bar": {
485+
"type": "string"
486+
}
487+
}
488+
}
489+
```
490+
491+
Resolving the `$ref: schemas/foo` against the retrieval URI of the OpenAPI Document produces `https://example.com/api/schemas/foo`, the retrieval URI of the JSON Schema document.
492+
493+
###### Application-Specific Default Base URI
494+
495+
When constructing an OpenAPI Document in memory that does not have a `$self`, or an encapsulating entity, or a retrieval URI, applications can resolve internal (fragment-only) references by assuming a default base URI (RFC3986 Section 5.1.4).
496+
While this sort of internal resolution an be performed in practice without choosing a base URI, choosing one avoids the need to implement it as a special case.
497+
498+
#### Resolving URI fragments
349499

350500
If a URI contains a fragment identifier, then the fragment should be resolved per the fragment resolution mechanism of the referenced document. If the representation of the referenced document is JSON or YAML, then the fragment identifier SHOULD be interpreted as a JSON-Pointer as per [RFC6901](https://tools.ietf.org/html/rfc6901).
351501

502+
#### Relative URI References in CommonMark Fields
503+
352504
Relative references in CommonMark hyperlinks are resolved in their rendered context, which might differ from the context of the API description.
353505

354506
### Relative References in API URLs
355507

356508
API endpoints are by definition accessed as locations, and are described by this specification as **_URLs_**.
357509

358510
Unless specified otherwise, all fields that are URLs MAY be relative references as defined by [RFC3986](https://tools.ietf.org/html/rfc3986#section-4.2).
511+
512+
Because the API Is a distinct entity from the OpenAPI Document, RFC3986's base URI rules for the OpenAPI Document do not apply.
359513
Unless specified otherwise, relative references are resolved using the URLs defined in the [Server Object](#server-object) as a Base URL. Note that these themselves MAY be relative to the referring document.
360514

515+
#### Examples of API Base URL Determination
516+
517+
Assume a retrieval URI of `https://device1.example.com` for the following OpenAPI Document:
518+
519+
```YAML
520+
openapi: 3.2.0
521+
$self: https://apidescriptions.example.com/foo
522+
info:
523+
title: Example API
524+
version: 1.0
525+
servers:
526+
- url: .
527+
description: The production API on this device
528+
- url: ./test
529+
description: The test API on this device
530+
```
531+
532+
For API URLs, the `$self` field, which identifies the OpenAPI Document, is ignored, and the retrieval URI is used instead. This produces a normalized production URL of `https://device1.example.com`, and a normalized test URL of `https://device1.example.com/test`.
533+
361534
### Schema
362535

363536
This section describes the structure of the OpenAPI Description format.
@@ -376,6 +549,7 @@ This is the root object of the [OpenAPI Description](#openapi-description).
376549
| Field Name | Type | Description |
377550
| ---- | :----: | ---- |
378551
| <a name="oas-version"></a>openapi | `string` | **REQUIRED**. This string MUST be the [version number](#versions) of the OpenAPI Specification that the OpenAPI Document uses. The `openapi` field SHOULD be used by tooling to interpret the OpenAPI Document. This is _not_ related to the API [`info.version`](#info-version) string. |
552+
| <a name="oas-self"></a>$self | `string` | This string MUST be in the form of an absolute URI as defined by [[RFC3986]] [Section 4.3](https://www.rfc-editor.org/rfc/rfc3986#section-4.3). The `$self` field provides the self-assigned URI of this document, which also serves as its base URI in accordance with [[RFC3986]] [Section 5.1.1](https://www.rfc-editor.org/rfc/rfc3986#section-5.1.1). Implementations MUST support identifying the targets of [API description URIs](#relative-references-in-api-description-uris) using the URI defined by this field when it is present. See [Establishing the Base URI](#establishing-the-base-uri) for the base URI behavior when `$self` is absent, and for examples of using `$self` to resolve references. |
379553
| <a name="oas-info"></a>info | [Info Object](#info-object) | **REQUIRED**. Provides metadata about the API. The metadata MAY be used by tooling as required. |
380554
| <a name="oas-json-schema-dialect"></a> jsonSchemaDialect | `string` | The default value for the `$schema` keyword within [Schema Objects](#schema-object) contained within this OAS document. This MUST be in the form of a URI. |
381555
| <a name="oas-servers"></a>servers | [[Server Object](#server-object)] | An array of Server Objects, which provide connectivity information to a target server. If the `servers` field is not provided, or is an empty array, the default value would be a [Server Object](#server-object) with a [url](#server-url) value of `/`. |
@@ -388,6 +562,8 @@ This is the root object of the [OpenAPI Description](#openapi-description).
388562

389563
This object MAY be extended with [Specification Extensions](#specification-extensions).
390564

565+
Implementations MAY choose to support referencing OpenAPI Documents that contain `$self` by another URI such as the retrieval URI, however this behavior is not interoperable and relying on it is NOT RECOMMENDED.
566+
391567
#### Info Object
392568

393569
The object provides metadata about the API.
@@ -516,6 +692,8 @@ An object representing a Server.
516692

517693
This object MAY be extended with [Specification Extensions](#specification-extensions).
518694

695+
See [Examples of API Base URL Determination](#examples-of-api-base-url-determination) for examples of resolving relative server URLs.
696+
519697
##### Server Object Example
520698

521699
A single server would be described as:

src/schemas/validation/schema.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ properties:
88
openapi:
99
type: string
1010
pattern: '^3\.2\.\d+(-.+)?$'
11+
$self:
12+
type: string
13+
format: uri
14+
$comment: MUST NOT contain a fragment
15+
pattern: '^[^#]*$'
1116
info:
1217
$ref: '#/$defs/info'
1318
jsonSchemaDialect:

0 commit comments

Comments
 (0)