Skip to content

HasManyThrough relationships always return empty data:[] array on GET #453

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
XaserAcheron opened this issue Dec 4, 2018 · 5 comments · Fixed by #456
Closed

HasManyThrough relationships always return empty data:[] array on GET #453

XaserAcheron opened this issue Dec 4, 2018 · 5 comments · Fixed by #456

Comments

@XaserAcheron
Copy link
Contributor

Description

Seems when trying to GET a record that contains a HasManyThrough relationship, it returns an empty data: [] array even though I'm not specifying ?include. Meanwhile, HasMany relationships seem to work just fine.

Here's a somewhat-anonymized sample response (comments added) which hopefully illustrates the issue:

{
    "data": [
        {
            "attributes": {
                "title": "Sample Article",
                "text": "...",
                "create-date": "2018-08-14T16:19:13.653"
            },
            "relationships": {
                "comments": { // HasMany relationship (works correctly)
                    "links": {
                        "self": "/api/articles/1/relationships/comments",
                        "related": "/api/articles/1/comments"
                    }
                },
                "authors": { // HasManyThrough relationship
                    "links": {
                        "self": "/api/articles/1/relationships/authors",
                        "related": "/api/articles/1/authors"
                    },
                    "data": [] // <-- this shouldn't be here
                }
            },
            "type": "articles",
            "id": "1"
        }
    ]
}

To clarify, the article has authors, and they're returned successfully when I ?include them, so this empty array is a bit of a red herring that's monkeying up the client application (ember-data).

I did stumble across this issue and double-checked to make sure I'm not initializing any lists anywhere, and it doesn't seem so. Not sure what else, if anything, might be configured differently in my project versus the repo example, so I'm a bit stumped.

Environment

  • JsonApiDotNetCore Version: 3.0.0
  • Other Relevant Package Versions: [?]
@jaredcnance
Copy link
Contributor

Yeah the only time I've seen this issue is when the list is initialized somewhere. Can you post your model? One thing you might try is adding a log entry (or setting a breakpoint) to the setter:

(roughly)

public class Article : Identifiable {
  private List<Tag> _tags;

  [HasManyThrough("tag", nameof(ArticleTags))]
  public List<Tag> Tags { 
    get => _tags 
    set {
      Console.WriteLine("ArticleTags set with size " + value?.Length);
      Console.WriteLine(Environment.StackTrace);
       _tags = value;
    }
  } 
}

@XaserAcheron
Copy link
Contributor Author

XaserAcheron commented Dec 5, 2018

Thanks for the response! I gave the logging a shot, and it doesn't seem that set gets called for either Tags or ArticleTags (nothing logged, no breakpoints hit). I put some breakpoints on the gets as well, and the get for ArticleTags does get called -- the value is null, as it should be.

It'll take me just a bit to post up the models. The example is anonymized so I gotta shuffle the names around and make sure I didn't break anything. :P

[EDIT] Welp, uh... substitute "Author" for "Tags", I guess. Forgot my example was a bit different than the built-in one.

@jaredcnance
Copy link
Contributor

Okay no worries. It’s probably not your model then and it’s likely an issue at the serialization later of the framework. I’ll try to work on it this week.

@XaserAcheron
Copy link
Contributor Author

Bit of a stab in the dark, but do you think this function could be the culprit? It seems like it's going to ship back an empty IEnumerable<IIdentifiable> if throughProperty is null, which sounds a lot like the symptoms here -- though as a disclaimer I've never monkeyed around with return yield so I'm a bit rusty on the inner workings.

XaserAcheron added a commit to XaserAcheron/JsonApiDotNetCore that referenced this issue Dec 6, 2018
XaserAcheron added a commit to XaserAcheron/JsonApiDotNetCore that referenced this issue Dec 6, 2018
@XaserAcheron
Copy link
Contributor Author

Yup, that was it. Just made a PR. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants