vt-elixir / ja_serializer

JSONAPI.org Serialization in Elixir.
Other
640 stars 148 forks source link

Links won't use the page_base_url #319

Open strzibny opened 5 years ago

strzibny commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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