Repository navigation
fix(laravel): accept null on an optional belongsTo #8666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devtechedge
wants to merge
2
commits into
api-platform:4.4
Choose a base branch
from
devtechedge:fix-laravel-nullable-belongsto
base: 4.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,18 @@ public function process(mixed $data, Operation $operation, array $uriVariables = | |
| $toMany = []; | ||
|
|
||
| foreach ($this->modelMetadata->getRelations($data) as $relation) { | ||
| // isset() is false for null, so an explicit null would be skipped and saved as a column named after the relation. | ||
| if ( | ||
| (BelongsTo::class === $relation['type'] || MorphTo::class === $relation['type']) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
| && \array_key_exists($relation['name'], $data->getAttributes()) | ||
| && null === $data->getAttributes()[$relation['name']] | ||
| ) { | ||
| $data->{$relation['method_name']}()->dissociate(); | ||
| unset($data->{$relation['name']}); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (!isset($data->{$relation['name']})) { | ||
| continue; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Laravel\Tests; | ||
|
|
||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Orchestra\Testbench\Concerns\WithWorkbench; | ||
| use Orchestra\Testbench\TestCase; | ||
| use Workbench\App\Models\DeliveryRequest; | ||
| use Workbench\Database\Factories\TimeSlotFactory; | ||
|
|
||
| /** | ||
| * @see https://github.com/api-platform/core/issues/8664 | ||
| */ | ||
| final class NullableBelongsToTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
| use WithWorkbench; | ||
|
|
||
| public function testPostWithExplicitNullBelongsTo(): void | ||
| { | ||
| $response = $this->postJson('/api/delivery_requests', [ | ||
| 'pickupTimeSlot' => null, | ||
| 'note' => 'no slot', | ||
| ], ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']); | ||
|
|
||
| $response->assertStatus(201); | ||
| $this->assertNull(DeliveryRequest::query()->sole()->pickup_time_slot_id); // @phpstan-ignore-line | ||
| } | ||
|
|
||
| public function testPatchWithExplicitNullClearsBelongsTo(): void | ||
| { | ||
| $iri = $this->createDeliveryRequestWithSlot(); | ||
|
|
||
| $response = $this->patchJson($iri, [ | ||
| 'pickupTimeSlot' => null, | ||
| ], ['accept' => 'application/ld+json', 'content-type' => 'application/merge-patch+json']); | ||
|
|
||
| $response->assertStatus(200); | ||
| $this->assertNull(DeliveryRequest::query()->sole()->pickup_time_slot_id); // @phpstan-ignore-line | ||
| } | ||
|
|
||
| public function testOmittingBelongsToKeepsIt(): void | ||
| { | ||
| $iri = $this->createDeliveryRequestWithSlot(); | ||
| $slotId = DeliveryRequest::query()->sole()->pickup_time_slot_id; // @phpstan-ignore-line | ||
|
|
||
| $response = $this->patchJson($iri, [ | ||
| 'note' => 'updated', | ||
| ], ['accept' => 'application/ld+json', 'content-type' => 'application/merge-patch+json']); | ||
|
|
||
| $response->assertStatus(200); | ||
| $deliveryRequest = DeliveryRequest::query()->sole(); | ||
| $this->assertSame($slotId, $deliveryRequest->pickup_time_slot_id); // @phpstan-ignore-line | ||
| $this->assertSame('updated', $deliveryRequest->note); // @phpstan-ignore-line | ||
| } | ||
|
|
||
| private function createDeliveryRequestWithSlot(): string | ||
| { | ||
| $slot = TimeSlotFactory::new()->create(); | ||
|
|
||
| $response = $this->postJson('/api/delivery_requests', [ | ||
| 'pickupTimeSlot' => '/api/time_slots/'.$slot->id, // @phpstan-ignore-line | ||
| 'note' => 'with slot', | ||
| ], ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']); | ||
| $response->assertStatus(201); | ||
| $this->assertNotNull(DeliveryRequest::query()->sole()->pickup_time_slot_id); // @phpstan-ignore-line | ||
|
|
||
| return $response->json('@id'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this changes the output for every existing nullable BelongsTo: JSON Schema/OpenAPI become nullable and GraphQL drops the
!on these fields (inputs included). It's the right fix and it matches what Doctrine does but we need to mention this in the changelog.