omeka / omeka-s

Omeka S is a web publication system for universities, galleries, libraries, archives, and museums. It consists of a local network of independently curated exhibits sharing a collaboratively built pool of items, media, and their metadata.
GNU General Public License v3.0
408 stars 136 forks source link

Deleting an item using REST API produces 500 error #2234

Closed wragge closed 1 week ago

wragge commented 1 week ago

Omeka-S version: 4.1.1

When I try to delete an item using the REST API I get a 500 error, even though the item is deleted as expected.

Here's a minimal example in Python that creates a new item, deletes it, then checks to see if it has really been deleted:

import os
import requests
from dotenv import load_dotenv
load_dotenv()

# Get keys from .env file
key_identity = os.getenv("KEY_IDENTITY")
key_credential = os.getenv("KEY_CREDENTIAL")

# Prepare authentication
auth_params = {
    "key_identity": key_identity, 
    "key_credential": key_credential
}

# Create a simple payload
payload = {
    "dcterms:title": [
        {
            "type": "literal",
            "property_id": 1,
            "property_label": "Title",
            "is_public": True,
            "@value": "Example Title"
        }
    ]
}

# Create a new item
create_response = requests.post("https://timsherratt.org/collections/api/items", json=payload, params=auth_params)
# HTTP status code is 200 (ok)
print(create_response.status_code)
# Get item id
item_id = create_response.json()["@id"]
# Display item id
print(item_id)

# Delete the item we just created
delete_response = requests.delete(item_id, params=auth_params)
# HTTP status code is 500 (server error)
print(delete_response.status_code)

# Try to get the deleted item
get_response = requests.get(item_id)
# HTTP status code is 404 (not found)
print(get_response.status_code)

Running this code produces the following output:

200
https://timsherratt.org/collections/api/items/1100
500
404

The error and traceback in the Omeka logs is:

2024-11-04T02:48:15+00:00 ERR (3): Doctrine\ORM\ORMInvalidArgumentException: Binding entities to query parameters only allowed for entities that have an identifier. in /home/timsherr/public_html/collections/vendor/doctrine/orm/lib/Doctrine/ORM/ORMInvalidArgumentException.php:202
Stack trace:
#0 /home/timsherr/public_html/collections/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php(435): Doctrine\ORM\ORMInvalidArgumentException::invalidIdentifierBindingEntity()
#1 /home/timsherr/public_html/collections/vendor/doctrine/orm/lib/Doctrine/ORM/Query.php(456): Doctrine\ORM\AbstractQuery->processParameterValue(NULL)
#2 /home/timsherr/public_html/collections/vendor/doctrine/orm/lib/Doctrine/ORM/Query.php(400): Doctrine\ORM\Query->resolveParameterValue(Object(Doctrine\ORM\Query\Parameter))
#3 /home/timsherr/public_html/collections/vendor/doctrine/orm/lib/Doctrine/ORM/Query.php(316): Doctrine\ORM\Query->processParameterMappings(Array)
#4 /home/timsherr/public_html/collections/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php(1181): Doctrine\ORM\Query->_doExecute()
#5 /home/timsherr/public_html/collections/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php(1135): Doctrine\ORM\AbstractQuery->executeIgnoreQueryCache(NULL, 1)
#6 /home/timsherr/public_html/collections/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php(871): Doctrine\ORM\AbstractQuery->execute(NULL, 1)
#7 /home/timsherr/public_html/collections/application/src/Api/Adapter/AbstractResourceEntityAdapter.php(632): Doctrine\ORM\AbstractQuery->getResult()
#8 /home/timsherr/public_html/collections/application/src/Api/Representation/AbstractResourceEntityRepresentation.php(424): Omeka\Api\Adapter\AbstractResourceEntityAdapter->getSubjectValuesSimple(Object(Omeka\Entity\Item), NULL, NULL, NULL)
#9 /home/timsherr/public_html/collections/application/src/Api/Representation/AbstractResourceEntityRepresentation.php(116): Omeka\Api\Representation\AbstractResourceEntityRepresentation->subjectValuesForReverse()
#10 /home/timsherr/public_html/collections/application/src/Api/Representation/AbstractResourceRepresentation.php(73): Omeka\Api\Representation\AbstractResourceEntityRepresentation->getJsonLd()
#11 [internal function]: Omeka\Api\Representation\AbstractResourceRepresentation->jsonSerialize()
#12 /home/timsherr/public_html/collections/vendor/laminas/laminas-json/src/Json.php(384): json_encode(Object(Omeka\Api\Representation\ItemRepresentation), 15)
#13 /home/timsherr/public_html/collections/vendor/laminas/laminas-json/src/Json.php(349): Laminas\Json\Json::encodeViaPhpBuiltIn(Object(Omeka\Api\Representation\ItemRepresentation), false)
#14 /home/timsherr/public_html/collections/vendor/laminas/laminas-json/src/Json.php(121): Laminas\Json\Json::encodeValue(Object(Omeka\Api\Representation\ItemRepresentation), false, Array, false)
#15 /home/timsherr/public_html/collections/vendor/laminas/laminas-view/src/Renderer/JsonRenderer.php(150): Laminas\Json\Json::encode(Object(Omeka\Api\Representation\ItemRepresentation))
#16 /home/timsherr/public_html/collections/application/src/View/Renderer/ApiJsonRenderer.php(71): Laminas\View\Renderer\JsonRenderer->render(Object(Omeka\Api\Representation\ItemRepresentation))
#17 /home/timsherr/public_html/collections/vendor/laminas/laminas-view/src/View.php(194): Omeka\View\Renderer\ApiJsonRenderer->render(Object(Omeka\View\Model\ApiJsonModel))
#18 /home/timsherr/public_html/collections/vendor/laminas/laminas-mvc/src/View/Http/DefaultRenderingStrategy.php(98): Laminas\View\View->render(Object(Omeka\View\Model\ApiJsonModel))
#19 /home/timsherr/public_html/collections/vendor/laminas/laminas-eventmanager/src/EventManager.php(319): Laminas\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Laminas\Mvc\MvcEvent))
#20 /home/timsherr/public_html/collections/vendor/laminas/laminas-eventmanager/src/EventManager.php(171): Laminas\EventManager\EventManager->triggerListeners(Object(Laminas\Mvc\MvcEvent))
#21 /home/timsherr/public_html/collections/vendor/laminas/laminas-mvc/src/Application.php(360): Laminas\EventManager\EventManager->triggerEvent(Object(Laminas\Mvc\MvcEvent))
#22 /home/timsherr/public_html/collections/vendor/laminas/laminas-mvc/src/Application.php(341): Laminas\Mvc\Application->completeRequest(Object(Laminas\Mvc\MvcEvent))
#23 /home/timsherr/public_html/collections/index.php(21): Laminas\Mvc\Application->run()
zerocrates commented 1 week ago

This one looks like an error with us trying to render the JSON to return after the deletion is done, I think a regression that was introduced when we added @reverse to the API.

jimsafley commented 1 week ago

This should be fixed in b192c2d3cb07fd84ea03b58a5b9b781b0b87597e. Thanks, @wragge, for this excellent bug report!