From 02bb330be7605d0070708d537daa8b36fad70798 Mon Sep 17 00:00:00 2001 From: "n.gnato" Date: Thu, 26 Jun 2025 16:06:26 +0300 Subject: [PATCH 1/3] Check input attributes value --- src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php b/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php index 5f69241..a28f36c 100644 --- a/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php +++ b/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php @@ -20,7 +20,7 @@ public function __construct(array $data) $concreteClass = new \ReflectionClass($this); - if (property_exists($this, 'attributes')) { + if (property_exists($this, 'attributes') && array_key_exists('attributes', $data)) { $attributesPropertyType = $concreteClass->getProperty('attributes')->getType(); if($attributesPropertyType instanceof \ReflectionUnionType) { From 17c0234d625b86b6e6d031dfabe0b1dcb90ca94e Mon Sep 17 00:00:00 2001 From: "n.gnato" Date: Thu, 26 Jun 2025 16:10:00 +0300 Subject: [PATCH 2/3] Prepare v0.0.7 --- CHANGELOG.md | 8 +++++++- src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c617c2..314f44b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.7] - 2025-06-26 + +### Fixed +- `attributes` and `relationships` fields optional in resource object, according to jsonapi v1.1#7.2. + ## [0.0.6] - 2025-04-05 ### Fixed @@ -37,7 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Extract all DTO types from FreeElephants/json-api-php-toolkit to this project -[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.6...HEAD +[Unreleased]: https://github.com/FreeElephants/json-api-dto/compare/0.0.7...HEAD +[0.0.7]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.7 [0.0.6]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.6 [0.0.5]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.5 [0.0.4]: https://github.com/FreeElephants/json-api-dto/releases/tag/0.0.4 diff --git a/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php b/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php index a28f36c..fd1b61d 100644 --- a/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php +++ b/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php @@ -31,7 +31,7 @@ public function __construct(array $data) $this->attributes = new $attributesClass($data['attributes']); } - if (property_exists($this, 'relationships')) { + if (property_exists($this, 'relationships') && array_key_exists('relationships', $data)) { $relationshipsData = $data['relationships']; $concreteClass = new \ReflectionClass($this); $relationshipsProperty = $concreteClass->getProperty('relationships'); From 247826212ba2b0a6580d84239a2c65bf1cb3d0b5 Mon Sep 17 00:00:00 2001 From: "n.gnato" Date: Sat, 23 Aug 2025 16:38:40 +0300 Subject: [PATCH 3/3] Optional id --- CHANGELOG.md | 2 +- .../JsonApi/DTO/AbstractResourceObject.php | 2 +- .../JsonApi/DTO/ResourceObjectTest.php | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 314f44b..d2dc258 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.0.7] - 2025-06-26 ### Fixed -- `attributes` and `relationships` fields optional in resource object, according to jsonapi v1.1#7.2. +- `id`, `attributes` and `relationships` fields optional in resource object, according to jsonapi v1.1#7.2. ## [0.0.6] - 2025-04-05 diff --git a/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php b/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php index fd1b61d..8fd2512 100644 --- a/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php +++ b/src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php @@ -15,7 +15,7 @@ class AbstractResourceObject public function __construct(array $data) { - $this->id = $data['id']; + $this->id = $data['id'] ?? null; $this->type = $data['type']; $concreteClass = new \ReflectionClass($this); diff --git a/tests/FreeElephants/JsonApi/DTO/ResourceObjectTest.php b/tests/FreeElephants/JsonApi/DTO/ResourceObjectTest.php index c2c5799..7c7d566 100644 --- a/tests/FreeElephants/JsonApi/DTO/ResourceObjectTest.php +++ b/tests/FreeElephants/JsonApi/DTO/ResourceObjectTest.php @@ -30,6 +30,29 @@ public function testRelationshipTypes() $this->assertInstanceOf(Example\OneRelationships::class, $resourceObject->relationships); $this->assertSame('one', $resourceObject->relationships->one->data->type); } + + public function testWithNullableId(): void + { + $resourceObject = new class([ + 'type' => 'type', + 'attributes' => [ + 'foo' => 'bar', + ], + 'relationships' => [ + 'one' => [ + 'data' => [ + 'type' => 'one', + 'id' => 'one', + ], + ], + ], + ]) extends AbstractResourceObject { + public Example\Attributes $attributes; + public Example\OneRelationships $relationships; + }; + + $this->assertNull($resourceObject->id); + } } class Attributes extends AbstractAttributes