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
Copy file name to clipboardExpand all lines: src/oas.md
+255-3Lines changed: 255 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -343,22 +343,53 @@ Note that some URI fields are named `url` for historical reasons, but the descri
343
343
344
344
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).
345
345
346
-
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).
346
+
#### Establishing the Base URI
347
347
348
-
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).
349
-
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.
348
+
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 in [Appendix G: Examples of Base URI Determination and Reference Resolution](#appendix-g-examples-of-base-uri-determination-and-reference-resolution).
349
+
350
+
If `$self` is a relative URI-reference, it is resolved agains the next possible base URI source ([[RFC3986]][Section 5.1.2 – 5.1.4](https://tools.ietf.org/html/rfc3986#section-5.1.2)) before being used for the resolution of other relative URI-references.
351
+
352
+
The most common base URI source that is used in the event of a missing or relative `$self` (in the [OpenAPI Object](#openapi-object)) and (for [Schema Object](#schema-object)) `$id` is the retrieval URI.
353
+
Implementations MAY support document retrieval, although see the [Security Considerations](#security-considerations) sections for additional guidance.
354
+
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.
355
+
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.
356
+
357
+
#### Resolving URI fragments
350
358
351
359
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).
352
360
361
+
#### Relative URI References in CommonMark Fields
362
+
353
363
Relative references in CommonMark hyperlinks are resolved in their rendered context, which might differ from the context of the API description.
354
364
355
365
### Relative References in API URLs
356
366
357
367
API endpoints are by definition accessed as locations, and are described by this specification as **_URLs_**.
358
368
359
369
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).
370
+
371
+
Because the API Is a distinct entity from the OpenAPI Document, RFC3986's base URI rules for the OpenAPI Document do not apply.
360
372
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.
361
373
374
+
#### Examples of API Base URL Determination
375
+
376
+
Assume a retrieval URI of `https://device1.example.com` for the following OpenAPI Document:
377
+
378
+
```YAML
379
+
openapi: 3.2.0
380
+
$self: https://apidescriptions.example.com/foo
381
+
info:
382
+
title: Example API
383
+
version: 1.0
384
+
servers:
385
+
- url: .
386
+
description: The production API on this device
387
+
- url: ./test
388
+
description: The test API on this device
389
+
```
390
+
391
+
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`.
392
+
362
393
### Schema
363
394
364
395
This section describes the structure of the OpenAPI Description format.
@@ -377,6 +408,7 @@ This is the root object of the [OpenAPI Description](#openapi-description).
377
408
| Field Name | Type | Description |
378
409
| ---- | :----: | ---- |
379
410
| <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. |
411
+
| <a name="oas-self"></a>$self | `string` | This string MUST be in the form of a URI-reference as defined by [[RFC3986]] [Section 4.1](https://www.rfc-editor.org/rfc/rfc3986#section-4.1). 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 or relative, and see [Appendix G]((#appendix-g-examples-of-base-uri-determination-and-reference-resolution)) for examples of using `$self` to resolve references. |
380
412
| <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. |
381
413
| <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. |
382
414
| <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 `/`. |
@@ -389,6 +421,9 @@ This is the root object of the [OpenAPI Description](#openapi-description).
389
421
390
422
This object MAY be extended with [Specification Extensions](#specification-extensions).
391
423
424
+
To ensure interoperability, references MUST use the target document's `$self` URI if the `$self` field is present.
425
+
Implementations MAY choose to support referencing by other URIs such as the retrieval URI even when `$self` is present, however this behavior is not interoperable and relying on it is NOT RECOMMENDED.
426
+
392
427
#### Info Object
393
428
394
429
The object provides metadata about the API.
@@ -483,6 +518,8 @@ An object representing a Server.
483
518
484
519
This object MAY be extended with [Specification Extensions](#specification-extensions).
485
520
521
+
See [Examples of API Base URL Determination](#examples-of-api-base-url-determination) for examples of resolving relative server URLs.
522
+
486
523
##### Server Object Example
487
524
488
525
A single server would be described as:
@@ -4049,3 +4086,218 @@ components:
4049
4086
```
4050
4087
4051
4088
In the `other` document, the referenced path item has a Security Requirement for a Security Scheme, `MySecurity`. The same Security Scheme exists in the original entry document. As outlined in [Resolving Implicit Connections](#resolving-implicit-connections), `MySecurity` is resolved with an [implementation-defined behavior](#undefined-and-implementation-defined-behavior). However, documented in that section, it is RECOMMENDED that tools resolve component names from the [entry document](#openapi-description-structure). As with all implementation-defined behavior, it is important to check tool documentation to determine which behavior is supported.
4089
+
4090
+
## Appendix G: Examples of Base URI Determination and Reference Resolution
4091
+
4092
+
This section shows each of the four possible sources of base URIs, followed by an example with a relative `$self` and `$id`.
4093
+
4094
+
### Base URI Within Content
4095
+
4096
+
A base URI within the resource's content (RFC3986 Section 5.1.1) is the highest-precedence source of a base URI.
4097
+
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:
4098
+
4099
+
Assume the retrieval URI of the following document is `file://home/someone/src/api/openapi.yaml`:
4100
+
4101
+
```YAML
4102
+
openapi: 3.2.0
4103
+
$self: https://example.com/api/openapi
4104
+
info:
4105
+
title: Example API
4106
+
version: 1.0
4107
+
paths:
4108
+
/foo:
4109
+
get:
4110
+
requestBody:
4111
+
$ref: "shared/foo#/components/requestBodies/Foo"
4112
+
```
4113
+
4114
+
Assume the retrieval URI for the following document is `https://git.example.com/shared/blob/main/shared/foo.yaml`:
4115
+
4116
+
```YAML
4117
+
openapi: 3.2.0
4118
+
$self: https://example.com/api/shared/foo
4119
+
info:
4120
+
title: Shared components for all APIs
4121
+
version: 1.0
4122
+
components:
4123
+
requestBodies:
4124
+
Foo:
4125
+
content:
4126
+
application/json:
4127
+
schema:
4128
+
$ref: ../schemas/foo
4129
+
schemas:
4130
+
Foo:
4131
+
$id: https://example.com/api/schemas/foo
4132
+
properties:
4133
+
bar:
4134
+
$ref: bar
4135
+
Bar:
4136
+
$id: https://example.com/api/schemas/bar
4137
+
type: string
4138
+
```
4139
+
4140
+
In this example, the retrieval URIs are irrelevant because both documents define `$self`.
4141
+
4142
+
The relative `$ref` in the first document is resolved against `$self` to produce `https://example.com/api/shared/foo#/components/requestBodies/Foo`.
4143
+
The portion of that URI before the '#' matches the `$self` of the second document, so the reference target is resolved to `#/components/requestBodies/Foo` in that second document.
4144
+
4145
+
In that document, the `$ref` in the Request Body Object is resolved using that document's `$self` as the base URI, producing `https://example.com/api/schemas/foo`.
4146
+
This matches the `$id` at `#/components/schemas/Foo/$id` so it points to that Schema Object.
4147
+
That Schema Object has a subschema with `$ref: bar`, which is resolved against the `$id` to produce `https://example.com/api/schemas/bar`, which matches the `$id` at `#/components/schemas/Bar/$id`.
4148
+
4149
+
To guarantee interoperability, Schema Objects containing an `$id`, or that are under a schema containing an `$id`, MUST be referenced by the nearest such `$id` for the non-fragment part of the reference.
4150
+
As the JSON Schema specification notes, using a base URI other than the nearest `$id` and crossing that `$id` with a JSON Pointer fragment [is not interoperable](https://www.ietf.org/archive/id/draft-bhutton-json-schema-01.html#name-json-pointer-fragments-and-).
4151
+
4152
+
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 _only_ a JSON Pointer fragment, as the JSON Pointer would be resolved relative to `https://example.com/api/schemas/foo`, not to the OpenAPI Document's base URI from `$self`.
4153
+
4154
+
### Base URI From Encapsulating Entity
4155
+
4156
+
If no base URI can be determined within the content, the next location to search is any encapsulating entity (RFC3986 Section 5.1.2).
4157
+
4158
+
This is common for Schema Objects encapsulated within an OpenAPI Document.
4159
+
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.
4160
+
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.
4161
+
4162
+
RFC2557 was written to allow sending hyperlinked sets of documents as email attachments, in which case there would not be a retrieval URI for the multipart attachment (although the format could also be used in HTTP as well).
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.
4210
+
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.
4211
+
The resulting reference to `https://example.com/schemas/bar` matches the `Content-Location` header of the second part, which according to RFC2557 allows the reference target to be located within the multipart archive.
4212
+
4213
+
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.
4214
+
4215
+
### Base URI From the Retrieval URI
4216
+
4217
+
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).
4218
+
4219
+
Assume this document was retrieved from `https://example.com/api/openapis.yaml`:
4220
+
4221
+
```YAML
4222
+
openapi: 3.2.0
4223
+
info:
4224
+
title: Example API
4225
+
version: 1.0
4226
+
components:
4227
+
requestBodies:
4228
+
Foo:
4229
+
content:
4230
+
application/json:
4231
+
schema:
4232
+
$ref: schemas/foo
4233
+
```
4234
+
4235
+
Assume this document was retrieved from `https://example.com/api/schemas/foo`:
4236
+
4237
+
```JSON
4238
+
{
4239
+
"type": "object",
4240
+
"properties": {
4241
+
"bar": {
4242
+
"type": "string"
4243
+
}
4244
+
}
4245
+
}
4246
+
```
4247
+
4248
+
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.
4249
+
4250
+
### Application-Specific Default Base URI
4251
+
4252
+
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).
4253
+
While this sort of internal resolution an be performed in practice without choosing a base URI, choosing one, such as a URN with a randomly generated UUID (e.g. `urn:uuid:f26cdaad-3193-4398-a838-4ecb7326c4c5`) avoids the need to implement it as a special case.
4254
+
4255
+
### Resolving Relative `$self` and `$id`
4256
+
4257
+
Let's re-consider the first example in this appendix, but with relative URI-references for `$self` and `$id`, and retrieval URIs that support that relative usage:
4258
+
4259
+
4260
+
Assume that the following is retrieved from `https://staging.example.com/api/openapi`:
4261
+
4262
+
```YAML
4263
+
openapi: 3.2.0
4264
+
$self: /api/openapi
4265
+
info:
4266
+
title: Example API
4267
+
version: 1.0
4268
+
paths:
4269
+
/foo:
4270
+
get:
4271
+
requestBody:
4272
+
$ref: "shared/foo#/components/requestBodies/Foo"
4273
+
```
4274
+
4275
+
Assume the retrieval URI for the following document is `https://staging.example.com/api/shared/foo`:
4276
+
4277
+
```YAML
4278
+
openapi: 3.2.0
4279
+
$self: /api/shared/foo
4280
+
info:
4281
+
title: Shared components for all APIs
4282
+
version: 1.0
4283
+
components:
4284
+
requestBodies:
4285
+
Foo:
4286
+
content:
4287
+
application/json:
4288
+
schema:
4289
+
$ref: ../schemas/foo
4290
+
schemas:
4291
+
Foo:
4292
+
$id: /api/schemas/foo
4293
+
properties:
4294
+
bar:
4295
+
$ref: bar
4296
+
Bar:
4297
+
$id: /api/schemas/bar
4298
+
type: string
4299
+
```
4300
+
4301
+
In this example, all of the `$self` and `$id` values are relative URI-references consisting of an absolute path.
4302
+
This allows the retrieval URI to set the host (and scheme), in this case `https://staging.example.com`, resulting in the first document's `$self` being `https://staging.example.com/openapi`, and the second document's `$self` being `https://staging.example.com/api/shared/foo`, with `$id` values of `https://staging.example.com/api/schemas/foo` and `https://staging.example.com/api/schemas/bar`.
4303
+
Relative `$self` and `$id` values of this sort allow the same set of documents to work when deployed to other hosts, e.g. `https://example.com` (production) or `https://localhost:8080` (local development).
0 commit comments