Skip to content

Commit 2832652

Browse files
committed
Improved examples
Including fixing a bug in one of the URIs.
1 parent db9f643 commit 2832652

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/oas.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5203,19 +5203,36 @@ In the `other` document, the referenced path item has a Security Requirement for
52035203
A base URI within the resource's content (RFC3986 Section 5.1.1) is the highest-precedence source of a base URI.
52045204
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:
52055205

5206+
Assume the retrieval URI of the following document is `file://home/someone/src/api/openapi.yaml`:
5207+
52065208
```YAML
52075209
openapi: 3.2.0
5208-
$self: https://example.com/openapi
5210+
$self: https://example.com/api/openapi
52095211
info:
52105212
title: Example API
52115213
version: 1.0
5214+
paths:
5215+
/foo:
5216+
get:
5217+
requestBody:
5218+
$ref: "shared#/components/requestBodies/Foo"
5219+
```
5220+
5221+
Assume the retrieval URI for the following document is `https://git.example.com/shared/blob/main/shared/foo.yaml`:
5222+
5223+
```YAML
5224+
openapi: 3.2.0
5225+
$self: https://example.com/api/shared/foo
5226+
info:
5227+
title: Shared components for all APIs
5228+
version: 1.0
52125229
components:
52135230
requestBodies:
52145231
Foo:
52155232
content:
52165233
application/json:
52175234
schema:
5218-
$ref: schemas/foo
5235+
$ref: ../schemas/foo
52195236
schemas:
52205237
Foo:
52215238
$id: https://example.com/api/schemas/foo
@@ -5227,7 +5244,12 @@ components:
52275244
type: string
52285245
```
52295246

5230-
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`.
5247+
In this example, the retrieval URIs are irrelevant because both documents define `$self`.
5248+
5249+
For the relative `$ref` in the first document, it is resolved against `$self` to produce `https://example.com/shared/foo#/components/requestBodies/Foo`.
5250+
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.
5251+
5252+
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/schemas/foo`.
52315253
This matches the `$id` at `#/components/schemas/Foo/$id` so it points to that Schema Object.
52325254
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`.
52335255

@@ -5237,7 +5259,6 @@ Therefore it is RECOMMENDED that OAD authors use `$id` values to reference such
52375259

52385260
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`.
52395261

5240-
52415262
### Base URI From Encapsulating Entity
52425263

52435264
If no base URI can be determined within the content, the next ___location to search is any encapsulating entity (RFC3986 Section 5.1.2).
@@ -5246,6 +5267,8 @@ This is common for Schema Objects encapsulated within an OpenAPI Document.
52465267
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.
52475268
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.
52485269

5270+
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).
5271+
52495272
```MULTIPART
52505273
--boundary-example
52515274
Content-Type: application/openapi+yaml
@@ -5293,7 +5316,7 @@ Content-Location: https://example.com/api/docs.html
52935316

52945317
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.
52955318
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.
5296-
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.
5319+
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.
52975320

52985321
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.
52995322

0 commit comments

Comments
 (0)