Skip to content

Commit 5bcb823

Browse files
TimvdLippesanderke
andauthored
Voeg regel toe voor het altijd teruggeven van objecten (#354)
Deze suggestie kwam naar voren tijdens de brainstorm sessie over pagination. De regel is breder toepasbaar dan enkel pagination en bij meerdere API's is deze verandering toegepast nadat bleek dat er behoefte was aan metadata. Ook is dit voor linked data belangrijk zodat ook API's beter integreren met linked data. Co-authored-by: Alexander Green <alexander.green@logius.nl>
1 parent 4b1fcca commit 5bcb823

4 files changed

Lines changed: 212 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
/testcases/return-objects/openapi.json
3+
108:44 error nlgov:always-return-objects The content of all responses should be wrapped in an object paths./incorrect.get.responses[200].content.application/json.schema.type
4+
113:44 error nlgov:always-return-objects The content of all responses should be wrapped in an object paths./incorrect.get.responses[200].content.application/hal+json.schema.type
5+
118:44 error nlgov:always-return-objects The content of all responses should be wrapped in an object paths./incorrect.get.responses[200].content.application/xml.schema.type
6+
7+
✖ 3 problems (3 errors, 0 warnings, 0 infos, 0 hints)
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"openapi": "3.0.3",
3+
"info": {
4+
"title": "Baseline",
5+
"description": "Deze OpenAPI specification bevat het minimale om aan alle regels te voldoen.",
6+
"contact": {
7+
"name": "Beheerder",
8+
"url": "https://www.example.com",
9+
"email": "mail@example.com"
10+
},
11+
"version": "1.0.0"
12+
},
13+
"servers": [
14+
{
15+
"url": "https://example.com/api/v1"
16+
}
17+
],
18+
"security": [
19+
{
20+
"default": []
21+
}
22+
],
23+
"tags": [
24+
{
25+
"name": "openapi"
26+
}
27+
],
28+
"paths": {
29+
"/correct": {
30+
"get": {
31+
"tags": [
32+
"openapi"
33+
],
34+
"description": "OpenAPI document",
35+
"operationId": "getCorrect",
36+
"parameters": [],
37+
"responses": {
38+
"200": {
39+
"description": "OK",
40+
"headers": {
41+
"API-Version": {
42+
"description": "De huidige versie van de applicatie",
43+
"style": "simple",
44+
"schema": {
45+
"type": "string"
46+
}
47+
}
48+
},
49+
"content": {
50+
"application/json": {
51+
"schema": {
52+
"type": "object"
53+
}
54+
},
55+
"application/hal+json": {
56+
"schema": {
57+
"type": "object"
58+
}
59+
},
60+
"application/xml": {
61+
"schema": {
62+
"type": "object"
63+
}
64+
},
65+
"text/plain": {
66+
"schema": {
67+
"type": "string"
68+
}
69+
},
70+
"text/html": {
71+
"schema": {
72+
"type": "string"
73+
}
74+
}
75+
}
76+
}
77+
},
78+
"security": [
79+
{
80+
"default": []
81+
}
82+
]
83+
}
84+
},
85+
"/incorrect": {
86+
"get": {
87+
"tags": [
88+
"openapi"
89+
],
90+
"description": "OpenAPI document",
91+
"operationId": "getIncorrect",
92+
"parameters": [],
93+
"responses": {
94+
"200": {
95+
"description": "OK",
96+
"headers": {
97+
"API-Version": {
98+
"description": "De huidige versie van de applicatie",
99+
"style": "simple",
100+
"schema": {
101+
"type": "string"
102+
}
103+
}
104+
},
105+
"content": {
106+
"application/json": {
107+
"schema": {
108+
"type": "array"
109+
}
110+
},
111+
"application/hal+json": {
112+
"schema": {
113+
"type": "array"
114+
}
115+
},
116+
"application/xml": {
117+
"schema": {
118+
"type": "array"
119+
}
120+
}
121+
}
122+
}
123+
},
124+
"security": [
125+
{
126+
"default": []
127+
}
128+
]
129+
}
130+
}
131+
},
132+
"components": {
133+
"schemas": {
134+
},
135+
"securitySchemes": {
136+
"default": {
137+
"type": "oauth2",
138+
"flows": {
139+
"implicit": {
140+
"authorizationUrl": "https://test.com",
141+
"scopes": {}
142+
}
143+
}
144+
}
145+
}
146+
}
147+
}

‎media/linter.yaml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,14 @@ rules:
290290
function: pattern
291291
functionOptions:
292292
match: ^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$
293+
294+
#/core/always-return-objects
295+
nlgov:always-return-objects:
296+
severity: error
297+
message: "The content of all responses should be wrapped in an object"
298+
given: $..[responses][*][content][?(@property.toString().match(/(json)|(xml)/))][schema]
299+
then:
300+
field: type
301+
function: pattern
302+
functionOptions:
303+
match: "object"

‎sections/designRules.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,53 @@ https://api.example.org/v1/vergunningen/d285e05c-6b01-45c3-92d8-5e19a946b66f</pr
192192
</dl>
193193
</div>
194194

195+
<div class="rule" id="/core/always-return-objects" data-type="technical">
196+
<p class="rulelab">Always return a response with an object</p>
197+
<dl>
198+
<dt>Statement</dt>
199+
<dd>
200+
<p>A JSON or XML response MUST have a top-level object, regardless of request method.
201+
For <a>collection resources</a>, the object MUST contain a field (its key MAY be named <code>items</code>) with its value an array of items from that collection.
202+
</dd>
203+
<dt>Rationale</dt>
204+
<dd>
205+
<p>For <a>singular resources</a>, objects are returned as part of RESTful design.
206+
<p>For <a>collection resources</a>, items can be retrieved all at once, or a subset thereof.
207+
In both cases, wrapping the returned items in an object allows metadata to be attached to the response.
208+
Examples of metadata are structured data to implement linked data concepts, or pagination data for performance purposes.
209+
<p>If a collection resource directly returns an array of object information from the collection, such metadata can only be provided using HTTP headers.
210+
While that is feasible, HTTP headers are generally more difficult to work with than content from a response body.
211+
<p>Even if currently no metadata is attached to a response, that could be the case in the future.
212+
Changing the response from an array to an object is a breaking change, hence it is future-proof to always return an object.
213+
<aside class="example">
214+
The following example shows a response from a collection resource containing all items
215+
<pre class="http">GET /organisations HTTP/1.1</pre>
216+
<pre class="http">HTTP/1.1 200 OK
217+
Content-Type: application/json
218+
219+
{
220+
"someOtherField": {
221+
},
222+
"items": [
223+
{
224+
"id": "12345",
225+
"name": "Logius"
226+
},
227+
{
228+
"id": "67890",
229+
"name": "Geonovum"
230+
}
231+
]
232+
}</pre>
233+
</aside>
234+
</dd>
235+
<dt>How to test</dt>
236+
<dd>
237+
Analyse all JSON and XML responses for paths and check that the response contains a top-level object.
238+
</dd>
239+
</dl>
240+
</div>
241+
195242
<span id="api-53"></span>
196243
<div class="rule" id="/core/hide-implementation" data-type="functional">
197244
<p class="rulelab">Hide irrelevant implementation details</p>

0 commit comments

Comments
 (0)