Skip to content

Commit

Permalink
fix(jsonschema): build non-resource class schema (#5842)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored Sep 20, 2023
1 parent b66fdcf commit 4ac62b0
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
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 <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\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;
}
}
7 changes: 7 additions & 0 deletions tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]);
}
}

0 comments on commit 4ac62b0

Please sign in to comment.