-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix #1759, Grape integration, adds serialization_context #1754
Fix #1759, Grape integration, adds serialization_context #1754
Conversation
@@ -505,6 +505,7 @@ def links_for(serializer) | |||
# prs: | |||
# https://github.com/rails-api/active_model_serializers/pull/1041 | |||
def pagination_links_for(serializer) | |||
return {} unless instance_options.key?(:serialization_context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not put this test there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref:
if is_collection && serializer.paginated?
hash[:links] ||= {}
hash[:links].update(pagination_links_for(serializer))
end
I did at first, but then I was thinking it's really tightly coupled to the paginationlinks object and it's intent isn't as obvious in the conditional.
But really we shouldn't let an empty serialization_context get in that far. This is a quick fix, but the better fix is at the boundary, I think
9b77f0a
to
6972c22
Compare
I spent too much time on this. Any blockers or anyone want to make changes? |
6972c22
to
ac67827
Compare
ac67827
to
f685c8c
Compare
@bf4 Sorry for the delay. Just seeing this, was a holiday weekend. I'll take a look and report any changes back |
FIRST_PAGE = 1 | ||
|
||
attr_reader :collection, :context | ||
|
||
def initialize(collection, adapter_options) | ||
@collection = collection | ||
@adapter_options = adapter_options | ||
@context = adapter_options.fetch(:serialization_context) | ||
@context = adapter_options.fetch(:serialization_context) do | ||
fail MissingSerializationContextError, 'JsonApi::PaginationLinks requires a'\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- fail MissingSerializationContextError, 'JsonApi::PaginationLinks requires a'\
+ fail MissingSerializationContextError, 'JsonApi::PaginationLinks requires a '\
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'
is use for defining the string here whereas "
is used below. Not sure if typo or if I'm missing something ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just because I have things in the others that need double quotes. rubocop caught it. changing to a heredoc
bfa1e35
to
159c529
Compare
@@ -8,6 +11,12 @@ module ActiveModelSerializers | |||
# | |||
# Example: To include pagination meta data: render(posts, meta: { page: posts.page, total_pages: posts.total_pages }) | |||
def render(resource, active_model_serializer_options = {}) | |||
active_model_serializer_options.fetch(:serialization_context) do | |||
active_model_serializer_options[:serialization_context] = ::ActiveModelSerializers::SerializationContext.new( | |||
original_url: request.url[/\A[^?]+/], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
original_url
option should be request_url
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops :)
Also 👍 to making a PR to my branch and moving this code to the formatter (and adding yourself in the changelog!) #1754 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also wasn't able to duplicate the failure in the grape tests, so if you could do that, that would be fantastic!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I don't use grape)
So the current solution works when an explicit
returns the |
@onomated if you would make a PR into my branch, that would be fantastic! I didn't check if I had all the objects I need in the formatter |
@onomated I just merged in your PR and will finish this up. Great work 💯 |
1a8ccef
to
d2b761a
Compare
@@ -2,14 +2,21 @@ module ActiveModelSerializers | |||
module Adapter | |||
class JsonApi < Base | |||
class PaginationLinks | |||
MissingSerializationContextError = Class.new(KeyError) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be defined within PaginationLinks
? It seems like it's more general than that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to put a guard wherever it's to be used... Can't think of a better spot...
See my 2 remarks. LGTM otherwise. |
@onomated bf4#4 (comment) so is this good for you? |
@bf4 couldn't quite resolve the comment link in the @ mention above, but on the general topic of pagination, it'll be awesome if this was configurable like so |
- improves improves serialization_context to take options and not depend on a `request` object. - adds descriptive error on missing serialization_context. - Document overriding `CollectionSerializer#paginated?`.
* Fix rails-api#1759, Grape integration, adds serialization_context - `serialization_context` is added in grape formatter so grape continues to render models without an explicit call to the `render` helper method - Made it straightforward for subclasses to add other serializer options (such as `serialization_scope`). * Updated Grape tests to include: - paginated collections - implicit Grape serializer (i.e. without explicit invocation of `render` helper method) * Update Changelog with fixes.
I missed that the code was changing global state.
d2b761a
to
1a9c622
Compare
@onomated I just rebased this off of master. Can you confirm it works for you before merge? |
@bf4 sure thing. Checking out now |
@bf4 All good! 👍 💯 |
Oh snap! Thanks, this is awesome! |
Thanks @onomated for your work here. This was actually a blocker for us for releasing 0.10.1 |
Just want to leave this here in case someone gets stuck with an error like: This formatter now depends on
or minimally:
|
@xn, I didn't encounter this as I use some other middleware that inherits from grape globals. Should this be mixed in as part of |
Well, if you just add it to the docs, then if someone wants/needs to monkey with |
PRs accepted :) |
@bf4 I'm happy to roll this into a documentation PR. Though I think it is better to allow the developer to meet the dependency, it is a question that should be answered by someone who has a stake in the project's maintainance/architecture. |
Not sure I follow. Do you think there is a problem in AMS impl or just docs needed? B mobile phone
|
AMS is kindly creating a grape formatter. Said formatter is depending on a grape middleware that is not enabled by default. You have a choice to automatically meet the dependency by hardcoding that dependency in the formatter itself or document the dependency to let the developer meet it on own their own terms. If I am being too circuitous, I apologize. Let me know and I will tell you what I believe should be done. |
I don't use grape, so I don't know what's right for you |
Ok, from basic OO principles you should let the developer meet the dependency. So, I'll create a PR for documentation to meet this in the README. |
on a
request
object.CollectionSerializer#paginated?
.Closes #1759