emcousin / grape-jsonapi Goto Github PK
View Code? Open in Web Editor NEWUse jsonapi-serializer with Grape
License: MIT License
Use jsonapi-serializer with Grape
License: MIT License
Following Netflix/fast_jsonapi#433, it appears that Netflix is no longer maintaining fast_jsonapi. A fork has been initiated and seems to be well maintained and up-to-date.
Let's update the fast_jsonapi
dependency of this gem so that it points to this fork, and ensure nothing is broken in the process.
Maybe some performance benchmarks?
Hello,
Thanks for the great gem. I'm currently using Grape + this gem, and I'm having some issues regarding the content-type. I can GET without problems, however patching/posting/putting is giving me problems:
error: "The provided content-type 'application/vnd.api+json' is not supported."
The CURL I'm doing is the following:
curl 'http://localhost:5000/api/v1/users/1' -H 'Origin: http://localhost:3000' -H 'access-token: JB3pVL_Zg5u3zO6V2PsLQg' -H 'client: D8QkWH9542kk9BtnUzZpgQ' -H 'Content-Type: application/vnd.api+json' -H 'Accept: application/vnd.api+json' -H 'expiry: 1554285246' -H 'uid: [email protected]' -H 'Referer: http://localhost:3000/profile' -H 'token-type: Bearer' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36' --data-binary '{"data":{"id":1,"type":"user","attributes":{"test":true}}}' --compressed
Hello and thank you for this great gem.
I'm just implementing this formatter, but it's not initializing.
Uninitialized Constant Grape::Formatter::Jsonapi
Bundler: 2.2.24
Rubygems: 3.2.25
Both are latest
The gem is quite simple, yet I can't parse the problem myself. My setup is identical to the readme, only my first class using Grape::API is one module deeper for versioning purposes. I.e.,
API
-Base.rb *<---Here're the three lines from the readme inside of module API
and class Base < Grape::API
--V1
---Lots of API controllers
I suspect this is a problem with my implementation and/or bundler and rubygems, so no problem if you close it out right. I appreciate your help.
Thank for this library/gem. It made my integration/setup of a json:api API using swagger very easy.
I have two questions with regards to the integration with grape-swagger
.
PageSerializer
declare some link to the Page
model?Any tips or hints?
Basically i want to be able to support https://github.com/ruby-grape/grape-swagger#response-documentation-
It looks like you can provide custom parsers, but I haven't really dug much more into this
https://github.com/ruby-grape/grape-swagger#model_parsers
Just wondering if you have tried adding response support for swagger + grape + fast_jsonapi, or if there is an approach that isn't going to end up being a major headache?
First of all, thank you for this gem. :)
But is it really necessary to pull Rails as a dependency? I see that it's specified as "development" dependency in gemspec
file, but there's an explicit require 'rails'
statement in lib/grape_fast_jsonapi.rb:3
, which prevents it from being used with, for example, Sinatra-based apps.
I'm probably overthinking this, but is there a cleaner way for specifying the serializer when you may have two serializers that share a type?
Here's standard usage when serializing a User
instance.
#[...]
user = User.last # AR record
render user, { params: { current_user: current_user } }
Here's usage when I want to render the typical User
instance, but using a thinner version of the serializer. Notice how it's a bit anti-pattern, probably because I wasn't sure how to cover this case.
#[...]
user = User.last # AR record
jsonapi_options = {}
render CompactUserSerializer.new(user, jsonapi_options).serialized_json
Respective serializers below
class UserSerializer < BaseSerializer
include FastJsonapi::ObjectSerializer
attributes :first_name, :last_name, :name, :bio, :email, :image_url
# More complex/expensive serialization
attribute :some_other_property do |user|
user.some_other_property
end
attribute :current_user_following do |user, params|
params[:current_user] ? params[:current_user].follows?(user) : false
end
# [...]
end
class CompactUserSerializer < BaseSerializer
include FastJsonapi::ObjectSerializer
set_type :user
attributes :first_name, :last_name, :name, :bio, :image_url
end
This is probably bad practice to begin with because it's definitely not DRY, I wonder if i should just be utilizing fast_jsonapi
's conditional attributes and utilize passed params to branch on the same serializer.
#[...]
user = User.last # AR record
render user, { params: { current_user: current_user, compact_version: true } }
Could probably also use a combination of mixins to make the serializer more readable
module AuthenticatedProperties
attribute :foo, if: Proc.new { |record, params|
params[:compact_version] == false
}
# [...]
end
class UserSerializer < BaseSerializer
include FastJsonapi::ObjectSerializer
include AuthenticatedProperties
attributes :first_name, :last_name, :name, :bio, :image_url
# [...]
end
Good afternoon !
I have a question regarding an edge case that I am experiencing using the gem.
Context:
I am sometimes sending to the render
method an empty array - if there are no objects to return - , and in addition some metadata passed as options:
Example:
response = []
metadata = {pagination: { page: 1, page_size: 2}}
render response, meta: metadata
I get in reply only
[]
This surprises me, because I was expecting the below reply, which seems for me consistent with the jsonapi specification for empty data:
{
"data": [],
"meta": {
"pagination": {
"page": 1,
"page_size": 2
}
}
}
The []
answer is due to the fact that, by the implementation, serializable_class
method will render nil
on an empty array, thus the method serialize_resource
will then perform a resource.map(&:serializable_hash)
, which ultimately leads to an empty array.
To override this, and get my expected answer payload, I have to do this little trick:
render data: response, meta: metadata
Questions:
To be sure that we both understand the same thing about the jsonapi
specification for empty data, could you confirm that for empty array of data, the expected return of the render
method should be:
{
"data": []
}
If not, how should I use the render
method for an empty array of data and my additional options ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.