Skip to content

Commit

Permalink
Handle passing an invalid related resource in include params
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Feb 28, 2025
1 parent 9c5befb commit dbcdd1f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ class BaseController < ActionController::API
render json: { "errors": [ { "title": "Too many results" } ] }, status: :bad_request
end

rescue_from JSONAPI::Serializer::UnsupportedIncludeError do |exception|
render json: {
"errors": [
{
"title": "`#{exception.include_item}` is not in the list of supported relationships",
source: { pointer: "/include" }
}
]
}, status: :bad_request
end

private

def current_account
Expand Down
30 changes: 30 additions & 0 deletions spec/requests/open_ews_api/v1/jsonapi_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

require "rails_helper"

RSpec.resource "JSONAPI", document: false do
get "/v1/beneficiaries/:id" do
it "supports include related resources" do
beneficiary = create(:beneficiary)
create(:beneficiary_address, beneficiary:)

set_authorization_header_for(beneficiary.account)
do_request(id: beneficiary.id, include: "addresses")

expect(response_status).to eq(200)
expect(response_body).to match_jsonapi_resource_schema("beneficiary")
expect(json_response.dig("included", 0).to_json).to match_api_response_schema("address")
end

it "handles passing an invalid related resource" do
beneficiary = create(:beneficiary)
create(:beneficiary_address, beneficiary:)

set_authorization_header_for(beneficiary.account)
do_request(id: beneficiary.id, include: "foobar")

expect(response_status).to eq(400)
expect(response_body).to match_api_response_schema("jsonapi_error")
expect(json_response.dig("errors", 0, "source", "pointer")).to eq("/include")
end
end
end

0 comments on commit dbcdd1f

Please sign in to comment.