Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Links won't use the page_base_url #319

Open
strzibny opened this issue May 7, 2019 · 7 comments
Open

Links won't use the page_base_url #319

strzibny opened this issue May 7, 2019 · 7 comments
Milestone

Comments

@strzibny
Copy link
Contributor

strzibny commented May 7, 2019

Example:

has_one :country,
    links: [
      related: "/countries/:country_id"
    ]

Now the API should serve it on the following URL:

config :ja_serializer,
  key_format: :camel_cased,
  page_base_url: "https://example.com/api/v1/"

But the page_base_url is not appended.

Do I miss something?

@beerlington
Copy link
Contributor

That should work, but if there's a base_url passed in when generating the links, that will take precedence. If you're able to reproduce the issue with a sample app, I am happy to investigate.

@strzibny
Copy link
Contributor Author

strzibny commented May 8, 2019

I realized it also happens for regular self links. The only place where it works is pagination.

Example blog app: https://github.com/strzibny/ja_serializer_links

Seeds will create one post and one comment. Then request http://localhost:4000/posts.

I am getting:

{"data":[{"attributes":{"body":"Body","excerpt":null,"tags":null,"title":"The Title"},"id":"1","links":{"self":"/posts/1"},"relationships":{"comments":{"links":{"related":"/posts/1/comments"}}},"type":"post"},{"attributes":{"body":"Body","excerpt":null,"tags":null,"title":"The Title"},"id":"2","links":{"self":"/posts/2"},"relationships":{"comments":{"links":{"related":"/posts/2/comments"}}},"type":"post"}],"jsonapi":{"version":"1.0"}}

and config has:

config :ja_serializer,
  key_format: :camel_cased,
  page_base_url: "https://example.com"

@beerlington beerlington added the needs-review Needs review from repo maintainer label May 8, 2019
@beerlington
Copy link
Contributor

@strzibny I noticed your example app isn't using Scrivener for pagination. The page_base_url is only applied when using that. See https://github.com/vt-elixir/ja_serializer#scrivener-integration. For links that are not using pagination, you'll need to prepend the base url in the view.

@strzibny
Copy link
Contributor Author

strzibny commented May 9, 2019

Okay, then it makes sense. As I said it works with pagination just not anywhere else. My original comment example would never work.

And if I understand correctly the default ja_serializer way is to only provide relative links. Feels a little strange to prepend pagination links, but not any other links. In another words, if people want to prepend links they have to make all links explicit and manually manage them, correct?

@beerlington
Copy link
Contributor

@strzibny to be honest, I don't know the history of why JaSerializer only provides relative links but allows you to prepend pagination links. In any app I've used JaSerializer for the backend, the base URL is always the same for all links, and it has been the client's responsibility to know what that URL is. I can't actually think of any cases where I've personally needed to change the base URL server-side, but that's just my personal experience with it.

@strzibny
Copy link
Contributor Author

strzibny commented May 16, 2019

I think my trouble is with the inconsistency of this and that if there is a base URL option that it's actually applied. I am fine with relative links, but mixing both seems wrong as clients have to check if the URL is relative or not.

@beerlington beerlington added help wanted and removed needs-review Needs review from repo maintainer labels May 17, 2019
@beerlington
Copy link
Contributor

I totally agree on the consistency thing. I would definitely be open to supporting the base url for non-paginated links.

@beerlington beerlington added the 1.0.0 1.0.0 label May 17, 2019
@beerlington beerlington added this to the 1.0 milestone May 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants