Skip to content

Request.parse_sort_criteria may not handle literal + in sort params #120

@realityendshere

Description

@realityendshere

I opened an equivalent issue on the JSON API spec project: json-api/json-api#486

If I am not mistaken, many web servers see the plus symbol ("+") as a substitute for a white space or "%20" when sent as a query param in the URL. For example, this request

GET /people?sort=+age,+name

shows up as " age, name" to the Request.parse_sort_criteria method. Which is a total bummer for the if statement

if sort.start_with?('+')
  sort_criteria[:direction] = :asc
elsif sort.start_with?('-')
  sort_criteria[:direction] = :desc
else
  @errors.concat(...)
end

The spec compliant GET request leads to a 400 error because " age" and " name" both start with a white space instead of a "+"!

I find I can address this by either allowing a leading white space to behave as a + (e.g. sort.start_with?(' ')) or using %2b to represent + in the query params and using something like sort_criteria = URI.unescape(sort_criteria) to decode %2b into +.

Is there a preference for using one, the other, or both?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions