JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Bad links with engine mounted as root

Open
#1,303 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

This issue is a (choose one):

  • Problem/bug report.
  • Feature request.
  • Request for support. Note: Please try to avoid submitting issues for support requests. Use Gitter instead.

Checklist before submitting:

  • I've searched for an existing issue.
  • I've asked my question on Gitter and have not received a satisfactory answer.
  • I've included a complete bug report template. This step helps us and allows us to see the bug without trying to reproduce the problem from your description. It helps you because you will frequently detect if it's a problem specific to your project.
  • The feature I'm asking for is compliant with the JSON:API spec.

Description

As described on lib/jsonapi/link_builder.rb#L139,

def resource_url(source)
    "#{ base_url }#{ engine_mount_point }#{ resource_path(source) }"
end

But in this case:

constraints subdomain: 'api' do
    mount MyEngine::Engine, at: '/' if defined?(MyEngine::Engine)
  end

This creates a double / due to root value of engine_mount_point.

I guess a little update of:

@engine_mount_point = @engine ? @engine.routes.find_script_name({}) : ""

should be enough.

Current result:

{
    "data": {
        "id": "ed526d56-cc7e-41f4-8e99-fccf73fb7500",
        "type": "users",
        "links": {
            "self": "//v1/users/ed526d56-cc7e-41f4-8e99-fccf73fb7500"
        },
        "attributes": {
            "first_name": null,
            "last_name": null,
            "email": "some@thing.io"
        }
    }
}

//v1/users/ed526d56-cc7e-41f4-8e99-fccf73fb7500 should be /v1/users/ed526d56-cc7e-41f4-8e99-fccf73fb7500

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/jsonapi/link_builder.rb at resource_url, then inspect how engine_mount_point is computed for an engine mounted at '/'. Reproduce the reported root-mounted case and verify that the generated self link has one leading slash rather than '//v1/users/...'; the existing link-building behavior should remain correct for non-root mounts.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.