Skip to content

Commit b56eaa5

Browse files
committed
Unformat the linkage type.
The linkage key was not being unformatted and was causing errors if there was a multiword key (because the raw key would not match the association name). This change unformats the key which fixes that bug.
1 parent 2cc9e61 commit b56eaa5

5 files changed

Lines changed: 110 additions & 37 deletions

File tree

lib/jsonapi/request.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def parse_has_one_links_object(raw)
233233
end
234234

235235
{
236-
type: raw['type'],
236+
type: unformat_key(raw['type']).to_s,
237237
id: raw['id']
238238
}
239239
end
@@ -281,7 +281,7 @@ def parse_params(params, allowed_fields)
281281
# Since we do not yet support polymorphic associations we will raise an error if the type does not match the
282282
# association's type.
283283
# ToDo: Support Polymorphic associations
284-
if links_object[:type] && (links_object[:type] != association.type.to_s)
284+
if links_object[:type] && (links_object[:type].to_s != association.type.to_s)
285285
raise JSONAPI::Exceptions::TypeMismatch.new(links_object[:type])
286286
end
287287

test/controllers/controller_test.rb

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,28 @@ def test_create_validations
16481648
assert_response :success
16491649
end
16501650

1651+
def test_update_link_with_dasherized_type
1652+
JSONAPI.configuration.json_key_format = :dasherized_key
1653+
set_content_type_header!
1654+
put :update,
1655+
{
1656+
id: 3,
1657+
data: {
1658+
id: '3',
1659+
type: 'people',
1660+
links: {
1661+
'hair-cut': {
1662+
linkage: {
1663+
type: 'hair-cuts',
1664+
id: '1'
1665+
}
1666+
}
1667+
}
1668+
}
1669+
}
1670+
assert_response :success
1671+
end
1672+
16511673
def test_create_validations_missing_attribute
16521674
set_content_type_header!
16531675
post :create,
@@ -1705,37 +1727,46 @@ def test_valid_filter_value
17051727
end
17061728

17071729
def test_get_related_resource
1730+
JSONAPI.configuration.json_key_format = :dasherized_key
1731+
JSONAPI.configuration.route_format = :underscored_key
17081732
get :get_related_resource, {post_id: '2', association: 'author', :source=>'posts'}
17091733
assert_response :success
1710-
assert_hash_equals json_response,
1711-
{
1712-
data: {
1713-
id: '1',
1714-
type: 'people',
1715-
name: 'Joe Author',
1716-
email: 'joe@xyz.fake',
1717-
dateJoined: '2013-08-07 16:25:00 -0400',
1718-
links: {
1719-
self: 'http://test.host/people/1',
1720-
comments: {
1721-
self: 'http://test.host/people/1/links/comments',
1722-
related: 'http://test.host/people/1/comments'
1723-
},
1724-
posts: {
1725-
self: 'http://test.host/people/1/links/posts',
1726-
related: 'http://test.host/people/1/posts'
1727-
},
1728-
preferences: {
1729-
self: 'http://test.host/people/1/links/preferences',
1730-
related: 'http://test.host/people/1/preferences',
1731-
linkage: {
1732-
type: 'preferences',
1733-
id: '1'
1734-
}
1735-
}
1736-
}
1737-
}
1738-
}
1734+
assert_hash_equals(
1735+
{
1736+
data: {
1737+
id: '1',
1738+
type: 'people',
1739+
name: 'Joe Author',
1740+
email: 'joe@xyz.fake',
1741+
"date-joined": '2013-08-07 16:25:00 -0400',
1742+
links: {
1743+
self: 'http://test.host/people/1',
1744+
comments: {
1745+
self: 'http://test.host/people/1/links/comments',
1746+
related: 'http://test.host/people/1/comments'
1747+
},
1748+
posts: {
1749+
self: 'http://test.host/people/1/links/posts',
1750+
related: 'http://test.host/people/1/posts'
1751+
},
1752+
preferences: {
1753+
self: 'http://test.host/people/1/links/preferences',
1754+
related: 'http://test.host/people/1/preferences',
1755+
linkage: {
1756+
type: 'preferences',
1757+
id: '1'
1758+
}
1759+
},
1760+
"hair-cut" => {
1761+
"self" => "http://test.host/people/1/links/hair_cut",
1762+
"related" => "http://test.host/people/1/hair_cut",
1763+
"linkage" => nil
1764+
}
1765+
}
1766+
}
1767+
},
1768+
json_response
1769+
)
17391770
end
17401771

17411772
def test_get_related_resource_nil

test/fixtures/active_record.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
t.string :email
1313
t.datetime :date_joined
1414
t.belongs_to :preferences
15+
t.integer :hair_cut_id, index: true
1516
t.timestamps null: false
1617
end
1718

@@ -142,6 +143,10 @@
142143
t.float :item_cost
143144
t.timestamps null: false
144145
end
146+
147+
create_table :hair_cuts, force: true do |t|
148+
t.string :style
149+
end
145150
end
146151

147152
### MODELS
@@ -150,6 +155,7 @@ class Person < ActiveRecord::Base
150155
has_many :comments, foreign_key: 'author_id'
151156
has_many :expense_entries, foreign_key: 'employee_id', dependent: :restrict_with_exception
152157
belongs_to :preferences
158+
belongs_to :hair_cut
153159

154160
### Validations
155161
validates :name, presence: true
@@ -439,6 +445,7 @@ class PersonResource < JSONAPI::Resource
439445
has_many :posts
440446

441447
has_one :preferences
448+
has_one :hair_cut
442449

443450
filter :name
444451

@@ -561,6 +568,11 @@ def self.verify_key(key, context = nil)
561568
end
562569
end
563570

571+
class HairCutResource < JSONAPI::Resource
572+
attribute :style
573+
has_many :people
574+
end
575+
564576
class IsoCurrencyResource < JSONAPI::Resource
565577
primary_key :code
566578
attributes :name, :country_name, :minor_unit
@@ -703,6 +715,7 @@ def subject
703715
PreferencesResource = PreferencesResource.dup
704716
EmployeeResource = EmployeeResource.dup
705717
FriendResource = FriendResource.dup
718+
HairCutResource = HairCutResource.dup
706719
end
707720
end
708721

test/fixtures/hair_cuts.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mohawk:
2+
id: 1
3+
style: mohawk

test/unit/serializer/serializer_test.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ def test_serializer_limited_fieldset
131131
end
132132

133133
def test_serializer_include
134+
serialized = JSONAPI::ResourceSerializer.new(
135+
PostResource,
136+
include: [:author]
137+
).serialize_to_hash(PostResource.new(@post))
134138

135139
assert_hash_equals(
136140
{
@@ -189,16 +193,26 @@ def test_serializer_include
189193
type: 'preferences',
190194
id: '1'
191195
}
196+
},
197+
hairCut: {
198+
self: "/people/1/links/hairCut",
199+
related: "/people/1/hairCut",
200+
linkage: nil
192201
}
193202
}
194203
}
195204
]
196205
},
197-
JSONAPI::ResourceSerializer.new(PostResource, include: [:author]).serialize_to_hash(
198-
PostResource.new(@post)))
206+
serialized
207+
)
199208
end
200209

201210
def test_serializer_key_format
211+
serialized = JSONAPI::ResourceSerializer.new(
212+
PostResource,
213+
include: [:author],
214+
key_formatter: UnderscoredKeyFormatter
215+
).serialize_to_hash(PostResource.new(@post))
202216

203217
assert_hash_equals(
204218
{
@@ -257,14 +271,17 @@ def test_serializer_key_format
257271
type: 'preferences',
258272
id: '1'
259273
}
274+
},
275+
hair_cut: {
276+
self: '/people/1/links/hairCut',
277+
related: '/people/1/hairCut',
278+
linkage: nil
260279
}
261280
}
262281
}
263282
]
264283
},
265-
JSONAPI::ResourceSerializer.new(PostResource,
266-
include: [:author],
267-
key_formatter: UnderscoredKeyFormatter).serialize_to_hash(PostResource.new(@post))
284+
serialized
268285
)
269286
end
270287

@@ -565,6 +582,10 @@ def test_serializer_include_has_one_sub_objects_only
565582
end
566583

567584
def test_serializer_different_foreign_key
585+
serialized = JSONAPI::ResourceSerializer.new(
586+
PersonResource,
587+
include: ['comments']
588+
).serialize_to_hash(PersonResource.new(@fred))
568589

569590
assert_hash_equals(
570591
{
@@ -592,6 +613,11 @@ def test_serializer_different_foreign_key
592613
self: "/people/2/links/preferences",
593614
related: "/people/2/preferences",
594615
linkage: nil
616+
},
617+
hairCut: {
618+
self: "/people/2/links/hairCut",
619+
related: "/people/2/hairCut",
620+
linkage: nil
595621
}
596622
}
597623
},
@@ -654,7 +680,7 @@ def test_serializer_different_foreign_key
654680
}
655681
]
656682
},
657-
JSONAPI::ResourceSerializer.new(PersonResource, include: ['comments']).serialize_to_hash(PersonResource.new(@fred))
683+
serialized
658684
)
659685
end
660686

0 commit comments

Comments
 (0)