forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(jsonschema): indirect resource input schema (api-platform#6001)
Fixes api-platform#5998 A resource embedded in another class can be writable without having a write operation (POST, PUT, PATCH).
- Loading branch information
Showing
5 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
76 changes: 76 additions & 0 deletions
76
tests/Fixtures/TestBundle/Entity/Issue5998/Issue5998Product.php
This file contains 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,76 @@ | ||
<?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\Issue5998; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Post; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
#[ApiResource] | ||
#[Post( | ||
denormalizationContext: ['groups' => ['product:write']], | ||
input: SaveProduct::class, | ||
)] | ||
class Issue5998Product | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column] | ||
private ?int $id = null; | ||
|
||
/** | ||
* @var Collection<int, ProductCode> | ||
*/ | ||
#[ORM\OneToMany(mappedBy: 'product', targetEntity: ProductCode::class, cascade: ['persist'], orphanRemoval: true)] | ||
private Collection $codes; | ||
|
||
public function __construct() | ||
{ | ||
$this->codes = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @return Collection<int, ProductCode> | ||
*/ | ||
public function getCodes(): Collection | ||
{ | ||
return $this->codes; | ||
} | ||
|
||
public function addCode(ProductCode $code): void | ||
{ | ||
if (!$this->codes->contains($code)) { | ||
$this->codes->add($code); | ||
$code->setProduct($this); | ||
} | ||
} | ||
|
||
public function removeCode(ProductCode $code): void | ||
{ | ||
if ($this->codes->removeElement($code)) { | ||
// set the owning side to null (unless already changed) | ||
if ($code->getProduct() === $this) { | ||
$code->setProduct(null); | ||
} | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
tests/Fixtures/TestBundle/Entity/Issue5998/ProductCode.php
This file contains 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,77 @@ | ||
<?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\Issue5998; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
|
||
#[ApiResource] | ||
#[Get] | ||
#[ORM\Entity] | ||
class ProductCode | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
#[ORM\Column] | ||
private ?int $id = null; | ||
|
||
#[ORM\Column(length: 180)] | ||
#[Groups(['product:write'])] | ||
private ?string $type = null; | ||
|
||
#[ORM\Column(length: 180)] | ||
#[Groups(['product:write'])] | ||
private ?string $value = null; | ||
|
||
#[ORM\ManyToOne(inversedBy: 'codes')] | ||
#[ORM\JoinColumn(nullable: false)] | ||
private ?Issue5998Product $product = null; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getType(): ?string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function setType(string $type): void | ||
{ | ||
$this->type = $type; | ||
} | ||
|
||
public function getValue(): ?string | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function setValue(?string $value): void | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function getProduct(): ?Issue5998Product | ||
{ | ||
return $this->product; | ||
} | ||
|
||
public function setProduct(?Issue5998Product $product): void | ||
{ | ||
$this->product = $product; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/Fixtures/TestBundle/Entity/Issue5998/SaveProduct.php
This file contains 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,54 @@ | ||
<?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\Issue5998; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Symfony\Component\Serializer\Annotation\Groups; | ||
|
||
class SaveProduct | ||
{ | ||
/** | ||
* @var Collection<int, ProductCode> | ||
*/ | ||
#[Groups(['product:write'])] | ||
private Collection $codes; | ||
|
||
public function __construct() | ||
{ | ||
$this->codes = new ArrayCollection(); | ||
} | ||
|
||
/** | ||
* @return Collection<int, ProductCode> | ||
*/ | ||
public function getCodes(): Collection | ||
{ | ||
return $this->codes; | ||
} | ||
|
||
public function addCode(ProductCode $code): void | ||
{ | ||
if (!$this->codes->contains($code)) { | ||
$this->codes->add($code); | ||
} | ||
} | ||
|
||
public function removeCode(ProductCode $code): void | ||
{ | ||
if ($this->codes->contains($code)) { | ||
$this->codes->removeElement($code); | ||
} | ||
} | ||
} |
This file contains 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