Allow association definition to specify serializer #102
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Found myself trying to find out a way to specify associations on the serializer level, and realized it wasn't supported. Here's a first pass at implementing that functionality.
For some extra background, my use case was the following:
I have a resource that references objects of the same class OR subclasses. But for the serialization, I want them to be all serialized the same way:
Something along these lines
And this is what I wanted to be able to do:
The changes in this PR enable this kind of configuration.
Looking through the code, I realized that we're always passing the
options
passed to the serializer for the main resource to all theincluded
associations. Instead with this PR, we are passing the association options.There was one main issue highlighted by the tests, and I am not sure that my fix is in line with what the maintainers of this project want. Basically, if a resource is serialized twice (as the main resource and in the under the
included
key), with my change the one in theincluded
resource doesn't get the customization options from the serialization of the root resource.I think it makes sense. Please let me know if you disagree.
I would actually argue that it would be safer to require each association to explicitly specify the serializer instead of having the serializer infer from the object class. But that may not be a very popular opinion.