jsonapi-resources: Links broken when used with `scope module:` routes

Hi,

we use a version parameter inside of the Accept header to be able to switch Api versions. The different versions are grouped in modules. And to remove these modules as namespaces from the generated URLs we use scope module:. This works great but the problem ist that the generated links inside a response still include the module.

The code just checks the module hierarchy of the resource class and completely ignores how rails generates the routes. (See: https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/link_builder.rb#L102). Also see #361 for a similar problem.

As a workaround we monkey patch the formatted_module_path_from_class method to return the correct path \api\. But that is not an ideal solution.

An example route file:

namespace :api do
  scope module: :v1, constraint: ApiConstraint.new(version: 1) do
    jsonapi_resources :my_resource
  end
end

And an example resource:

module Api
  module V1
    class MyResource < JSONAPI::Resource
      [...]
    end
  end
end

So Rails generates routes like: /api/my-resource But the generated links look like this: /api/v1/my-resource

Sascha

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 12
  • Comments: 17 (4 by maintainers)

Commits related to this issue

Most upvoted comments

@beechnut: I have a very simple workaround and it just ignores klass all together 😄

module JSONAPI
  class LinkBuilder
    def formatted_module_path_from_class(klass)
      '/api/'
    end
  end
end

I just checked and it should be possible to create links via Rails but I do not know why we need this big LinkBuilder klass. I assume there is some hidden complexity.

What I testes was the following:

Routes:

namespace :api do
  scope module: :v1, constraint: ApiConstraint.new(version: 1) do
    jsonapi_resources :my_resource
  end
end

Resource:

module Api
  module V1
    class MyResource < JSONAPI::Resource
      model_name 'MyResourceModel'
      [...]
    end
  end
end

Rails Console:

app.url_for(Api::V1::MyResource.new(MyResourceModel.last, nil)._model)
# "http://www.example.com/api/my-resource/ffd9304a-036a-4b14-a871-8e2c339d5de2"

So it should be possible to somehow use Rails to create the links. Unfortunately I have no time at the moment and to be honest I do not know JSON API well enough to know all the requirements for resource linking.

Also there seems to be a lot of code that is dedicated to create links and I just looked at this one method.

I think that you’re on the way using app.url_for, however I’m not sure it’s perfect just yet. This is mainly due to the fact that the Rails Routes have a variety of options that can be used:

  • subdomain constraints
  • other custom constraints (e.g. ApiConstraints as given above to provide versioning constraints)
  • path: nil

Really we should be using ActionDispatch::Routing::UrlFor if we’re relying on the Rails Routing engine for inbound routing and not extracting the routing to elsewhere.

As such, we do need to know the controller and action to use in order to determine the route, yes?

include UrlFor
url_for(controller: 'users',
        action: 'new',
        message: 'Welcome!',
        only_path: true)

@ntippie your solution looks like it will work but is highly configured and needs mapping tightly with the selected routing structure.

Reading through the commentary with Devise routing (rabbit warren of https://github.com/cerebris/jsonapi-resources/issues/380 » https://github.com/rails/rails/issues/21231 » https://github.com/plataformatec/devise/pull/3714/files ) I’m now looking into how the Routing methods of JSONAPI::Resource are dealt with.

👍 thanks for the workarounds! but a fix would be splendid

Borrowing from the gem’s LinkBuilder tests, I added the following hacks to my app, for the moment. With this, my links are pointing to the right place.

I’m matching the slash at the end because doing the one at the beginning would catch the ‘/api’ in http://api.example.com.

# test/lib/jsonapi_test.rb

require 'test_helper'

class JSONAPITest < ActiveSupport::TestCase

  def setup
    @base_url        = "http://example.com"
    @route_formatter = JSONAPI.configuration.route_formatter
    @search          = searches(:exact) # Grabs one of my fixtures
  end

test "route without /api in path" do
    primary_resource_klass = API::SearchResource

    config = {
      base_url: @base_url,
      route_formatter: @route_formatter,
      primary_resource_klass: primary_resource_klass,
    }

    builder = JSONAPI::LinkBuilder.new(config)
    source  = primary_resource_klass.new(@search, nil)
    expected_link = "#{ @base_url }/searches/#{ source.id }"

    assert_equal expected_link, builder.self_link(source)
  end
end
# lib/extensions/jsonapi.rb

module JSONAPI
  class LinkBuilder

    private

    def formatted_module_path_from_class(klass)
      scopes = module_scopes_from_class(klass)

      unless scopes.empty?
        # The Main Hack: adding the #gsub
        "/#{ scopes.map(&:underscore).join('/') }/".gsub(/api\//, '')
      else
        "/"
      end
    end

  end
end