-
-
Notifications
You must be signed in to change notification settings - Fork 922
fix(serializer): Allow nested denormalization when allow_extra_attributes=false #7270
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
calbro7
wants to merge
6
commits into
api-platform:4.1
Choose a base branch
from
calbro7:fix-issue-6225
base: 4.1
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
6 commits
Select commit
Hold shift + click to select a range
606edf2
fix(serializer): Allow "ID" field even when allow_extra_attributes=false
calbro7 8572266
Make property nullable
calbro7 f80f644
Use protected static ?bool $alwaysBootKernel = false;
calbro7 83eaa22
Unset ID before calling parent denormalizer
calbro7 ffafc6d
Skip test in mongodb environment
calbro7 81a393f
php-cs-fixer fix
calbro7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Tests\Fixtures\TestBundle\Entity\Issue6225; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
#[ORM\Entity] | ||
#[ORM\Table(name: 'bar6225')] | ||
#[ApiResource] | ||
class Bar6225 | ||
{ | ||
public function __construct() | ||
{ | ||
$this->id = Uuid::v7(); | ||
} | ||
|
||
#[ORM\Id] | ||
#[ORM\Column(type: 'symfony_uuid', unique: true)] | ||
#[Groups(['Foo:Read'])] | ||
private Uuid $id; | ||
|
||
#[ORM\OneToOne(mappedBy: 'bar', cascade: ['persist', 'remove'])] | ||
private ?Foo6225 $foo; | ||
|
||
#[ORM\Column(length: 255)] | ||
#[Groups(['Foo:Write', 'Foo:Read'])] | ||
private string $someProperty; | ||
|
||
public function getId(): Uuid | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getFoo(): Foo6225 | ||
{ | ||
return $this->foo; | ||
} | ||
|
||
public function setFoo(Foo6225 $foo): static | ||
{ | ||
$this->foo = $foo; | ||
|
||
return $this; | ||
} | ||
|
||
public function getSomeProperty(): string | ||
{ | ||
return $this->someProperty; | ||
} | ||
|
||
public function setSomeProperty(string $someProperty): static | ||
{ | ||
$this->someProperty = $someProperty; | ||
|
||
return $this; | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Tests\Fixtures\TestBundle\Entity\Issue6225; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Patch; | ||
use ApiPlatform\Metadata\Post; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
#[ORM\Entity()] | ||
#[ORM\Table(name: 'foo6225')] | ||
#[ApiResource( | ||
operations: [ | ||
new Post(), | ||
new Patch(), | ||
], | ||
normalizationContext: [ | ||
'groups' => ['Foo:Read'], | ||
], | ||
denormalizationContext: [ | ||
'allow_extra_attributes' => false, | ||
'groups' => ['Foo:Write'], | ||
], | ||
)] | ||
class Foo6225 | ||
{ | ||
public function __construct() | ||
{ | ||
$this->id = Uuid::v7(); | ||
} | ||
|
||
#[ORM\Id] | ||
#[ORM\Column(type: 'symfony_uuid', unique: true)] | ||
#[Groups(['Foo:Read'])] | ||
private Uuid $id; | ||
|
||
#[ORM\OneToOne(inversedBy: 'foo', cascade: ['persist', 'remove'])] | ||
#[ORM\JoinColumn(nullable: false)] | ||
#[Groups(['Foo:Write', 'Foo:Read'])] | ||
private Bar6225 $bar; | ||
|
||
public function getId(): Uuid | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getBar(): Bar6225 | ||
{ | ||
return $this->bar; | ||
} | ||
|
||
public function setBar(Bar6225 $bar): static | ||
{ | ||
$this->bar = $bar; | ||
|
||
return $this; | ||
} | ||
} |
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,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Tests\Functional; | ||
|
||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6225\Bar6225; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6225\Foo6225; | ||
use ApiPlatform\Tests\RecreateSchemaTrait; | ||
use ApiPlatform\Tests\SetupClassResourcesTrait; | ||
|
||
class NestedPatchTest extends ApiTestCase | ||
{ | ||
use RecreateSchemaTrait; | ||
use SetupClassResourcesTrait; | ||
|
||
protected static ?bool $alwaysBootKernel = false; | ||
|
||
public static function getResources(): array | ||
{ | ||
return [Foo6225::class, Bar6225::class]; | ||
} | ||
|
||
public function testIssue6225(): void | ||
{ | ||
if ($this->isMongoDB()) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$this->recreateSchema(self::getResources()); | ||
|
||
$response = self::createClient()->request('POST', '/foo6225s', [ | ||
'json' => [ | ||
'bar' => [ | ||
'someProperty' => 'abc', | ||
], | ||
], | ||
'headers' => [ | ||
'accept' => 'application/json', | ||
], | ||
]); | ||
static::assertResponseIsSuccessful(); | ||
$responseContent = json_decode($response->getContent(), true); | ||
$createdFooId = $responseContent['id']; | ||
$createdBarId = $responseContent['bar']['id']; | ||
|
||
$patchResponse = self::createClient()->request('PATCH', '/foo6225s/'.$createdFooId, [ | ||
'json' => [ | ||
'bar' => [ | ||
'id' => $createdBarId, | ||
'someProperty' => 'def', | ||
], | ||
], | ||
'headers' => [ | ||
'accept' => 'application/json', | ||
'content-type' => 'application/merge-patch+json', | ||
], | ||
]); | ||
static::assertResponseIsSuccessful(); | ||
static::assertEquals([ | ||
'id' => $createdFooId, | ||
'bar' => [ | ||
'id' => $createdBarId, | ||
'someProperty' => 'def', | ||
], | ||
], json_decode($patchResponse->getContent(), true)); | ||
} | ||
} |
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.
what if
id
isn't the property/attribute used to identify a resource?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.
Hello, possibly I'm misunderstanding what you mean, I think it always will be
id
, as it's not configurable is it? And it's also hardcoded eg here:When/how could it be something else?