Skip to content

Commit 1c243bf

Browse files
committed
Splitting guidelines
Original authors credit - @olensmar and @jasonh-n-austin
1 parent 9289c71 commit 1c243bf

File tree

2 files changed

+173
-171
lines changed

2 files changed

+173
-171
lines changed

guidelines/JSON_References.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Guidelines for Referencing
2+
3+
The OpenAPI Specification relies on JSON Refereneces as the machinsm for reusability (DRY). This guide gives you a brief introduction to JSON References and its capability. It does not provide the full details which are covered in its own specification, but aims to provide you with information about the reference types.
4+
5+
All references should follow the [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03) specification.
6+
7+
JSON Reference provides guidance on the resolution of references, notably:
8+
9+
> If the URI contained in the JSON Reference value is a relative URI,
10+
then the base URI resolution MUST be calculated according to
11+
[RFC3986], section 5.2. Resolution is performed relative to the
12+
referring document.
13+
14+
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.
15+
16+
### Local references
17+
18+
When referencing locally (within the current document), the target references should follow the conventions, as defined by the spec:
19+
20+
* Parameters -> `#/parameters`
21+
* Responses -> `#/responses`
22+
* Definitions (Models/Schema) -> `#/definitions`
23+
24+
An example of a local definition reference:
25+
26+
_Example from https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/json/petstore.json_
27+
``` json
28+
// ...
29+
"200": {
30+
"description": "pet response",
31+
"schema": {
32+
"type": "array",
33+
"items": {
34+
"$ref": "#/definitions/Pet"
35+
}
36+
}
37+
```
38+
39+
### Remote references
40+
41+
#### Relative path
42+
43+
Files can be referred to in relative paths to the current document.
44+
45+
_Example from https://github.com/OAI/OpenAPI-Specification/tree/master/examples/v2.0/json/petstore-separate/spec/swagger.json_
46+
47+
``` json
48+
// ...
49+
"responses": {
50+
"default": {
51+
"description": "unexpected error",
52+
"schema": {
53+
"$ref": "../common/Error.json"
54+
}
55+
}
56+
}
57+
```
58+
59+
Remote references may also reference properties within the relative remote file.
60+
61+
_Example from https://github.com/OAI/OpenAPI-Specification/tree/master/examples/v2.0/json/petstore-separate/spec/swagger.json_
62+
``` json
63+
// ...
64+
"parameters": [
65+
{
66+
"$ref": "parameters.json#/tagsParam"
67+
},
68+
{
69+
"$ref": "parameters.json#/limitsParam"
70+
}
71+
]
72+
```
73+
74+
75+
#### URL
76+
77+
Remote files can be hosted on an HTTP server (rather than the local file system).
78+
79+
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).
80+
81+
_Assuming file https://my.company.com/definitions/Model.json_
82+
```json
83+
{
84+
"description": "A simple model",
85+
"type": "object",
86+
"properties": {
87+
"id": {
88+
"type": "integer",
89+
"format": "int64"
90+
},
91+
"tag": {
92+
"description": "A complex, shared property. Note the absolute reference",
93+
"$ref": "https://my.company.com/definitions/Tag.json"
94+
}
95+
}
96+
}
97+
```
98+
99+
Remote references may also reference properties within the remote file.
100+
101+
_Assuming file https://my.company.com/definitions/models.json_
102+
```json
103+
{
104+
"models": {
105+
"Model": {
106+
"description": "A simple model",
107+
"type": "object",
108+
"properties": {
109+
"id": {
110+
"type": "integer",
111+
"format": "int64"
112+
},
113+
"tag": {
114+
"description": "a complex, shared property. Note the absolute reference",
115+
"$ref": "https://my.company.com/definitions/models.json#/models/Tag"
116+
}
117+
}
118+
},
119+
"Tag": {
120+
"description": "A tag entity in the system",
121+
"type": "object",
122+
"properties": {
123+
"name": {
124+
"type": "string"
125+
}
126+
}
127+
}
128+
}
129+
}
130+
```

0 commit comments

Comments
 (0)