Open dulleh opened 7 years ago
I do something similar with #108 😃
@Kevinrob your PR does not resolve the issue as the returned object will be parsed just for the fields in the interface rather than as it's own object.
In case that's not clear, here's what I mean: if we have a Parent
model with a child
field that can receive a Son
or Daughter
(implements Child
) from the API, the json will be parsed as a Child
rather than a Son
or Daughter
. We want to be parsing a Son
or Daughter
depending on what's received from the API.
@dulleh I don't think that what you said it's right. I use it in production and relationships declared with an interface are deserialized to the correct type.
It seems @Kevinrob is right, sorry about the confusion. I was under the impression that when you do something like Parent item = new Child()
it casts the item
as a Parent
and therefore loses any additional information that a Child
has (it doesn't). I've learned something fundamental about inheritance, thanks for helping me get there.
This means that #137 is no longer necessary as #108 already accomplishes what the PR attempts. However, I do think having polymorphic relationships explicitly defined is beneficial for models compared to having the responsibility fall on developers to comment their models with the return types. I don't know which @jasminb would prefer for this library of course, and there still remains the bug with RelationshipLinks
in #137.
So we have an issue with the kitsu.io api where a relationship we need can return one of two different types (in our case,
mediaRelationship.source
can be either ananime
or amanga
). I've written a test case which fails for unknown reasons, and was thinking perhaps you could help.The test consists of a parent object that contains two child objects with the same
@Relationship
annotation as so:The test:
The json to be parsed (
polymorph-relationship.json
):This gives the output:
parsed as author
This passes so the assumption is that changing the object returned by the api to a
User
object will also pass:However, this fails with the following error:
Is it possible that the library can be made to leave relationships as null if the returned type is different? Perhaps to prevent errors for regular relationships being ignore a
@PolymorphRelationship
could be created?