Another issue found during our tests with JSON API and JSON API Extras - we have an entity with a relationship to another entity, both of which are using JSON API Extras to override their URL paths and field names. When testing POSTs and PATCHes of this entity, we found that since the incoming data had a relationship type that respected the overrides (i.e. "module" instead of "config_entity--module"), it was throwing an exception as it tried to use the entityTypeManager->getStorage() with the overridden relationship type string.
I have a patch that resolves the issue by fetching the originalID and replacing the relationship data with the correct string if they don't match. There's probably a better way to address this, but it does allow for our records to be POSTed and PATCHed with the same data as they output from a GET.
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | 2886618--rel-type-conversion.patch | 5.39 KB | dpolant |
| #18 | jsonapi-n2886618-18.interdiff.txt | 3.43 KB | damienmckenna |
| #18 | jsonapi-n2886618-18.patch | 7.05 KB | damienmckenna |
Comments
Comment #2
logickal commentedComment #3
logickal commentedComment #5
logickal commentedOooops. I used getJsonapiResourceConfig(), which comes from jsonapi_extras. I'll have to find a better way to get the original entity ID back.
Comment #6
logickal commentedI'm sure there's a better way to do this, but this is at least more straightforward and doesn't require jsonapi_extras.
Comment #7
logickal commentedWorked on the unit test to more accurately reflect the case, but feeling like this is a very naive solution and could use more thought.
Comment #9
logickal commentedOk, this patch is passing tests on my local, but it's really only good enough to get our specific implementation unblocked. It seems to me we have an interesting problem to solve here in that if someone uses jsonapi_extras to override type names, it not only breaks the ability to post content with those relationships, but there doesn't seem to be any good way to expose that change back to jsonapi.
I'll freely admit to possibly being dense here, so any additional ideas around this would be greatly appreciated - I'm not sure if perhaps this needs to roll back down to jsonapi_extras to implement its own deserialization functions that would be able to handle any override mappings properly...
Comment #10
mrjmd commentedComment #11
e0ipsoSorry for dropping the ball here.
Did you try using the
$resource_typeto see if the name is changed? It should have a name translation method you can use.You can also get the
$type_idfrom there (I think).Comment #12
damienmckennaLoop over each $relationship['data'] item.
Comment #14
damienmckennaThe $resource_type points to the primary object, not the referenced object, as can be seen from this patch. So that doesn't work.
Comment #15
damienmckennaComment #17
damienmckennaI missed some changes for that last patch. This better illustrates the issue - $resource_type doesn't point to the referenced entity type.
Comment #18
damienmckennaThis appears to work. A key problem I had was that when examining a relationship I didn't know what relationship was currently being processed, so by changing from array_map() to a foreach() I can find that out, and then match it up against the "publicName" of the fields, then identify the correct entity type based upon the settings of the entityreference field.
Food for thought.
Comment #20
logickal commentedThe big problem with #18 is that it again relies on getJsonapiResourceConfig(), which comes from jsonapi_extras. I ran into this on #2. Maybe there is a case to be made to move that method into the main jsonapi module?
I also know that there are some other relationship issues with metadata that probably need to be solved in parallel with this. so It might make sense to solve both problems with one refactor of denormalize().
Comment #21
damienmckennaMoving this to JSON API Extras, because that's where the "override" functionality comes from.
Comment #22
dpolant commentedThis patch should take care of translating the relationship > data > type into a viable entity type without relying on methods from JSONAPI Extras.
Comment #23
dpolant commentedI believe this work needs to be done in JSONAPI core, so moving it back.
Comment #24
logickal commentedWe have this working and fully tested in our stack - @e0ipso, let us know your thoughts on the approach here.
Comment #26
e0ipsoThanks everyone for the work and the patience. This is a superb patch!