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/link_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def formatted_module_path_from_class(klass)
scopes = module_scopes_from_class(klass)

unless scopes.empty?
"/#{ scopes.map{ |scope| format_route(scope.to_s.underscore) }.join('/') }/"
"/#{ scopes.map{ |scope| format_route(scope.to_s.underscore) }.compact.join('/') }/"
else
"/"
end
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,13 @@ class PersonResource < JSONAPI::Resource
end
end

module OptionalNamespace
module V1
class PersonResource < JSONAPI::Resource
end
end
end

module MyEngine
module Api
module V1
Expand All @@ -1950,6 +1957,13 @@ class PersonResource < JSONAPI::Resource
end
end
end

module OptionalNamespace
module V1
class PersonResource < JSONAPI::Resource
end
end
end
end

module ApiV2Engine
Expand Down
19 changes: 19 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ class CatResource < JSONAPI::Resource
jsonapi_resources :people
end
end

namespace :optional_namespace, path: 'optional_namespace' do
namespace :v1, path: '' do
jsonapi_resources :people
end
end
end

ApiV2Engine::Engine.routes.draw do
Expand Down Expand Up @@ -670,3 +676,16 @@ def unformat(value)
end
end
end

class OptionalRouteFormatter < JSONAPI::RouteFormatter
class << self
def format(route)
return if route == 'v1'
super
end

def unformat(formatted_route)
super
end
end
end
28 changes: 28 additions & 0 deletions test/unit/serializer/link_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ def test_query_link_for_regular_app_with_dasherized_scope
assert_equal expected_link, builder.query_link(query)
end

def test_query_link_for_regular_app_with_optional_scope
config = {
base_url: @base_url,
route_formatter: OptionalRouteFormatter,
primary_resource_klass: OptionalNamespace::V1::PersonResource
}

query = { page: { offset: 0, limit: 12 } }
builder = JSONAPI::LinkBuilder.new(config)
expected_link = "#{ @base_url }/optional_namespace/people?page%5Blimit%5D=12&page%5Boffset%5D=0"

assert_equal expected_link, builder.query_link(query)
end

def test_query_link_for_engine
config = {
base_url: @base_url,
Expand Down Expand Up @@ -344,6 +358,20 @@ def test_query_link_for_engine_with_dasherized_scope
assert_equal expected_link, builder.query_link(query)
end

def test_query_link_for_engine_with_optional_scope
config = {
base_url: @base_url,
route_formatter: OptionalRouteFormatter,
primary_resource_klass: MyEngine::OptionalNamespace::V1::PersonResource
}

query = { page: { offset: 0, limit: 12 } }
builder = JSONAPI::LinkBuilder.new(config)
expected_link = "#{ @base_url }/boomshaka/optional_namespace/people?page%5Blimit%5D=12&page%5Boffset%5D=0"

assert_equal expected_link, builder.query_link(query)
end

def test_query_link_for_engine_with_camel_case_scope
config = {
base_url: @base_url,
Expand Down