wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
266 stars 109 forks source link

Ascending sort order does not work #74

Closed davidgoli closed 8 years ago

davidgoli commented 8 years ago

It also doesn't match the JSONAPI spec.

Routing.swift line 156 builds an ascending sort order with a prepended +:

                if descriptor.ascending {
                    return "+\(key)"
                } else {
                    return "-\(key)"
                }

However, this gets converted into a path that looks like /api/cameras/21/sleeps?sort=+started-at, which without extra encoding is interpreted as a space character per https://www.w3.org/Addressing/URL/4_URI_Recommentations.html:

Within the query string, the plus sign is reserved as shorthand notation for a space. Therefore, real plus signs must be encoded. This method was used to make query URIs easier to pass in systems which did not allow spaces.

This plus sign is also not required by the JSONAPI spec, which says:

The sort order for each sort field MUST be ascending unless it is prefixed with a minus (U+002D HYPHEN-MINUS, "-"), in which case it MUST be descending.

(http://jsonapi.org/format/#fetching-sorting)

As-is, this implementation of ascending sort is not compatible with popular server-side frameworks like Rails' jsonapi-resources, which returns a 400: Bad Request since it interprets the + sign as a leading space, not as a plus.