Repository navigation
feat: ETU-76297: Added guidelines for sorting #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1da1ae7
docs: ETU-76297: Added guidelines for sorting, and improved paginatio…
rikard-swahn 8593edb
docs: ETU-76297: "Response format" is no longer a separate section, b…
rikard-swahn d2075be
docs: ETU-76297: Removed unnecessary filtering in examples.
rikard-swahn ad46e48
docs: ETU-76297: Comma instead of em-dash.
rikard-swahn 2993418
docs: ETU-76297: Removed redundant doc.
rikard-swahn f0083ae
docs: ETU-76297: Improved Sorting section.
rikard-swahn 3760355
docs: ETU-76297: Removed redundant doc.
rikard-swahn ed2cedc
docs: ETU-76297: Removed redundant "<field> MUST be named ...".
rikard-swahn 52ad613
docs: ETU-76297: Simplified Sorting section and referring to sorting.…
rikard-swahn 23b706f
docs: ETU-76297: Language cleanup in Sorting section.
rikard-swahn 71d0808
docs: ETU-76297: Introduction now defines "sorting", and Tiebreaking …
rikard-swahn 4a7dbca
docs: ETU-76297: Added OpenAPI documentation examples.
rikard-swahn 4b86327
docs: ETU-76297: Returned collections must be sorted.
rikard-swahn 7330df1
docs: ETU-76297: Added section about sorting on all collections.
rikard-swahn ee512d2
docs: ETU-76297: Removed redundant info.
rikard-swahn c426964
docs: ETU-76297: Removed redundant info.
rikard-swahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # Sorting | ||
|
|
||
| All returned collections **MUST** be sorted, which it means that the order is deterministic: the same | ||
| request **MUST** return items in the same order. | ||
|
|
||
| ## Client-controlled sorting | ||
|
|
||
| Client-controlled sorting **MUST** use the query parameter `sort`. | ||
|
|
||
| | Parameter | Type | Description | | ||
| |-----------|--------|-----------------------------------------------------| | ||
| | `sort` | string | The field(s) to sort on. | | ||
|
|
||
| Example: | ||
| ```http | ||
| GET /api/v1/bus-stops?sort=name | ||
| ``` | ||
|
|
||
| ### Sort field and direction | ||
|
|
||
| A `sort` value is a field name, optionally followed by a comma (`,`) and a | ||
| sort direction, either `asc` (ascending) or `desc` (descending): | ||
|
|
||
| ```http | ||
| GET /api/v1/bus-stops?sort=name,asc | ||
| ``` | ||
|
|
||
| - If the direction is omitted, the direction **MUST** default to `asc`. | ||
| - The direction tokens `asc` and `desc` **MUST** be treated as | ||
| case-insensitive. | ||
|
|
||
| ### Sorting on multiple fields | ||
|
|
||
| You **MAY** allow sorting on multiple fields by repeating the `sort` parameter: | ||
|
|
||
| ```http | ||
| GET /api/v1/bus-stops?sort=name&sort=created | ||
| ``` | ||
|
|
||
| Each field **MAY** have its own direction: | ||
|
|
||
| ```http | ||
| GET /api/v1/bus-stops?sort=name,asc&sort=created,desc | ||
| ``` | ||
|
|
||
| ### Allowed sort fields | ||
|
|
||
| You **MUST** document which fields are sortable. | ||
|
|
||
| ### Case sensitivity, collation and null ordering | ||
|
|
||
| - For string fields, the API **SHOULD** document whether sorting is | ||
| case-sensitive and which collation/locale is used. For Norwegian data, | ||
| sorting **SHOULD** order the letters `æ`, `ø` and `å` according to Norwegian | ||
| collation rules. | ||
| - The API **SHOULD** document where `null` or missing values are placed | ||
| (sorted first or last). | ||
|
|
||
|
|
||
| ### Tiebreaking | ||
| To achieve a deterministic order, the sorting implementation **MUST** append a unique | ||
| field with a stable value (typically `id`) as a final tiebreaker. For example, if a | ||
| client requests sorting on `name` and `name` is not unique across all items, a secondary | ||
| sort **MUST** also be applied on a unique field. | ||
|
|
||
| **Example** | ||
|
rikard-swahn marked this conversation as resolved.
|
||
|
|
||
| ```http | ||
| GET /item/v1/items?sort=name,asc | ||
| ``` | ||
|
|
||
| ```json | ||
| { | ||
| "items": [ | ||
| { | ||
| "id": "100", | ||
| "name": "Item A" | ||
| }, | ||
| { | ||
| "id": "101", | ||
| "name": "Item A" | ||
| }, | ||
| { | ||
| "id": "99", | ||
| "name": "Item B" | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| In this example, the client requested items sorted on `name` ascending. There | ||
| are two items with the name "Item A" but the backend applies a secondary sort on `id`, so they are | ||
| always returned in the same order (`100` before `101`). The item with `id` 99 | ||
| is still returned last, because the primary sort is on `name` and "Item B" | ||
| sorts after "Item A". | ||
|
|
||
| ### Documenting in OpenAPI | ||
|
|
||
| The documentation requirements above (allowed sort fields, default order, case | ||
| sensitivity, collation and null ordering) **MUST** be expressed on the `sort` | ||
| query parameter in your OpenAPI specification. | ||
|
|
||
| **Example** | ||
| ```json | ||
| { | ||
| "parameters": [ | ||
| { | ||
| "name": "sort", | ||
| "in": "query", | ||
| "description": "Field(s) to sort on. Repeat the parameter to sort on multiple fields. Each value is a field name optionally followed by a direction: `<field>,<asc|desc>`.\n\n**Sortable fields:** `name`, `created`.\n\n**Collation:** String fields are sorted case-insensitively using Norwegian collation.\n\n**Null ordering:** Items with a `null` or missing value for the sort field are placed last, regardless of sort direction.", | ||
| "schema": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "string", | ||
| "pattern": "^(name|created)(,(asc|desc))?$" | ||
| }, | ||
| "default": ["name,asc"], | ||
| "example": ["name,asc", "created,desc"] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| ## Sorting not controlled by client | ||
| When returning a collection, there is no requirement that the client can control the sorting. When sorting is not controlled by the client, you **MUST** however document the order on the response field for the collection: | ||
|
|
||
| **Example** | ||
| ```json | ||
| { | ||
| "items": { | ||
| "type": "array", | ||
| "description": "Sorted by `name` ascending, with `id` as a tiebreaker.", | ||
| "items": { | ||
| "$ref": "#/components/schemas/Item" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.