Correctly parse @id referenced entities in API

Fixes issue #1370
This commit is contained in:
Jan Böhmer 2026-05-25 22:37:48 +02:00
parent 0da5befd7b
commit 2e223f4ee4
2 changed files with 34 additions and 4 deletions

View file

@ -63,9 +63,15 @@ class SkippableItemNormalizer implements NormalizerInterface, DenormalizerInterf
// check (line 271). For abstract resource classes with a discriminator map (e.g. Attachment), this // check (line 271). For abstract resource classes with a discriminator map (e.g. Attachment), this
// fails because the array has no _type key. Fix by resolving IRI strings directly. // fails because the array has no _type key. Fix by resolving IRI strings directly.
// See: https://github.com/Part-DB/Part-DB-server/issues/1370 // See: https://github.com/Part-DB/Part-DB-server/issues/1370
if (is_string($data)) { if (is_string($data) || (is_array($data) && isset($data['@id']) && is_string($data['@id']))) {
if (is_array($data)) {
$iri = $data['@id'];
} else {
$iri = $data;
}
try { try {
return $this->iriConverter->getResourceFromIri($data, $context + ['fetch_data' => true]); return $this->iriConverter->getResourceFromIri($iri, $context + ['fetch_data' => true]);
} catch (ItemNotFoundException $e) { } catch (ItemNotFoundException $e) {
if (false === ($context['denormalize_throw_on_relation_not_found'] ?? true)) { if (false === ($context['denormalize_throw_on_relation_not_found'] ?? true)) {
return null; return null;
@ -76,7 +82,7 @@ class SkippableItemNormalizer implements NormalizerInterface, DenormalizerInterf
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [$type], $context['deserialization_path'] ?? null, true, $e->getCode(), $e); throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [$type], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
if (!isset($context['not_normalizable_value_exceptions'])) { if (!isset($context['not_normalizable_value_exceptions'])) {
throw new UnexpectedValueException(sprintf('Invalid IRI "%s".', $data), $e->getCode(), $e); throw new UnexpectedValueException(sprintf('Invalid IRI "%s".', $iri), $e->getCode(), $e);
} }
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Invalid IRI "%s".', $data), $data, [$type], $context['deserialization_path'] ?? null, true, $e->getCode(), $e); throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Invalid IRI "%s".', $data), $data, [$type], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
} }

View file

@ -70,7 +70,7 @@ final class PartEndpointTest extends CrudEndpointTestCase
$this->_testDeleteItem(1); $this->_testDeleteItem(1);
} }
public function testAttachmentPatchWithIRI(): void public function testMasterPictureAttachmentPatchWithIRI(): void
{ {
$client = static::createAuthenticatedClient(); $client = static::createAuthenticatedClient();
@ -93,4 +93,28 @@ final class PartEndpointTest extends CrudEndpointTestCase
self::assertResponseIsSuccessful(); self::assertResponseIsSuccessful();
self::assertJsonContains(['master_picture_attachment' => ['@id' => $attachmentIri]]); self::assertJsonContains(['master_picture_attachment' => ['@id' => $attachmentIri]]);
} }
public function testMasterPictureAttachmentPatchWithArray(): void
{
$client = static::createAuthenticatedClient();
// Create a new attachment with a picture URL for Part 1
$response = $client->request('POST', '/api/attachments', ['json' => [
'name' => 'Test Picture',
'url' => 'http://example.com/test.jpg',
'_type' => 'Part',
'element' => '/api/parts/1',
'attachment_type' => '/api/attachment_types/1',
]]);
self::assertResponseIsSuccessful();
$attachmentIri = $response->toArray()['@id'];
// Now PATCH Part 1 to set master_picture_attachment
$client->request('PATCH', '/api/parts/1', [
'json' => ['master_picture_attachment' => ['@id' => $attachmentIri, '_type' => 'Part']],
'headers' => ['Content-Type' => 'application/merge-patch+json'],
]);
self::assertResponseIsSuccessful();
self::assertJsonContains(['master_picture_attachment' => ['@id' => $attachmentIri]]);
}
} }