You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: src/oas.md
+181-3Lines changed: 181 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -308,22 +308,195 @@ Note that some URI fields are named `url` for historical reasons, but the descri
308
308
309
309
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).
310
310
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
312
312
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.
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
315
465
316
466
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).
317
467
468
+
#### Relative URI References in CommonMark Fields
469
+
318
470
Relative references in CommonMark hyperlinks are resolved in their rendered context, which might differ from the context of the API description.
319
471
320
472
### Relative References in API URLs
321
473
322
474
API endpoints are by definition accessed as locations, and are described by this specification as **_URLs_**.
323
475
324
476
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.
325
479
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.
326
480
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
+
327
500
### Schema
328
501
329
502
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).
342
515
| Field Name | Type | Description |
343
516
| ---- | :----: | ---- |
344
517
| <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. |
345
519
| <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. |
346
520
| <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. |
347
521
| <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).
354
528
355
529
This object MAY be extended with [Specification Extensions](#specification-extensions).
356
530
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
+
357
533
#### Info Object
358
534
359
535
The object provides metadata about the API.
@@ -482,6 +658,8 @@ An object representing a Server.
482
658
483
659
This object MAY be extended with [Specification Extensions](#specification-extensions).
484
660
661
+
See [Examples of API Base URL Determination](#examples-of-api-base-url-determination) for examples of resolving relative server URLs.
0 commit comments