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)
@beechnut: I have a very simple workaround and it just ignores
klass
all together 😄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:
Resource:
Rails Console:
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:
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?
@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.