Skip to content

fix: return an invalid error for sort input that is not a string or list - #2947

Merged
zachdaniel merged 1 commit into
ash-project:mainfrom
chiliec:fix/sort-input-invalid-shape
Sep 17, 2026
Merged

zachdaniel merged 1 commit into
ash-project:mainfrom
chiliec:fix/sort-input-invalid-shape

Conversation

@chiliec

@chiliec chiliec commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Closes #2941.

Ash.Sort.parse_input/3 has clauses for a string, an atom, a list and nil, so any other value (5, %{}, %{"title" => "asc"}) raises FunctionClauseError. A list is accepted, but an element that is neither a field name nor a {field, order} tuple (e.g. [%{"field" => "title"}]) falls through parse_sort/4 into Ash.Resource.Info.attribute/2, which raises the same way. Ash.Query.filter_input/2 puts an InvalidFilterValue on the query for a malformed shape; sort_input/2 now does the equivalent.

  • New Ash.Error.Query.InvalidSort (class :invalid, field :sort), alongside the existing InvalidSortOrder.
  • parse_input/3 gets a final clause returning {:error, %InvalidSort{}}.
  • The catch-all parse_sort/4 clause is guarded to binary | atom | struct (the shapes get_field/5 handles), with a final clause returning InvalidSort for anything else. Ash.Query.sort_input/2 then adds that error to the query instead of raising.

Tests: test/sort/sort_test.exs covers sort_input with 5, %{}, %{"title" => "asc"} and [%{"field" => "title"}], and parse_input with 5 and a map. On main the four sort_input cases raise FunctionClauseError (Ash.Sort.parse_input/3 / Ash.Resource.Info.attribute/2); with the change they pass. Full suite: 4149/4150 — the one failure (Mix.Tasks.Ash.Gen.ResourceTest "generates resource with custom base") fails identically on main without this change. mix format --check-formatted and mix credo --strict clean on the touched files.

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

@grempe

grempe commented Sep 16, 2026

Copy link
Copy Markdown

Thanks for picking this up. I ran the reproduction suite from https://github.com/grempe/ash-fuzz-repros with ash pinned to f43fd6a. The four sort_input cases in https://github.com/grempe/ash-fuzz-repros/blob/main/test/ash/sort_input_shape_test.exs (5, %{}, %{"title" => "asc"} and [%{"field" => "title"}]) now put an InvalidSort error on the query instead of raising, and the control still passes. As a side effect the ash_lua sort = 5 case in that suite, which was surfacing the same FunctionClauseError through the script boundary, also passes. Nothing else in the suite changed.

@zachdaniel
zachdaniel merged commit 363bff8 into ash-project:main Sep 17, 2026
@zachdaniel

Copy link
Copy Markdown
Contributor

🚀 Thank you for your contribution! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sort_input crashes on a value that is neither a string nor a list

3 participants