Skip to content

Enable use of an object key in serilaized forms #1354

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

Closed
wants to merge 2 commits into from

Conversation

noahsilas
Copy link
Contributor

Because all serializers already implement an object method (part of the API expected when customizing a serializer), attempting to add a key to the serialized object with the name object would not output the expected value, and would instead dump a serialized version of the object under serialization.

This prevents a commonly used identifier from appearing in serialized output. As one real world example, the Stripe API includes an object key in each serialized object describing its type.

I introduced a change in the storage for the serializer configuration, and added methods to access it in the same way as before. Open question: I stored the key/attribute mapping in a Hash<Symbol => Symbol>; would the project be better served mapping to a nested hash to support additional configuration options in the future without changing the storage again?

This may have introduced a breaking change; before, methods were defined on the serializer based on the name of the key; now they will be defined based on the name of the underlying attribute. This behavior doesn't seem to be explicitly mentioned in docs or tested anywhere, though, so it may be a non-issue.

Because all serializers already implement an `object` method (part of
the API expected when customizing a serializer), attempting to add a key
to the serialized object with the name `object` would not output the
expected value, and would instead dump a serialized version of the
object under serialization.

Instead of having attributes directly map to methods on the serializer,
we introduce one layer of abstraction: the `_attributes_map`. This hash
maps the key names expected in the output to the names of the
implementing methods.

This simplifies some things (removing the need to maintain both
`_attributes` and `_attribute_keys`), but does add some complexity.
@bf4
Copy link
Member

bf4 commented Nov 30, 2015

@noahsilas Thanks for this. I believe this will be resolved by a PR such as @beauby 's #1262

@noahsilas
Copy link
Contributor Author

@bf4 I tried checking that out locally and adding my test case, but it still fails:

 FAIL["test_object_attribute_override", ActiveModel::Serializer::AttributeTest, 0.16997674852609634]
 test_object_attribute_override#ActiveModel::Serializer::AttributeTest (0.17s)
        --- expected
        +++ actual
        @@ -1 +1 @@
        -{:blog=>{:object=>"AMS Hints"}}
        +{:blog=>{:object=>#<Blog:0xXXXXXX @attributes={:id=>1, :name=>"AMS Hints", :type=>"stuff"}>}}
        /home/noah/src/active_model_serializers/test/serializers/attribute_test.rb:96:in `test_object_attribute_override'

I'll start looking into #1262 and see how I can resolve my commit against it.

@bf4
Copy link
Member

bf4 commented Nov 30, 2015

@noahsilas I'll have a PR soon, just some finishing touches

adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
assert_equal({ blog: { object: 'AMS Hints' } }, adapter.serializable_hash)
end

Copy link
Member

Choose a reason for hiding this comment

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

nice

@NullVoxPopuli
Copy link
Contributor

Just requires a little more inline documentation, but otherwise LGTM! :-)

@noahsilas
Copy link
Contributor Author

@NullVoxPopuli: This ended up getting merged via #1356 as 7bde7bf. I'm going to close this PR, but I'll come back later with another to improve those docs.

@noahsilas noahsilas closed this Dec 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants