Skip to content

Commit 4ac62b0

Browse files
authored
fix(jsonschema): build non-resource class schema (#5842)
1 parent b66fdcf commit 4ac62b0

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

src/JsonSchema/SchemaFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
195195
}
196196

197197
$className = $valueType?->getClassName();
198-
if (null === $className || !$this->isResourceClass($className)) {
198+
if (null === $className) {
199199
continue;
200200
}
201201

tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ class BagOfTests
4040
#[Groups(['read', 'write'])]
4141
private Collection $tests;
4242

43+
#[ORM\OneToMany(mappedBy: 'bagOfTests', targetEntity: NonResourceTestEntity::class)]
44+
#[Groups(['read', 'write'])]
45+
private Collection $nonResourceTests;
46+
4347
public function __construct()
4448
{
4549
$this->tests = new ArrayCollection();
50+
$this->nonResourceTests = new ArrayCollection();
4651
}
4752

4853
public function getId(): ?int
@@ -91,4 +96,31 @@ public function removeTest(TestEntity $test): static
9196

9297
return $this;
9398
}
99+
100+
public function getNonResourceTests(): Collection
101+
{
102+
return $this->nonResourceTests;
103+
}
104+
105+
public function addNonResourceTest(NonResourceTestEntity $nonResourceTest): static
106+
{
107+
if (!$this->nonResourceTests->contains($nonResourceTest)) {
108+
$this->nonResourceTests->add($nonResourceTest);
109+
$nonResourceTest->setBagOfTests($this);
110+
}
111+
112+
return $this;
113+
}
114+
115+
public function removeNonResourceTest(NonResourceTestEntity $nonResourceTest): static
116+
{
117+
if ($this->nonResourceTests->removeElement($nonResourceTest)) {
118+
// set the owning side to null (unless already changed)
119+
if ($nonResourceTest->getBagOfTests() === $this) {
120+
$nonResourceTest->setBagOfTests(null);
121+
}
122+
}
123+
124+
return $this;
125+
}
94126
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5793;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
use Symfony\Component\Serializer\Annotation\Groups;
18+
19+
#[ORM\Entity]
20+
class NonResourceTestEntity
21+
{
22+
#[ORM\Id]
23+
#[ORM\GeneratedValue]
24+
#[ORM\Column]
25+
#[Groups(['read', 'write'])]
26+
private ?int $id = null;
27+
#[ORM\Column(length: 255, nullable: true)]
28+
#[Groups(['read', 'write'])]
29+
private ?string $nullableString = null;
30+
31+
#[ORM\Column(nullable: true)]
32+
#[Groups(['read', 'write'])]
33+
private ?int $nullableInt = null;
34+
35+
#[ORM\ManyToOne(inversedBy: 'tests')]
36+
private ?BagOfTests $bagOfTests = null;
37+
38+
public function getId(): ?int
39+
{
40+
return $this->id;
41+
}
42+
43+
public function getNullableString(): ?string
44+
{
45+
return $this->nullableString;
46+
}
47+
48+
public function setNullableString(?string $nullableString): static
49+
{
50+
$this->nullableString = $nullableString;
51+
52+
return $this;
53+
}
54+
55+
public function getNullableInt(): ?int
56+
{
57+
return $this->nullableInt;
58+
}
59+
60+
public function setNullableInt(?int $nullableInt): static
61+
{
62+
$this->nullableInt = $nullableInt;
63+
64+
return $this;
65+
}
66+
67+
public function getBagOfTests(): ?BagOfTests
68+
{
69+
return $this->bagOfTests;
70+
}
71+
72+
public function setBagOfTests(?BagOfTests $bagOfTests): static
73+
{
74+
$this->bagOfTests = $bagOfTests;
75+
76+
return $this;
77+
}
78+
}

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,12 @@ public function testArraySchemaWithReference(): void
109109
'$ref' => '#/definitions/TestEntity.jsonld-write',
110110
],
111111
]);
112+
113+
$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['nonResourceTests'], [
114+
'type' => 'array',
115+
'items' => [
116+
'$ref' => '#/definitions/NonResourceTestEntity.jsonld-write',
117+
],
118+
]);
112119
}
113120
}

0 commit comments

Comments
 (0)