-
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
Follow up to #1454 #1504
Follow up to #1454 #1504
Conversation
72b5699
to
8856736
Compare
end | ||
|
||
def test_relationship_data_not_included | ||
test_relationship({}, options: { include_data: false }) |
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.
This case should probably raise an error since JSONAPI does not allow the relationship to be empty (http://jsonapi.org/format/#document-resource-object-relationships). I'll let you guys decide how to procede with this one.
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.
Depends if the conditions are programmer error. Generated nil values aren't included... So? In this case, is it always programmer error? Should it create an error response?
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.
In this case, it would most likely be a programmer mistake who would do something like:
has_one :author do
include_data false
end
which is kind of useless but that may happen. This should be the concern of AMS to make sure that the serializer either does not serializer the whole relationship in such case (not usefull in my opinion) or raise an error to let the programmer know that this is a misuse of the gem.
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.
FOLLOWUP
Awesome, thanks! Let's keep up the momentum |
@groyoh Can you add a Changelog entry at the top of whichever section makes sense to you? |
@bf4 I'll add it to the changelog 😉 |
PR rails-api#1454 was merged with some missing fixes. All these fixes are addressed by this commit: - Rename ActiveModel::Serializer::Adapter::JsonApi::Association to ActiveModel::Serializer::Adapter::JsonApi::Relationship - Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship and ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier to ActiveModel::Serializer::Adapter::JsonApi::ApiObjects module - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::Relationship - Add unit test for ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
8856736
to
2c41938
Compare
@bf4 I added the changelog. Should I also update the documentation or create another PR for this? |
If you're offering docs now is good :) B mobile phone
|
@bf4 since this change mainly concerns JSONAPI, should it go into |
@groyoh right now the serializers include some jsonapi concerns. As long as it's part of the serializer, I think it makes sense to put in that file, but include more usage info on it in the jsonapi or adapter section |
When using the relationships DSL with a block e.g. has_one :bio do link :self, "some_link" end the "data" field would be rendered with a nil value even though the bio is not nil. This happened because the block return value was set to nil but used as a value for the "data" field.
@bf4 @beauby I updated the PR because I figured out that #1454 actually had a bug. When using a block for a relationship like: has_one :bio do
link :self, "whatever"
end
# => { data: nil, links: { self: "whatever" } } would always render I am unsure about the solution though, let me know what you think. |
@@ -42,17 +42,17 @@ def initialize(*) | |||
|
|||
def link(name, value = nil, &block) | |||
@_links[name] = block || value | |||
nil | |||
:nil |
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.
Not sure if this should be called :nil
or something else. I actually wanted to have your input on this as I was not too creative here.
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.
this is to signify something like :block_returned_nil_value
?
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.
Yes. For more details, there are two use cases to consider for the relationship block:
- Where the block return the value we want to use for the relationship:
has_one :author do
meta some: :thing
object.own_author
end
- Where the block return some value to define that we want to use the default relationship:
has_one :author do
meta some: :thing
end
For the latest, I defined the value to be :nil
but it could be change to anything else if a better suggestion pops up since this is the internal API.
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.
Maybe create an issue or start a pr to discuss any improvements
B mobile phone
On Feb 12, 2016, at 6:11 PM, Yohan Robert [email protected] wrote:
In lib/active_model/serializer/reflection.rb:
@@ -42,17 +42,17 @@ def initialize(*)
def link(name, value = nil, &block) @_links[name] = block || value
nil
Yes. For more details, there are two use cases to consider for the relationship block::nil
Where the block return the value we want to use for the relationship:
has_one :author do
meta some: :thing
object.own_author
end
Where the block return some value to define that we want to use the default relationship:
has_one :author do
meta some: :thing
end
For the latest, I defined the value to be :nil but it could be change to anything else if a better suggestion pops up since this is the internal API.—
Reply to this email directly or view it on GitHub.
Also please let me know if I should squash the commits. |
autoload :Meta | ||
autoload :Deserialization | ||
require 'active_model/serializer/adapter/json_api/api_objects' |
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.
So, in https://github.com/rails-api/active_model_serializers/pull/1004/files#r52563837 I noticed that we've already started a pattern of sticking the json_api objects in the json_api folder. e.g. above we have Meta and Link which are also json_api object.
In code I removed I move that object into json_api/jsonapi.rb
. What do you think about just sticking also the api objects in the json_api
folder as we've essentially already been doing?
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.
It's fine by me. Should I still keep the ApiObjects
namespace?
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.
NEEDS FOLLOWUP
@groyoh Sorry, I just realized this - your branch does resolve the issue. Nice work 👍 |
Merged! |
PR #1454 was merged with some missing fixes. All these fixes are addressed by this PR:
ActiveModel::Serializer::Adapter::JsonApi::Association
toActiveModel::Serializer::Adapter::JsonApi::Relationship
Move ActiveModel::Serializer::Adapter:: JsonApi::Relationship
andActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
toActiveModel::Serializer::Adapter::JsonApi::ApiObjects
moduleActiveModel::Serializer::Adapter::JsonApi::Relationship
ActiveModel::Serializer::Adapter::JsonApi::ResourceIdentifier
@bf4 here you go ;)