-
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
JsonApi PaginationLinks doesn't work outside of ActionController #1268
Comments
Looking at the code, I can't even see how this would work for ActionController. The SerializableResource filters the adapter options, so that they must be one of |
@jpaas good bug report. https://github.com/rails-api/active_model_serializers/blob/da7e6dc795ac4f6eb0af63c19c46638b13d0d04e/test/action_controller/json_api/pagination_test.rb should cover that it works I agree that we are sometime too eager to add foreign objects to the app. It should really be encapsulated. context should maybe be called 'request_context' and has two methods we use:
|
I was using a fork of grape-active_model_serializers https://github.com/Thanx/grape-active_model_serializers.git, but once I started using the jsonapi adapter, I built my own very simple grape json formatter:
I also had to create this little grape helper to pass my serializer options to the formatter via the env:
So now I can implement a grape endpoint like this and put metadata into the jsonapi response:
|
Oh and BTW, my workaround for this problem at the moment was to subclass the JsonApi adapter to simply skip links for now...
|
@jpaas would you want to submit the formatter here as a PR? Also, you should be able to just |
Ah yes @bf4 to_json alone works. I thought I tried that at one point but had trouble. I can submit as a PR. Where would you like it in the namespace? |
I'm thinking |
I just spent like 6 hours figuring this out. I'm using the same set of tools as @jpaas and I can get the links to work if I pass in the (bastardized) context where I would least expect it So I changed the code at #1268 (comment) to: # A grape response formatter that can be used as 'formatter :json, Grape::Formatters::ActiveModelSerializers'
# # Serializer options can be passed as a hash from your grape endpoint using env[:active_model_serializer_options],
# or better yet user the render helper in Grape::Helpers::ActiveModelSerializers
module Grape
module Formatters
module ActiveModelSerializers
RequestContext = Struct.new(:original_url, :query_parameters)
class << self
def call(resource, env) serializer_options = {}
serializer_options.merge!(env[:active_model_serializer_options]) if env[:active_model_serializer_options]
serializer_options[:context] = RequestContext.new(env['REQUEST_URI'],
env['rack.request.query_hash'])
ActiveModel::SerializableResource.new(resource, serializer_options).to_json(serializer_options)
end
end
end
end
end Without passing my little context hack into |
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - fixing last checkin - Adds intellij .idea file to .gitignore - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - rubocop corrections - Rebased and squashed commits, resubmitted to CI
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - fixing last checkin - Adds intellij .idea file to .gitignore - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - rubocop corrections - Rebased and squashed commits, resubmitted to CI - Remove .idea from gitignore - Add integration test
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - fixing last checkin - Adds intellij .idea file to .gitignore - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - rubocop corrections - Rebased and squashed commits, resubmitted to CI - Remove .idea from gitignore - Add integration test
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - fixing last checkin - Adds intellij .idea file to .gitignore - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - rubocop corrections - Rebased and squashed commits, resubmitted to CI - Remove .idea from gitignore - Add integration test
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - Add integration test - Refine scope of Grape version dependency - Assert that the response is equal to a manually defined JSON string - Add single module to include in Grape projects - Create a Serializable Resource to test rails-api from Grape - Update docs - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - Add integration test - Refine scope of Grape version dependency - Assert that the response is equal to a manually defined JSON string - Add single module to include in Grape projects - Create a Serializable Resource to test rails-api from Grape - Update docs - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2 - Updated Changelog
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - Add integration test - Refine scope of Grape version dependency - Assert that the response is equal to a manually defined JSON string - Add single module to include in Grape projects - Create a Serializable Resource to test rails-api from Grape - Update docs - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2 - Updated Changelog
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - Add integration test - Refine scope of Grape version dependency - Assert that the response is equal to a manually defined JSON string - Add single module to include in Grape projects - Create a Serializable Resource to test rails-api from Grape - Update docs - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2 - Updated Changelog
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - Add integration test - Refine scope of Grape version dependency - Assert that the response is equal to a manually defined JSON string - Add single module to include in Grape projects - Create a Serializable Resource to test rails-api from Grape - Update docs - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2 - Updated Changelog
- adds handling for when the returned resource is not serializable via ams - fix for when resource is an Array - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable. - Add integration test - Refine scope of Grape version dependency - Assert that the response is equal to a manually defined JSON string - Add single module to include in Grape projects - Create a Serializable Resource to test rails-api from Grape - Update docs - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2 - Updated Changelog
I am also getting the same issue. On executing Although, |
I've used @hut8's approach, but changed it for rails 4.1 and active_model_serializers version 0.10.rc3, to solve my problem. My problem was that I wanted to render a view using the Since I'm not doing this within the context of a
|
Is Grape support complete? I've followed this thread and updated to the latest
|
That's a bug. Please open a new issue B mobile phone
|
I'm trying to use Grape+Kaminari+AMS 1.0 (head) and I get a stack trace like so...
It seems to assume that the context has been set as an ActionDispatch::Request. I would try setting it, but I can't figure out how to get it in Grape.
The text was updated successfully, but these errors were encountered: