Core: WriteListener should use $persistResult if available?

Created on 5 Apr 2019  路  3Comments  路  Source: api-platform/core

Hi, I'm getting the "Unable to generate an IRI for the item of type.." error when using a custom DataPersister after sending a POST request. Reason being that the WriteListener is using the $controllerResult instead of $persistResult returned by the DataPersister.

In my specific case the "id" of the resource is generated in the DataPersister, the $controllerResult is not aware of this ID and therefore the iriConverter is unable to generate a valid IRI. In my case it would make sense to use the $persistResult, if available, to determine the IRI of the persisted resource. I might be missing other usecases though?

Has a PR bug

Most helpful comment

Fixed by #2693

All 3 comments

In the WriteListener, the current logic is

if ($hasOutput) {
    $request->attributes->set('_api_write_item_iri', $this->iriConverter>getIriFromItem($controllerResult));
 }

As described above, the $controllerResult contains an object which does not have an ID yet. This is set afterwards in the DataPerister.

The WriteListener does update the controller result correctly with the object returned by the DataPersister.

$event->setControllerResult($persistResult ?? $controllerResult);

So when generating the IRI, the new controller result should be used instead.

$request->attributes->set('_api_write_item_iri', $this->iriConverter->getIriFromItem($event->getControllerResult()));

Fixed by #2693

Small note: As the fix is not yet released, one might need to use
"api-platform/core": "2.4.x-dev" as composer requirement constraint.

Was this page helpful?
0 / 5 - 0 ratings