Skip to content

Commit 2478262

Browse files
author
n.gnato
committed
Optional id
1 parent 17c0234 commit 2478262

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
## [0.0.7] - 2025-06-26
1010

1111
### Fixed
12-
- `attributes` and `relationships` fields optional in resource object, according to jsonapi v1.1#7.2.
12+
- `id`, `attributes` and `relationships` fields optional in resource object, according to jsonapi v1.1#7.2.
1313

1414
## [0.0.6] - 2025-04-05
1515

src/FreeElephants/JsonApi/DTO/AbstractResourceObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AbstractResourceObject
1515

1616
public function __construct(array $data)
1717
{
18-
$this->id = $data['id'];
18+
$this->id = $data['id'] ?? null;
1919
$this->type = $data['type'];
2020

2121
$concreteClass = new \ReflectionClass($this);

tests/FreeElephants/JsonApi/DTO/ResourceObjectTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ public function testRelationshipTypes()
3030
$this->assertInstanceOf(Example\OneRelationships::class, $resourceObject->relationships);
3131
$this->assertSame('one', $resourceObject->relationships->one->data->type);
3232
}
33+
34+
public function testWithNullableId(): void
35+
{
36+
$resourceObject = new class([
37+
'type' => 'type',
38+
'attributes' => [
39+
'foo' => 'bar',
40+
],
41+
'relationships' => [
42+
'one' => [
43+
'data' => [
44+
'type' => 'one',
45+
'id' => 'one',
46+
],
47+
],
48+
],
49+
]) extends AbstractResourceObject {
50+
public Example\Attributes $attributes;
51+
public Example\OneRelationships $relationships;
52+
};
53+
54+
$this->assertNull($resourceObject->id);
55+
}
3356
}
3457

3558
class Attributes extends AbstractAttributes

0 commit comments

Comments
 (0)