Skip to content

Commit 0216d47

Browse files
bdunhamchingor13
authored andcommitted
Unformat error source parameters using the prescribed key_formatter (#230)
* Run key_formatter.unformat on field error responses so field names are assigned correctly * Added test for key formatted attribute error messages
1 parent 9828422 commit 0216d47

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/json_api_client/resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def save
398398
if last_result_set.has_errors?
399399
last_result_set.errors.each do |error|
400400
if error.source_parameter
401-
errors.add(error.source_parameter, error.title || error.detail)
401+
errors.add(self.class.key_formatter.unformat(error.source_parameter), error.title || error.detail)
402402
else
403403
errors.add(:base, error.title || error.detail)
404404
end

test/unit/server_side_error_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,18 @@ def test_can_handle_validation_strings
3232
assert_equal ["Email address is invalid"], user.errors.full_messages
3333
end
3434

35+
def test_can_handle_key_formatted_attribute_validation_strings
36+
with_altered_config(User, :json_key_format => :dasherized_key) do
37+
stub_request(:post, "http://example.com/users")
38+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
39+
errors: [{source: {pointer: "/data/attributes/email-address"}, title: "Email address is invalid"}]
40+
}.to_json)
41+
42+
user = User.create(name: 'Bob', email_address: 'invalid email')
43+
assert !user.persisted?
44+
assert user.errors.present?
45+
assert_equal ["Email address is invalid"], user.errors[:email_address]
46+
end
47+
end
48+
3549
end

0 commit comments

Comments
 (0)