Skip to content

Commit cf9ec25

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 e889471 commit cf9ec25

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
@@ -308,22 +308,195 @@ Note that some URI fields are named `url` for historical reasons, but the descri
308308

309309
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).
310310

311-
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).
311+
#### Establishing the Base URI
312312

313-
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).
314-
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.
313+
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.
314+
315+
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.
316+
Implementations MAY support document retrieval, although see the [Security Considerations](#security-considerations) sections for additional guidance.
317+
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.
318+
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.
319+
320+
##### Examples of Base URI Determination and Reference Resolution
321+
322+
###### Base URI Within Content
323+
324+
A base URI within the resource's content (RFC3986 Section 5.1.1) is the highest-precedence source of a base URI.
325+
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:
326+
327+
```YAML
328+
openapi: 3.2.0
329+
$self: https://example.com/openapi
330+
info:
331+
title: Example API
332+
version: 1.0
333+
components:
334+
requestBodies:
335+
Foo:
336+
content:
337+
application/json:
338+
schema:
339+
$ref: schemas/foo
340+
schemas:
341+
Foo:
342+
$id: https://example.com/api/schemas/foo
343+
properties:
344+
bar:
345+
$ref: bar
346+
Bar:
347+
$id: https://example.com/api/schemas/bar
348+
type: string
349+
```
350+
351+
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`.
352+
This matches the `$id` at `#/components/schemas/Foo/$id` so it points to that Schema Object.
353+
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`.
354+
355+
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-).
356+
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.
357+
Therefore it is RECOMMENDED that OAD authors use `$id` values to reference such schemas rather than JSON Pointers.
358+
359+
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`.
360+
361+
362+
###### Base URI From Encapsulating Entity
363+
364+
If no base URI can be determined within the content, the next location to search is any encapsulating entity (RFC3986 Section 5.1.2).
365+
366+
This is common for Schema Objects encapsulated within an OpenAPI Document.
367+
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.
368+
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.
369+
370+
```MULTIPART
371+
--boundary-example
372+
Content-Type: application/openapi+yaml
373+
Content-Location: https://inaccessible-domain.com/api/openapi.yaml
374+
375+
openapi: 3.2.0
376+
info:
377+
title: Example API
378+
version: 1.0
379+
externalDocs:
380+
url: docs.html
381+
components:
382+
requestBodies:
383+
Foo:
384+
content:
385+
application/json:
386+
schema:
387+
$ref: "#/components/api/schemas/Foo"
388+
schemas:
389+
Foo:
390+
properties:
391+
bar:
392+
$ref: schemas/bar
393+
394+
--boundary-example
395+
Content-Type: application/schema+json; schema=https://spec.openapis.org/oas/3.2/schema-base/YYYY-MM-DD
396+
Content-Location: https://example.com/api/schemas/bar
397+
398+
{
399+
"type": "string"
400+
}
401+
402+
--boundary-example
403+
Content-Type: text/html
404+
Content-Location: https://example.com/api/docs.html
405+
406+
<html>
407+
<head>
408+
<title>API Documentation</title>
409+
</head>
410+
<body>
411+
<p>Awesome documentation goes here</p>
412+
</body>
413+
</html>
414+
```
415+
416+
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.
417+
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.
418+
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.
419+
420+
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.
421+
422+
###### Base URI From the Retrieval URI
423+
424+
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).
425+
426+
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`
427+
428+
Assume this document was retrieved from `https://example.com/api/openapis.yaml`:
429+
430+
```YAML
431+
openapi: 3.2.0
432+
info:
433+
title: Example API
434+
version: 1.0
435+
components:
436+
requestBodies:
437+
Foo:
438+
content:
439+
application/json:
440+
schema:
441+
$ref: schemas/foo
442+
```
443+
444+
Assume this document was retrieved from `https://example.com/api/schemas/foo`:
445+
446+
```JSON
447+
{
448+
"type": "object",
449+
"properties": {
450+
"bar": {
451+
"type": "string"
452+
}
453+
}
454+
}
455+
```
456+
457+
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.
458+
459+
###### Application-Specific Default Base URI
460+
461+
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).
462+
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.
463+
464+
#### Resolving URI fragments
315465

316466
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).
317467

468+
#### Relative URI References in CommonMark Fields
469+
318470
Relative references in CommonMark hyperlinks are resolved in their rendered context, which might differ from the context of the API description.
319471

320472
### Relative References in API URLs
321473

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

324476
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).
477+
478+
Because the API Is a distinct entity from the OpenAPI Document, RFC3986's base URI rules for the OpenAPI Document do not apply.
325479
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.
326480

481+
#### Examples of API Base URL Determination
482+
483+
Assume a retrieval URI of `https://device1.example.com` for the following OpenAPI Document:
484+
485+
```YAML
486+
openapi: 3.2.0
487+
$self: https://apidescriptions.example.com/foo
488+
info:
489+
title: Example API
490+
version: 1.0
491+
servers:
492+
- url: .
493+
description: The production API on this device
494+
- url: ./test
495+
description: The test API on this device
496+
```
497+
498+
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`.
499+
327500
### Schema
328501

329502
This section describes the structure of the OpenAPI Description format.
@@ -342,6 +515,7 @@ This is the root object of the [OpenAPI Description](#openapi-description).
342515
| Field Name | Type | Description |
343516
| ---- | :----: | ---- |
344517
| <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. |
518+
| <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. |
345519
| <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. |
346520
| <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. |
347521
| <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 `/`. |
@@ -354,6 +528,8 @@ This is the root object of the [OpenAPI Description](#openapi-description).
354528

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

531+
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.
532+
357533
#### Info Object
358534

359535
The object provides metadata about the API.
@@ -482,6 +658,8 @@ An object representing a Server.
482658

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

661+
See [Examples of API Base URL Determination](#examples-of-api-base-url-determination) for examples of resolving relative server URLs.
662+
485663
##### Server Object Example
486664

487665
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)