Skip to content

Commit

Permalink
fix: 400 error on invalid includes instead of 500
Browse files Browse the repository at this point in the history
fixes: #109
  • Loading branch information
zachdaniel committed Nov 13, 2023
1 parent 7c32427 commit cb22a76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/ash_json_api/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule AshJsonApi.Request do
InvalidBody,
InvalidField,
InvalidHeader,
InvalidIncludes,
InvalidQuery,
InvalidType,
UnacceptableMediaType,
Expand Down Expand Up @@ -473,8 +474,8 @@ defmodule AshJsonApi.Request do
%{allowed: allowed, disallowed: []} ->
%{request | includes: includes, includes_keyword: includes_to_keyword(request, allowed)}

%{allowed: _allowed, disallowed: _disallowed} ->
add_error(request, "invalid includes", request.route.type)
%{allowed: _allowed, disallowed: disallowed} ->
add_error(request, InvalidIncludes.new(includes: disallowed), request.route.type)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,19 @@ defmodule AshJsonApiTest.FetchingData.InclusionOfRelatedResources do
# If an endpoint does not support the include parameter, it MUST respond with 400 Bad Request to any requests that include it.
# --------------------------
describe "400 Bad Request for requests that with include parameter for endpoints without include parameter support" do
# We will be supporting the "include" parameter, so this statement is not applicable.
# However, I like the idea of keeping this here for explict documentation purposes.
# I'm not sure what exactly to do though - do we write a test, or just leave a comment saying "N/A"
# GET /posts/1?include=foobar
author =
Author
|> Ash.Changeset.for_create(:create, %{name: "foo"})
|> Api.create!()

post =
Post
|> Ash.Changeset.for_create(:create, %{name: "foo"})
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|> Api.create!()

get(Api, "/posts/#{post.id}/?include=foobar", status: 400)
end

# I put this as "may" because loading is an optional feature
Expand Down

0 comments on commit cb22a76

Please sign in to comment.