Skip to content
Open
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
29 changes: 29 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,18 @@ class AuthorsController < JSONAPI::ResourceController
end

### CONTROLLERS
module NestedApi
class PostsController < ActionController::Base
include JSONAPI::ActsAsResourceController
def context
{writer_id: params[:writer_id]}
end
end
class WritersController < ActionController::Base
include JSONAPI::ActsAsResourceController
end
end

module Api
module V1
class AuthorsController < JSONAPI::ResourceController
Expand Down Expand Up @@ -1208,6 +1220,23 @@ def custom_links(options)
end
end

module NestedApi
class WriterResource < JSONAPI::Resource
attributes :name
model_name 'Person'
has_many :posts
end

class PostResource < JSONAPI::Resource
attributes :title, :body
has_one :writer, foreign_key: 'author_id', class_name: 'Writer'

def self.records(options = {})
context = options.fetch(:context, {})
super.where(author_id: context[:writer_id])
end
end
end

module Api
module V1
Expand Down
70 changes: 70 additions & 0 deletions test/integration/requests/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,54 @@ def test_pagination_empty_results
# assert_equal 'This is comment 18 on book 1.', json_response['data'][9]['attributes']['body']
# end

def test_nested_route_relation_links
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.json_key_format = :underscored_key
JSONAPI.configuration.route_format = :underscored_route

get "/nested_api/writers/#{$test_user.id}/posts"
assert_jsonapi_response 200
post_1 = json_response['data'][0]
assert_hash_equals({
'self' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts/#{post_1['id']}/relationships/writer",
'related' => "http://www.example.com/nested_api/writers/#{$test_user.id}"
}, post_1['relationships']['writer']['links'])
ensure
JSONAPI.configuration = original_config
end

def test_nested_route_pagination_links
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.json_key_format = :underscored_key
JSONAPI.configuration.route_format = :underscored_route
NestedApi::PostResource.paginator :paged

get "/nested_api/writers/#{$test_user.id}/posts?page[size]=1"
assert_jsonapi_response 200
assert_hash_equals({
'first' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'next' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts?page%5Bnumber%5D=2&page%5Bsize%5D=1",
'last' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts?page%5Bnumber%5D=3&page%5Bsize%5D=1"
}, json_response['links'])
ensure
JSONAPI.configuration = original_config
end

def test_nested_route_self_links
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.json_key_format = :underscored_key
JSONAPI.configuration.route_format = :underscored_route

get "/nested_api/writers/#{$test_user.id}/posts"
assert_jsonapi_response 200
post_1 = json_response['data'][0]
assert_hash_equals({
'self' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts/#{post_1['id']}"
}, post_1['links'])
ensure
JSONAPI.configuration = original_config
end


def test_flow_self
get '/posts'
Expand All @@ -504,6 +552,28 @@ def test_flow_self
assert_hash_equals post_1, json_response['data']
end

def test_flow_link_to_one_self_link_for_nested_resources
original_config = JSONAPI.configuration.dup
JSONAPI.configuration.json_key_format = :underscored_key
JSONAPI.configuration.route_format = :underscored_route

get "/nested_api/writers/#{$test_user.id}/posts"
assert_jsonapi_response 200
post_1 = json_response['data'][0]

get post_1['relationships']['writer']['links']['self']
assert_jsonapi_response 200
assert_hash_equals({
'links' => {
'self' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts/#{post_1['id']}/relationships/writer",
'related' => "http://www.example.com/nested_api/writers/#{$test_user.id}"
},
'data' => {type: 'people', id: $test_user.id.to_s}
}, json_response)
ensure
JSONAPI.configuration = original_config
end

def test_flow_link_to_one_self_link
get '/posts'
assert_jsonapi_response 200
Expand Down
6 changes: 6 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ class CatResource < JSONAPI::Resource
jsonapi_resources :books
jsonapi_resources :authors

namespace :nested_api do
jsonapi_resources :writers do
jsonapi_resources :posts
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@senid231 , did you try to change
jsonapi_resources :posts
to
jsonapi_related_resources :posts
?

end
end

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