- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 158
Empty HasMany relationship data[] on requests without "includes" #542
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
Comments
It's definitely not by design, but I'm not sure if it's caused by your setup or if its a bug. I can look into it soon, but a small repro case would be helpful! |
Okay. I'll take a look and see if I can make a reproduction case for you! I wanted to make sure it wasn't by design before I went really nuts on tearing up my database configuration. Thanks. |
Hey. Below is some example JSON from an API response. I made a stripped-down version of my project to just be two models with bare bones attributes. I want to to clarify just so I'm expecting the right things: The relationships links work fine and return data as I'd expect. I'm also used to for a HasMany relationship, seeing the Am I right to expect that that API response from the PlanController.cs
I pasted my models and DB context below, I'm happy to send you a full project if you're interested, I feel like I'm missing something small and critical but I've checked over the documentation a dozen times and I can't seem to spot what. I removed everything but these two models + context, and two simple controllers, and am using model-first scaffolding the DB instead of DB-first. Plan.cs
PricingTier.cs
MyDataContext.cs
Thanks a ton for your help! Tremendously appreciated. |
So perusing the JSON API spec at https://jsonapi.org/format/#fetching-relationships-responses-200 says:
Content-Type: application/vnd.api+json
{
"links": {
"self": "/articles/1/relationships/tags",
"related": "/articles/1/tags"
},
"data": [
{ "type": "tags", "id": "2" },
{ "type": "tags", "id": "3" }
]
} ...which is what I'd expect from the above. So I guess I must have a misconfiguration somewhere in the above. (also here are my startup options for the above examples)
|
Thanks for the elaborate feedback!
Indeed, it gets a bit confusing here. Note that in your last two post (I think you're already aware of this), two different endpoints are involved for which the semantics are slightly different:
Note that for the latter the specs state:
so the (relationship) data must always be populated with resource identifier objects of every However, concerning the former (fetching resources endpoint): the specs are indifferent about having to include the relationship data. The specs only state that the data must a be valid resource object, for which the specs state that it may or may not include relationships objects. In conclusion: the specs are indifferent about whether the has-one (or has-many) relationships should be included in the dataset by default for the If you have a very specific setup with a particular behaviour that you require to be different for your application to work, I can look into that specific case for you and point you towards a work around until we've fixed this inconsistency. In that case I would need a repo I can checkout to and fiddle around with. |
@maurei Thanks for the fast and thorough response! So for context, I initially came across this problem when Ember frontend hooked up to my original (un-trimmed) app seemed to be breaking on the empty On reading your response, where you point out that I'll fiddle around with it a little bit and report back. Just for my own curiosity, do you have a link to that issue on the backlog, and do you know of any example of a custom setup that you mention above where the inconsistency is resolved? Thanks much! I really appreciate your help. |
If you get an empty Apart from this issue, we didn't have a dedicated issue for this yet. The closest we had related to this is #504. This is because I believe the inconsistent behaviour is caused by the serializer, which in turn is strongly related to the serializer being tightly coupled with the rest of the framework through Let me know what you find by fiddling around! |
Okay so I stepped through the library code a little bit, through the
Using the above models (a
...which always created an empty list of PricingTiers instead of a null one, which was returned as the empty Data[] in the returned JSON. I was able to have the HasMany relationship's data array included the way I wanted by adding the following to the constructor of my
...which just adds the
...which is exactly what I wanted. So I think with that small addendum, I'm basically set. It involves including a little bit of extra boilerplate code in controllers, but since the JsonApiControllers are so lean as-is, that doesn't really strike me as the end of the world. |
Thanks for the investigations. I'm leaving this issue open as long as this behaviour has been fixed, but I'm taking that you have a viable work-around for now. |
This is fixed in #558 |
Description
Hi folks. I'm seeing some odd behavior setting up / fetching relationships. I have two entities,
subscription
andpricingTier
.pricingTier
hasManysubscription
andsubscription
hasOnepricingTier
, which seems straightforward enough.When requesting
/api/pricing-tier
, I see:...note the empty array under
data
for the HasMany, but not the HasOne relationship toplan
Requesting
/api/pricing-tier?inclue=subscription
instead gets me:...plus the "included" collection that I'd expect.
I'm a little confused as to whether this is an error somewhere in my model setup or this is by design. I think I followed the examples reasonably closely, and to make sure it wasn't a problem with my DB-first approach, I've been using migrations on a fresh DB, but the problem has persisted.
Any help greatly appreciated! If it's not by design and it's a problem with my setup, I can provide the relevant file excerpts.
Thanks!
Environment
The text was updated successfully, but these errors were encountered: