Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- `id`, `attributes` and `relationships` fields optional in resource object, according to jsonapi v1.1#7.2.

## [0.0.6] - 2025-04-05

### Fixed
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class AbstractResourceObject

public function __construct(array $data)
{
$this->id = $data['id'];
$this->id = $data['id'] ?? null;
$this->type = $data['type'];

$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) {
Expand All @@ -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');
Expand Down
23 changes: 23 additions & 0 deletions tests/FreeElephants/JsonApi/DTO/ResourceObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down