Skip to content

[2.4.0-beta1]: POST with self referencing inclusion returns null values (or default) in included entity #343

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
milosloub opened this issue Jul 18, 2018 · 2 comments · Fixed by #373
Labels

Comments

@milosloub
Copy link
Contributor

milosloub commented Jul 18, 2018

I have entity Client like this:

public class Client : Identifiable
    {
        [Attr("firstName")]
        public string FirstName  { get; set; }

        [Attr("lastName")]
        public string LastName  { get; set; }

        //[HasOne("advisor", Link.None)]
        //public Advisor Advisor { get; set; }
        //public int AdvisorId { get; set; }

        [HasOne("recommender", Link.None)]
        public Client Recommender { get; set; }
        public int? RecommenderId { get; set; }
    }

Trying to post like:
POST: http://localhost:5001/api/clients?include=recommender

{  
   "data":{  
      "type":"clients",
      "attributes":{  
         "firstName":"Mike",
         "lastName":"Razor"
      },
      "relationships":{  
         "recommender":{  
            "data":{  
               "type":"clients",
               "id":"1"
            }
         }
      }
   }
}

It returns:

{  
   "data":{  
      "attributes":{  
         "firstName":"Mike",
         "lastName":"Razor"
      },
      "relationships":{  
         "recommender":{  
            "data":{  
               "type":"clients",
               "id":"1"
            }
         }
      },
      "type":"clients",
      "id":"2"
   },
   "included":[  
      {  
         "attributes":{  
            "firstName":null,  // <--- This should be not null
            "lastName":null   // <--- This should be not null
         },
         "relationships":{  
            "recommender":{}
         },
         "type":"clients",
         "id":"1"
      }
   ]
}

This is caused only when there is self referencing inclusion (client include another client).
When I change POST with entity of another type (commented Advisor for example), POST with inclusion returns all data normally.

In version 2.3.1 it works fine. 2.3.2 and above has this problem

@milosloub
Copy link
Contributor Author

Is there any progress? Currently can't move from version 2.3.1, because one of the client standardly use POST with inclusion. So now I can´t use all those new sexy features after 2.3.1.
Maybe I can help, but I have no clue where to fix this in new relationship pointers system

@jaredcnance
Copy link
Contributor

jaredcnance commented Aug 10, 2018

This issue only occurs when you try to POST a resource with a relationship and try to return the relationship via ?include. In 2.3.2 we started using pointers to create these relationships rather than making an extra trip to the database to fetch the object. So, the reason the relationship attributes are null/default is because we never fetched the full object. I don't believe the self-reference actually matters in this case.

I have a PR open now (#373) although I hit a bit of a snag with the HasMany relationship loading. The current version works but it enumerates the pointers and fetches them from the db individually 😬. I would like to find a better solution before releasing it. If you want I can push it to MyGet. You just need to be aware that the APIs will likely change before it actually gets released.

Update: I've altered my approach and rather than reloading each relationships separately, I detach them from the context so EF is no longer tracking them. Then we can just re-query the newly created entity in the same way a GET /clients/:id request would.

jaredcnance added a commit that referenced this issue Aug 11, 2018
fix(#343): reload relationships from database if included during POST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 participants