Skip to content

Error PATCH'ing with json relationship #282

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
joshhubers opened this issue May 10, 2018 · 7 comments
Closed

Error PATCH'ing with json relationship #282

joshhubers opened this issue May 10, 2018 · 7 comments
Labels

Comments

@joshhubers
Copy link
Contributor

I'll do my best to be brief and descriptive.

I should mention the application has two DbContexts and so far everything has been working well.

I have two seperate IDbContextResolvers, one for each of my database contexts. I DI the resolvers, and have seperate base repositories for each database connection and repositories for each model that extend from the two repositories. (As per #269 ).

I have a model EntityA which has a has-one relationship with EntityB

public class EntityA : Identifiable<int>
  [InverseProperty("EntityAs")]
  public virtual EntityB EntityB { get; set; }

EntityB has-many EntityA's

public class EntityB : Identifiable<int>
    [InverseProperty("EntityB")]
    [HasMany("EntityAs")]
    public ICollection<EntityA> EntityAs { get; set; }

Calling a PATCH to EntityB endpoint works as expected when I DO NOT include the json relationships: ... which include EntityA's

Calling a PATCH to EntityB endpoint errors when PATCH'ing with relationships included...

 fail: JsonApiDotNetCore.Formatters.JsonApiReader[0]
       An error occurred while de-serializing the payload  JsonApiDotNetCore.Internal.JsonApiException: Failed to deserialize request body ---> System.InvalidOperationException: Unable to resolve service for type 'JsonApiDotNetCore.Data.
IDbContextResolver' while attempting to activate 'JsonApiDotNetCore.Internal.Generics.GenericProcessor`1[EntityA]'.

Endpoints for both EntityA and EntityB work fine, with no issues or other errors. Both EntityA and EntityB belong to the same repository and should fall under the same context resolver. I don't believe I'm missing anything obvious as everything else has been working perfectly, and I'm not sure if it's an issue because of the multiple IDbContextResolvers or not.

@jaredcnance
Copy link
Contributor

@joshhubers this should be fixed by #254.

There is some mixing of concerns in the de-serialization process that required this call:

var genericProcessor = _genericProcessorFactory.GetProcessor<IGenericProcessor>(typeof(GenericProcessor<>), attr.Type);

The original approach to updating relationships, was to actually fetch the relationship so EF would track it and it could be updated 😞 . However, #254 is removing that requirement. The reason you are getting the above error is because when we try to resolve the GenericProcessor<EntityA>, it is failing to resolve IDbContextResolver:

public GenericProcessor(IDbContextResolver contextResolver)

So, while the error you're currently experiencing will be fixed soon, I think there may still be some other lurking issues, especially when trying to use the new implementation of json:api v1.1 operations.

@joshhubers
Copy link
Contributor Author

joshhubers commented May 21, 2018

@jaredcnance

So I started using your 2.2.2-alpha1-0075 branch in my project. As per #254 .

It has been working issue free, until I try to set the relationship to null/unset the relationship.
Such as PATCH'ing "relationships":{"EntityB":{"data":null}}

Do you know of this issue or have a workaround?

Edit: I should mention that the request goes through successfully, but the returned data still contains the relationship and the change does not persist.

@joshhubers
Copy link
Contributor Author

@jaredcnance

Any word on this?

@milosloub
Copy link
Contributor

@joshhubers

I had same problem. The temp solution for me is define extra "reset" route to unset relationship and make second request after Patch. The most similar http method is HttpDelete, so for your case it should be:

        [HttpDelete("reset-entityb/{id}")]
        public async Task<IActionResult> ResetEntityB(int id)
        {
            var removed = await _service.RemoveEntityBAsync(id);
            if (!removed)
                return NotFound();

            return Ok();
        }

After fix, this route can be removed and start using standard Patch

@jaredcnance
Copy link
Contributor

jaredcnance commented Jun 5, 2018

@joshhubers sorry for the delay on this. I'm working on wrapping up a few other items but should be able to finish this out this week.

Thanks @joshhubers and @milosloub for grabbing the alpha and providing feedback! That is super helpful.

@jaredcnance
Copy link
Contributor

jaredcnance commented Jun 9, 2018

The error you hit around dropping relationships has been fixed on #254...if you don't mind, can you try pulling the latest pre-release (2.2.5-alpha1-0086)? If that works for you guys, then I think that PR is just about ready to merge.

@joshhubers
Copy link
Contributor Author

@jaredcnance Looks to be working on my end!

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

No branches or pull requests

3 participants