Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/jsonapi/resource_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def add_included_object(id, object_hash, primary = false)
@included_objects[type] = {} unless @included_objects.key?(type)

if already_serialized?(type, id)
@included_objects[type][id][:object_hash].merge!(object_hash)
@included_objects[type][id][:object_hash].deep_merge!(object_hash)
set_primary(type, id) if primary
else
@included_objects[type].store(id, primary: primary, object_hash: object_hash)
Expand Down
67 changes: 67 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3599,3 +3599,70 @@ def test_whitelisted_error_in_controller
$PostProcessorRaisesErrors = false
end
end


class Api::BoxesControllerTest < ActionController::TestCase
def test_complex_includes_base
get :index
assert_response :success
end

def test_complex_includes_two_level
get :index, params: {include: 'things,things.user'}

assert_response :success

assert_equal '1', json_response['included'][0]['id']
assert_equal 'users', json_response['included'][0]['type']
assert_nil json_response['included'][0]['relationships']['things']['data']

# The test is hardcoded with the include order. This should be changed at some point since either thing could come first and still be valid
assert_equal '1', json_response['included'][1]['id']
assert_equal 'things', json_response['included'][1]['type']
assert_equal '1', json_response['included'][1]['relationships']['user']['data']['id']
assert_nil json_response['included'][1]['relationships']['things']['data']

assert_equal '2', json_response['included'][2]['id']
assert_equal 'things', json_response['included'][2]['type']
assert_equal '1', json_response['included'][2]['relationships']['user']['data']['id']
assert_nil json_response['included'][2]['relationships']['things']['data']
end

def test_complex_includes_things_nested_things
get :index, params: {include: 'things,things.things'}

assert_response :success

# The test is hardcoded with the include order. This should be changed at some point since either thing could come first and still be valid
assert_equal '2', json_response['included'][0]['id']
assert_equal 'things', json_response['included'][0]['type']
assert_nil json_response['included'][0]['relationships']['user']['data']
assert_equal '1', json_response['included'][0]['relationships']['things']['data'][0]['id']

assert_equal '1', json_response['included'][1]['id']
assert_equal 'things', json_response['included'][1]['type']
assert_nil json_response['included'][1]['relationships']['user']['data']
assert_equal '2', json_response['included'][1]['relationships']['things']['data'][0]['id']
end

def test_complex_includes_nested_things_secondary_users
get :index, params: {include: 'things,things.user,things.things'}

assert_response :success

assert_equal '1', json_response['included'][0]['id']
assert_equal 'users', json_response['included'][0]['type']
assert_nil json_response['included'][0]['relationships']['things']['data']

# The test is hardcoded with the include order. This should be changed at some point since either thing could come first and still be valid
assert_equal '2', json_response['included'][1]['id']
assert_equal 'things', json_response['included'][1]['type']
assert_equal '1', json_response['included'][1]['relationships']['user']['data']['id']
assert_equal '1', json_response['included'][1]['relationships']['things']['data'][0]['id']

assert_equal '1', json_response['included'][2]['id']
assert_equal 'things', json_response['included'][2]['type']
assert_equal '1', json_response['included'][2]['relationships']['user']['data']['id']
assert_equal '2', json_response['included'][2]['relationships']['things']['data'][0]['id']
end
end
70 changes: 70 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,33 @@

create_table :questionables, force: true do |t|
end

create_table :boxes, force: true do |t|
t.string :name
t.timestamps null: false
end

create_table :things, force: true do |t|
t.string :name
t.references :user
t.references :box

t.timestamps null: false
end

create_table :users, force: true do |t|
t.string :name
t.timestamps null: false
end

create_table :related_things, force: true do |t|
t.string :name
t.references :from, references: :thing
t.references :to, references: :thing

t.timestamps null: false
end

# special cases
end

Expand Down Expand Up @@ -538,6 +565,27 @@ class Make < ActiveRecord::Base
class WebPage < ActiveRecord::Base
end

class Box < ActiveRecord::Base
has_many :things
end

class User < ActiveRecord::Base
has_many :things
end

class Thing < ActiveRecord::Base
belongs_to :box
belongs_to :user

has_many :related_things, foreign_key: :from_id
has_many :things, through: :related_things, source: :to
end

class RelatedThing < ActiveRecord::Base
belongs_to :from, class_name: Thing, foreign_key: :from_id
belongs_to :to, class_name: Thing, foreign_key: :to_id
end

module Api
module V7
class Client < Customer
Expand Down Expand Up @@ -794,6 +842,11 @@ class NumerosTelefoneController < JSONAPI::ResourceController
end
end

module Api
class BoxesController < JSONAPI::ResourceController
end
end

### RESOURCES
class BaseResource < JSONAPI::Resource
abstract
Expand Down Expand Up @@ -1668,6 +1721,23 @@ def show
end
end

module Api
class BoxResource < JSONAPI::Resource
has_many :things
end

class ThingResource < JSONAPI::Resource
has_one :box
has_one :user

has_many :things
end

class UserResource < JSONAPI::Resource
has_many :things
end
end

### PORO Data - don't do this in a production app
$breed_data = BreedData.new
$breed_data.add(Breed.new(0, 'persian'))
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/boxes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
box_1:
id: 1
9 changes: 9 additions & 0 deletions test/fixtures/related_things.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
related_thing_1:
id: 1
from_id: 1
to_id: 2

related_thing_2:
id: 2
from_id: 2
to_id: 1
9 changes: 9 additions & 0 deletions test/fixtures/things.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
thing_1:
id: 1
user_id: 1
box_id: 1

thing_2:
id: 2
user_id: 1
box_id: 1
2 changes: 2 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
user_1:
id: 1
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ class CatResource < JSONAPI::Resource
jsonapi_resources :authors

namespace :api do
jsonapi_resources :boxes

namespace :v1 do
jsonapi_resources :people
jsonapi_resources :comments
Expand Down