Skip to content

Can't create resource with relationship in Entity-Resource separation mode #445

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 Nov 12, 2018 · 6 comments · Fixed by #446
Closed

Can't create resource with relationship in Entity-Resource separation mode #445

milosloub opened this issue Nov 12, 2018 · 6 comments · Fixed by #446
Assignees
Labels

Comments

@milosloub
Copy link
Contributor

Consider following:

Entities:

    public class Client : Identifiable
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Parent Parent { get; set; }
        public int ParentId { get; set }
    }
    public class Parent: Identifiable
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

Resources:

    public class ClientResource : Identifiable
    {
        [Attr]
        public string FirstName { get; set; }
        [Attr]
        public string LastName { get; set; }
        [HasOne]
        public ParentResource Parent { get; set; }
    }
    public class ParentResource: Identifiable
    {
        [Attr]
        public string FirstName { get; set; }
        [Attr]
        public string LastName { get; set; }
    }

Three Layer flow (with Automapper Profile)
1) Controller - JsonApiController<ClientResource>
2) Service - EntityResourceService<ClientResource, Client, int>
3) Repository - DefaultEntityRepository<Client>

If I POST request like this:

{
    "data": {
        "attributes": {
            "first-name": "Homer",
            "last-name": "Simpson"
        },
        "relationships": {
            "parent": {
                "data": {
                    "type": "parents",
                    "id": "1"
                }
            }
        },
        "type": "clients"
    }
}

the InvalidOperationException is thrown, because:
"The entity type 'ParentResource' was not found. Ensure that the entity type has been added to the model."
This exception points to line 234 of DefaultEntityRepository and it's AttachHasOnePointers method.

Do you have any idea, how to handle this? Does this mean, that Repository should be responsible for proper resolve of navigation property type?

@roblankey
Copy link
Contributor

roblankey commented Nov 12, 2018

Hm. I would expect that to work just from looking at it quickly. The Course/Department example is very similar. There is no test for this specific scenario though, so let me add one and check it out.

@roblankey
Copy link
Contributor

Quick update. I do indeed get the same error when attempting to add the relationship like this. As a workaround, you can update the relationship after creation (e.g. RelationshipModifyTests)

@milosloub
Copy link
Contributor Author

@roblankey Thanks for the info. Unfortunately I can't do this as a workaround - several clients are connected to API using about 40 controllers, so I have to keep unseparated flow. Every other operations like inclusion, filter, sort etc. works with no problems. All my acceptance tests fails only in this one use-case. If I'm thinking about solution, it's quite hell of pain :(

@roblankey
Copy link
Contributor

Ok. Makes sense. Let me see what I can do.

@roblankey roblankey self-assigned this Nov 13, 2018
@roblankey roblankey mentioned this issue Nov 13, 2018
3 tasks
@roblankey
Copy link
Contributor

see #446
adds a withEntity property to the HasOne attribute that you would simply specify your entity property name within, similar to java's mappedBy

@milosloub
Copy link
Contributor Author

Cool! As @jaredcnance said, the same problem should be also with HasMany (I didnt't try, because we don't have this use-case...for now).
Thanks for this, it looks good!

jaredcnance added a commit that referenced this issue Nov 29, 2018
fix/#445: create resource with relationship in Entity-Resource separation mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants