Skip to content

Commit 71d0b0d

Browse files
committed
Fix TypeError when removing non-nullable to-one relationship
Fixes #74
1 parent 208a1d5 commit 71d0b0d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Schema/Field/ToOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function deserializeValue(mixed $value, Context $context): mixed
5252
throw new BadRequestException('relationship does not include data key');
5353
}
5454

55-
if ($this->nullable && $value['data'] === null) {
55+
if ($value['data'] === null) {
5656
return null;
5757
}
5858

tests/feature/RelationshipToOneTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Tobyz\JsonApiServer\Endpoint\Create;
77
use Tobyz\JsonApiServer\Endpoint\Show;
88
use Tobyz\JsonApiServer\Exception\BadRequestException;
9+
use Tobyz\JsonApiServer\Exception\UnprocessableEntityException;
910
use Tobyz\JsonApiServer\JsonApi;
1011
use Tobyz\JsonApiServer\Schema\Field\ToOne;
1112
use Tobyz\Tests\JsonApiServer\AbstractTestCase;
@@ -238,4 +239,32 @@ public function test_to_one_create_polymorphic()
238239

239240
$this->assertEquals(201, $response->getStatusCode());
240241
}
242+
243+
public function test_to_one_create_null_not_nullable()
244+
{
245+
$this->api->resource(
246+
new MockResource(
247+
'users',
248+
models: [(object) ['id' => '1']],
249+
endpoints: [Create::make()],
250+
fields: [
251+
ToOne::make('friend')
252+
->type('users')
253+
->writable()
254+
->includable(),
255+
],
256+
),
257+
);
258+
259+
$this->expectException(UnprocessableEntityException::class);
260+
261+
$this->api->handle(
262+
$this->buildRequest('POST', '/users')->withParsedBody([
263+
'data' => [
264+
'type' => 'users',
265+
'relationships' => ['friend' => ['data' => null]],
266+
],
267+
]),
268+
);
269+
}
241270
}

0 commit comments

Comments
 (0)