Skip to content
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

Merged
merged 2 commits into from
Feb 12, 2016
Merged

Follow up to #1454 #1504

merged 2 commits into from
Feb 12, 2016

Conversation

groyoh
Copy link
Member

@groyoh groyoh commented Feb 10, 2016

PR #1454 was merged with some missing fixes. All these fixes are addressed by this PR:

  • 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

@bf4 here you go ;)

end

def test_relationship_data_not_included
test_relationship({}, options: { include_data: false })
Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FOLLOWUP

@bf4
Copy link
Member

bf4 commented Feb 10, 2016

Awesome, thanks! Let's keep up the momentum

@bf4
Copy link
Member

bf4 commented Feb 10, 2016

@groyoh Can you add a Changelog entry at the top of whichever section makes sense to you?

@groyoh
Copy link
Member Author

groyoh commented Feb 10, 2016

@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
@groyoh
Copy link
Member Author

groyoh commented Feb 10, 2016

@bf4 I added the changelog. Should I also update the documentation or create another PR for this?

@bf4
Copy link
Member

bf4 commented Feb 10, 2016

If you're offering docs now is good :)

B mobile phone

On Feb 10, 2016, at 6:00 AM, Yohan Robert notifications@github.com wrote:

@bf4 I added the changelog. Should I also update the documentation or create another PR for this?


Reply to this email directly or view it on GitHub.

@groyoh
Copy link
Member Author

groyoh commented Feb 10, 2016

@bf4 since this change mainly concerns JSONAPI, should it go into docs/serializers.md next to the associations documentation or somewhere else?

@bf4
Copy link
Member

bf4 commented Feb 10, 2016

@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.
@groyoh
Copy link
Member Author

groyoh commented Feb 10, 2016

@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 data as nil because we would take the block return value as the relationship value. I added some specs for it too.

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
Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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 notifications@github.com 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
    
  •    :nil
    
    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.


Reply to this email directly or view it on GitHub.

@groyoh
Copy link
Member Author

groyoh commented Feb 10, 2016

Also please let me know if I should squash the commits.

autoload :Meta
autoload :Deserialization
require 'active_model/serializer/adapter/json_api/api_objects'
Copy link
Member

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?

Copy link
Member Author

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NEEDS FOLLOWUP

@groyoh
Copy link
Member Author

groyoh commented Feb 12, 2016

@scharfie have you tried with master or with my branch? #1454 indeed contained a bug and this PR should fix it.

@scharfie
Copy link

@groyoh Sorry, I just realized this - your branch does resolve the issue. Nice work 👍

@bf4 bf4 merged commit b1fd433 into rails-api:master Feb 12, 2016
@bf4
Copy link
Member

bf4 commented Feb 12, 2016

Merged!

@groyoh groyoh deleted the association-blocks branch February 20, 2016 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants