Problem/Motivation

The JSON API spec demonstrates the sort query parameter as a list of fields to sort on, with hyphens indicating a field should be sorted in descending order:

/people?sort=age
/articles?sort=-created,title

The JSON API module, however, uses a different sort syntax. For example:

/node/article?sort[sort-created][path]=created&sort[sort-created][direction]=DESC

This is both more complex than the spec's syntax, and won't be compatible with other frameworks that utilize JSON API out-of-the-box.

Proposed resolution

Modify handling of the sort query parameter to match the official specification.

Comments

hampercm created an issue. See original summary.

e0ipso’s picture

Our sorting options are more complex than what the spec anticipates. We may want to sort by a language variant of a field, for instance.

I feel very strongly that we need to support the spec compliant version. I'm not 100% sure about the best way to achieve that. We could introduce some shortcut syntax that gets expanded into the current syntax (if that is not already in place. See #2742769: [FEATURE] Add support for sort). Or maybe we should limit our sorting options to what the spec allows.

Thoughts?

wim leers’s picture

Thanks @hampercm for reporting this and @e0ipso for your thoughtful response. I will be digging through this and related code and will hopefully be able to post a suggestion/recommendation somewhere in the next few days.

In the mean time, keep the discussion going, don't wait for me :)

hampercm’s picture

+1 on @e0ipso's idea of adding a shortcut syntax.

spleshka’s picture

Status: Active » Fixed

It seems like it had been done as a part of #2742769: [FEATURE] Add support for sort. We certainly can do something like this:

GET /jsonapi/node/article?sort=-created,title

and it works! I've added shorthand info to the docs https://www.drupal.org/docs/8/modules/json-api/collections-filtering-sor.... Setting the issue status to Fixed. Feel free to reopen if you disagree.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.