Skip to content

Commit 9eafc9c

Browse files
committed
feat: add REST documentation for fields query string parameters to filter response fields
1 parent 58233ce commit 9eafc9c

3 files changed

Lines changed: 113 additions & 3 deletions

File tree

.editorconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ end_of_line = lf
1010
insert_final_newline = true
1111
trim_trailing_whitespace = true
1212
charset = utf-8
13+
quote_type = single
1314

1415
[*.js]
15-
max_line_length = 88
16+
max_line_length = 88

docs/api-guidelines/pagination-filtering-sorting.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,93 @@ components:
233233
value: nhsNumber|asc,type|desc
234234
summary: Sort by multiple fields
235235
```
236+
237+
## Field Selection
238+
239+
**SHOULD** support a `fields` query parameter to allow clients to specify which fields should be included in the JSON response.
240+
241+
This improves performance by reducing payload size and network transfer time, and enhances usability for consumers who don't need the full object representation.
242+
243+
### Query Parameter
244+
245+
- `fields`: A comma-separated list of field names to include in the response.
246+
247+
### Examples
248+
249+
#### Basic field selection
250+
251+
```text
252+
GET /api/widgets?fields=id,name,createdAt
253+
```
254+
255+
Returns only the specified fields:
256+
257+
```json
258+
{
259+
"results": [
260+
{
261+
"id": "de750613-ef3c-4f5d-8148-10308b91896c",
262+
"name": "Widget Example",
263+
"createdAt": "2024-01-15T10:30:00Z"
264+
}
265+
]
266+
}
267+
```
268+
269+
#### Combining with other parameters
270+
271+
```text
272+
GET /api/widgets?fields=id,name,status&status=active&limit=5
273+
```
274+
275+
#### All fields (default behavior)
276+
277+
When the `fields` parameter is omitted, all fields are returned:
278+
279+
```text
280+
GET /api/widgets
281+
```
282+
283+
### Behavior
284+
285+
**SHOULD** implement the following behavior for the `fields` parameter:
286+
287+
- **Valid fields**: Include only the requested fields in the response
288+
- **Invalid field names**: Ignore invalid field names silently and include only valid ones
289+
- **Empty fields parameter**: Return all fields (same as omitting the parameter)
290+
- **Case sensitivity**: Field names should be case-sensitive
291+
- **Nested fields**: Support dot notation for nested objects (e.g., `metadata.total`)
292+
293+
```yaml
294+
components:
295+
parameters:
296+
fieldsParam:
297+
in: query
298+
name: fields
299+
required: false
300+
description: |
301+
Comma-separated list of fields to include in the response.
302+
303+
Behaviour:
304+
- Valid fields: include only the requested fields in the response.
305+
- Invalid field names: ignore silently and include only valid ones.
306+
- Empty fields parameter: return all fields (same as omitting the parameter).
307+
- Case sensitivity: field names are case-sensitive.
308+
- Nested fields: support dot notation for nested objects (e.g., `metadata.total`).
309+
schema:
310+
type: string
311+
pattern: ^[a-z][a-z0-9]*(?:[A-Z0-9](?:[a-z0-9]+|$))*(?:\.[a-z][a-z0-9]*(?:[A-Z0-9](?:[a-z0-9]+|$))*)*(?:,[a-z][a-z0-9]*(?:[A-Z0-9](?:[a-z0-9]+|$))*(?:\.[a-z][a-z0-9]*(?:[A-Z0-9](?:[a-z0-9]+|$))*)*)*$
312+
examples:
313+
basicFields:
314+
value: id,name,createdAt
315+
summary: Basic field selection
316+
nestedFields:
317+
value: id,name,metadata.createdAt
318+
summary: Including nested fields
319+
withInvalidFields:
320+
summary: Invalid fields are ignored
321+
value: id,name,doesNotExist,metadata.total
322+
emptyValueMeansAll:
323+
summary: Empty value returns all fields
324+
value: ""
325+
```

example/example.1.0.0.oas.yml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ paths:
5858
- $ref: '#/components/parameters/limitParam'
5959
- $ref: '#/components/parameters/offsetParam'
6060
- $ref: '#/components/parameters/sortParam'
61+
- $ref: '#/components/parameters/fieldsParam'
6162
- in: query
6263
name: type
6364
required: false
@@ -200,7 +201,7 @@ components:
200201
scopes:
201202
tests:read: read test results
202203
tests:write: submit test results
203-
204+
204205
schemas:
205206
ResultRequest:
206207
type: object
@@ -465,6 +466,24 @@ components:
465466
value: nhsNumber|asc,type|desc
466467
summary: Sort by multiple fields
467468

469+
fieldsParam:
470+
in: query
471+
name: fields
472+
description: Comma-separated list of fields to include in the response.
473+
schema:
474+
type: string
475+
pattern: ^[a-z][a-z0-9]*(?:[A-Z0-9](?:[a-z0-9]+|$))*(?:\.[a-z][a-z0-9]*(?:[A-Z0-9](?:[a-z0-9]+|$))*)*(?:,[a-z][a-z0-9]*(?:[A-Z0-9](?:[a-z0-9]+|$))*(?:\.[a-z][a-z0-9]*(?:[A-Z0-9](?:[a-z0-9]+|$))*)*)*$
476+
examples:
477+
basicFields:
478+
value: resultId,nhsNumber,result
479+
summary: Basic field selection
480+
nestedFields:
481+
value: resultId,nhsNumber,metadata.createdAt
482+
summary: Including nested fields
483+
allFields:
484+
value: resultId,nhsNumber,type,result,metadata.createdAt,metadata.updatedAt
485+
summary: Multiple fields selection
486+
468487
headers:
469488
Location:
470489
description: The URL of the created test result.
@@ -588,4 +607,4 @@ components:
588607
security:
589608
- oAuth:
590609
- tests:read
591-
- tests:write
610+
- tests:write

0 commit comments

Comments
 (0)