Skip to content

Commit

Permalink
fix: validate type matches resource type for sparse fieldsets (#1461)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkiczula authored Nov 21, 2024
1 parent 42a11b7 commit d3c094b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/jsonapi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def parse_fields(resource_klass, fields)
if type_resource.nil?
fail JSONAPI::Exceptions::InvalidResource.new(type, error_object_overrides)
else
verify_type(type, type_resource)
unless values.nil?
valid_fields = type_resource.fields.collect { |key| format_key(key) }
values.each do |field|
Expand Down
24 changes: 24 additions & 0 deletions test/unit/jsonapi_request/jsonapi_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ def test_parse_dasherized_with_underscored_include
assert_equal 'iso_currency is not a valid includable relationship of expense-entries', request.errors[0].detail
end

def test_parse_fields_singular
params = ActionController::Parameters.new(
{
controller: 'expense_entries',
action: 'index',
fields: {expense_entry: 'iso_currency'}
}
)

request = JSONAPI::Request.new(
params,
{
context: nil,
key_formatter: JSONAPI::Formatter.formatter_for(:underscored_key)
}
)

e = assert_raises JSONAPI::Exceptions::InvalidResource do
request.parse_fields(ExpenseEntryResource, params[:fields])
end
refute e.errors.empty?
assert_equal 'expense_entry is not a valid resource.', e.errors[0].detail
end

def test_parse_fields_underscored
params = ActionController::Parameters.new(
{
Expand Down

0 comments on commit d3c094b

Please sign in to comment.