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
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -342,22 +342,195 @@ Note that some URI fields are named `url` for historical reasons, but the descri
342
342
343
343
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).
344
344
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
346
346
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.
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
349
499
350
500
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).
351
501
502
+
#### Relative URI References in CommonMark Fields
503
+
352
504
Relative references in CommonMark hyperlinks are resolved in their rendered context, which might differ from the context of the API description.
353
505
354
506
### Relative References in API URLs
355
507
356
508
API endpoints are by definition accessed as locations, and are described by this specification as **_URLs_**.
357
509
358
510
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.
359
513
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.
360
514
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
+
361
534
### Schema
362
535
363
536
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).
376
549
| Field Name | Type | Description |
377
550
| ---- | :----: | ---- |
378
551
| <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. |
379
553
| <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. |
380
554
| <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. |
381
555
| <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).
388
562
389
563
This object MAY be extended with [Specification Extensions](#specification-extensions).
390
564
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
+
391
567
#### Info Object
392
568
393
569
The object provides metadata about the API.
@@ -516,6 +692,8 @@ An object representing a Server.
516
692
517
693
This object MAY be extended with [Specification Extensions](#specification-extensions).
518
694
695
+
See [Examples of API Base URL Determination](#examples-of-api-base-url-determination) for examples of resolving relative server URLs.
0 commit comments