diff --git a/src/JsonSchema/SchemaFactory.php b/src/JsonSchema/SchemaFactory.php index 946bcf7a729..831f4330430 100644 --- a/src/JsonSchema/SchemaFactory.php +++ b/src/JsonSchema/SchemaFactory.php @@ -195,7 +195,7 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str } $className = $valueType?->getClassName(); - if (null === $className || !$this->isResourceClass($className)) { + if (null === $className) { continue; } diff --git a/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php b/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php index b12f1c7f7b3..490f74e6b4c 100644 --- a/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php +++ b/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php @@ -40,9 +40,14 @@ class BagOfTests #[Groups(['read', 'write'])] private Collection $tests; + #[ORM\OneToMany(mappedBy: 'bagOfTests', targetEntity: NonResourceTestEntity::class)] + #[Groups(['read', 'write'])] + private Collection $nonResourceTests; + public function __construct() { $this->tests = new ArrayCollection(); + $this->nonResourceTests = new ArrayCollection(); } public function getId(): ?int @@ -91,4 +96,31 @@ public function removeTest(TestEntity $test): static return $this; } + + public function getNonResourceTests(): Collection + { + return $this->nonResourceTests; + } + + public function addNonResourceTest(NonResourceTestEntity $nonResourceTest): static + { + if (!$this->nonResourceTests->contains($nonResourceTest)) { + $this->nonResourceTests->add($nonResourceTest); + $nonResourceTest->setBagOfTests($this); + } + + return $this; + } + + public function removeNonResourceTest(NonResourceTestEntity $nonResourceTest): static + { + if ($this->nonResourceTests->removeElement($nonResourceTest)) { + // set the owning side to null (unless already changed) + if ($nonResourceTest->getBagOfTests() === $this) { + $nonResourceTest->setBagOfTests(null); + } + } + + return $this; + } } diff --git a/tests/Fixtures/TestBundle/Entity/Issue5793/NonResourceTestEntity.php b/tests/Fixtures/TestBundle/Entity/Issue5793/NonResourceTestEntity.php new file mode 100644 index 00000000000..f6d4fe1d1a6 --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/Issue5793/NonResourceTestEntity.php @@ -0,0 +1,78 @@ + + * + * 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\Issue5793; + +use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Annotation\Groups; + +#[ORM\Entity] +class NonResourceTestEntity +{ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column] + #[Groups(['read', 'write'])] + private ?int $id = null; + #[ORM\Column(length: 255, nullable: true)] + #[Groups(['read', 'write'])] + private ?string $nullableString = null; + + #[ORM\Column(nullable: true)] + #[Groups(['read', 'write'])] + private ?int $nullableInt = null; + + #[ORM\ManyToOne(inversedBy: 'tests')] + private ?BagOfTests $bagOfTests = null; + + public function getId(): ?int + { + return $this->id; + } + + public function getNullableString(): ?string + { + return $this->nullableString; + } + + public function setNullableString(?string $nullableString): static + { + $this->nullableString = $nullableString; + + return $this; + } + + public function getNullableInt(): ?int + { + return $this->nullableInt; + } + + public function setNullableInt(?int $nullableInt): static + { + $this->nullableInt = $nullableInt; + + return $this; + } + + public function getBagOfTests(): ?BagOfTests + { + return $this->bagOfTests; + } + + public function setBagOfTests(?BagOfTests $bagOfTests): static + { + $this->bagOfTests = $bagOfTests; + + return $this; + } +} diff --git a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php index 346da8b5eb5..c5bcb8303ff 100644 --- a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php +++ b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php @@ -109,5 +109,12 @@ public function testArraySchemaWithReference(): void '$ref' => '#/definitions/TestEntity.jsonld-write', ], ]); + + $this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['nonResourceTests'], [ + 'type' => 'array', + 'items' => [ + '$ref' => '#/definitions/NonResourceTestEntity.jsonld-write', + ], + ]); } }