JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Links broken when used with `scope module:` routes

未关闭
#591 17 条评论 12 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Type: Bug
主要语言
Ruby
星标
2.3k
派生
546
PR 合并指标
30 天内没有已合并 PR

描述

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

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 lib/jsonapi/link_builder.rb#L102 开始,将其中对模块路径的处理方式与 issue 中展示的 Rails 路由进行比较。复现 Api/V1/MyResource 示例,并查看 #361 了解相关背景。当 scope module: 移除 namespace 时,响应链接使用 /api/my-resource 而不是 /api/v1/my-resource,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
rails, ruby
领域
api, backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。