Skip to content

Commit dc58a15

Browse files
committed
Refactoring based on PR discussion
1 parent 70c72a3 commit dc58a15

File tree

1 file changed

+27
-88
lines changed

1 file changed

+27
-88
lines changed

guidelines/REUSE.md

Lines changed: 27 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,40 @@ The following types are reusable, as defined by the spec:
99
* Parameters
1010
* Models (or Schema Objects in general)
1111
* Responses
12-
* Operations (_Operations can only be referenced externally_)
12+
* Operations (_Operations can only be remote references_)
1313

1414
## Reuse strategy
1515

16-
When reusing components in an API design, a pointer is created from the definition to target design. The references are maintained in the structure, and can be updated by modifying the source definitions. This is different from a "copy on design" approach where references are injected into the design itself.
16+
When reusing components in an API design, a reference is created from the definition to target entity. The references are maintained in the structure, and can be updated by modifying the source definitions. This is different from a "copy on design" approach where references are injected into the design itself.
1717

1818
The reuse technique is transparent between JSON or YAML and is lossless when converting between the two.
1919

2020
YAML anchors are technically allowed but break the general reuse strategy in OpenAPI Specification, since anchors are "injected" into a single document. They are not recommended.
2121

22-
Referenes can be made either internal to the OpenApi Specification file or to external files.
23-
2422
## Techniques
2523

2624
### Guidelines for Referencing
2725

28-
Whether you reference definitions internally or externally, you can never override or change their definitions from the referring ___location. The definitions can only be used as-is.
26+
All references should follow the [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03) specification.
27+
28+
JSON Reference provides guidance on the resolution of references, notably:
29+
30+
> If the URI contained in the JSON Reference value is a relative URI,
31+
then the base URI resolution MUST be calculated according to
32+
[RFC3986], section 5.2. Resolution is performed relative to the
33+
referring document.
34+
35+
Whether you reference definitions locally or remote, you can never override or change their definitions from the referring ___location. The definitions can only be used as-is.
2936

30-
#### Internal references
37+
#### Local references
3138

32-
When referencing internally, the target references have designated locations:
39+
When referencing locally (within the current document), the target references should follow the conventions, as defined by the spec:
3340

3441
* Parameters -> `#/parameters`
3542
* Responses -> `#/responses`
36-
* Models (and general Schema Objects) -> `#/definitions`
43+
* Definitions (Models/Schema) -> `#/definitions`
3744

38-
All references are canonical and must be a qualified [JSON Pointer, per RFC6901](http://tools.ietf.org/html/rfc6901). For example, simply referencing a model `Pet` is not allowed, even if there are no other definitions of it in the file.
45+
An example of a local definition reference:
3946

4047
_Example from https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/json/petstore.json_
4148
``` json
@@ -49,77 +56,11 @@ _Example from https://github.com/OAI/OpenAPI-Specification/blob/master/examples/
4956
}
5057
```
5158

52-
#### External references
59+
#### Remote references
5360

54-
If your referenced file contains only one root-level definition, you can refer to the file directly.
61+
##### Relative path
5562

56-
To reference the example below:
57-
58-
`"$ref": "definitions/Model.json""`
59-
60-
or
61-
62-
`"$ref": "https://my.company.com/definitions/Model.json"`.
63-
64-
_Assuming file https://my.company.com/definitions/Model.json_
65-
```json
66-
{
67-
"description": "A simple model",
68-
"type": "object",
69-
"properties": {
70-
"id": {
71-
"type": "integer"
72-
}
73-
}
74-
}
75-
```
76-
77-
To reference `Model` in the example below:
78-
79-
_Note this approach potentially combines URL, JSON Reference, and JSON Pointer_
80-
81-
`"$ref": "definitions/models.json#/models/Model"`
82-
83-
or
84-
85-
`"$ref": "https://my.company.com/definitions/models.json#/models/Model"`
86-
87-
88-
_Assuming file https://my.company.com/definitions/models.json_
89-
```json
90-
{
91-
"models": {
92-
"Model": {
93-
"description": "A simple model",
94-
"type": "object",
95-
"properties": {
96-
"id": {
97-
"type": "integer"
98-
}
99-
},
100-
"Tag": {
101-
"description": "A tag entity in the system",
102-
"type": "object",
103-
"properties": {
104-
"name": {
105-
"type": "string"
106-
}
107-
}
108-
}
109-
}
110-
}
111-
```
112-
113-
#### External by relative reference
114-
115-
All external relative references should follow the [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03) specification.
116-
117-
Per the JSON Reference spec:
118-
119-
> If the URI contained in the JSON Reference value is a relative URI,
120-
then the base URI resolution MUST be calculated according to
121-
[RFC3986], section 5.2. Resolution is performed relative to the
122-
referring document.
63+
Files can be referred to in relative paths to the current document.
12364

12465
_Example from https://github.com/OAI/OpenAPI-Specification/tree/master/examples/v2.0/json/petstore-separate/spec/swagger.json_
12566

@@ -134,7 +75,7 @@ _Example from https://github.com/OAI/OpenAPI-Specification/tree/master/examples/
13475
}
13576
```
13677

137-
External references may also utilize [JSON Pointer](http://tools.ietf.org/html/rfc6901) to reference properties within the relative external file.
78+
Remote references may also reference properties within the relative remote file.
13879

13980
_Example from https://github.com/OAI/OpenAPI-Specification/tree/master/examples/v2.0/json/petstore-separate/spec/swagger.json_
14081
``` json
@@ -149,14 +90,12 @@ _Example from https://github.com/OAI/OpenAPI-Specification/tree/master/examples/
14990
```
15091

15192

152-
#### External by URL
93+
##### URL
15394

154-
External files can be hosted on an HTTP server (rather than the local file system).
95+
Remote files can be hosted on an HTTP server (rather than the local file system).
15596

15697
One risk of this approach is that environment specific issues could arise if DNS is not taken into account (as the reference can only contain one hostname).
15798

158-
Resolution of URLs should follow [RFC3986](https://tools.ietf.org/html/rfc3986).
159-
16099
_Assuming file https://my.company.com/definitions/Model.json_
161100
```json
162101
{
@@ -175,7 +114,7 @@ _Assuming file https://my.company.com/definitions/Model.json_
175114
}
176115
```
177116

178-
External references may also utilize a URL + JSON Pointer to reference properties within the external file.
117+
Remote references may also reference properties within the remote file.
179118

180119
_Assuming file https://my.company.com/definitions/models.json_
181120
```json
@@ -213,14 +152,14 @@ _Assuming file https://my.company.com/definitions/models.json_
213152

214153
Reuse schema definitions by creating a repository of definitions. This is done by simply hosting a file or set of files for commonly used definitions across a company or organization.
215154

216-
Refer to [External references](#external-references) for referencing strategies.
155+
Refer to [Guidelines for Referencing](#guidelines-for-referencing) for referencing strategies.
217156

218157

219158
### Parameters
220159

221160
Similar to model schemas, you can create a repository of `parameters` to describe the common entities that appear throughout a set of systems.
222161

223-
Refer to [External references](#external-references) for referencing strategies.
162+
Refer to [Guidelines for Referencing](#guidelines-for-referencing) for referencing strategies.
224163

225164
Using the same technique as above, you can host on either a single or multiple files. For simplicity, the example below assumes a single file.
226165

@@ -296,7 +235,7 @@ To include these parameters, you would need to add them individually as such:
296235

297236
Again, Operations can be shared across files. Although the reusability of operations will be less than with Parameters and models. For this example, we will share a common `health` resource so that all APIs can reference it:
298237

299-
Refer to [External references](#external-references) for additional referencing strategies.
238+
Refer to [Guidelines for Referencing](#guidelines-for-referencing) for referencing strategies.
300239

301240
```json
302241
{
@@ -338,7 +277,7 @@ Remember, you cannot override the definitions, but in this case, you can add add
338277

339278
### Responses
340279

341-
Refer to [External references](#external-references) for additional referencing strategies.
280+
Refer to [Guidelines for Referencing](#guidelines-for-referencing) for referencing strategies.
342281

343282
Assume the file `responses.json`:
344283

0 commit comments

Comments
 (0)