Skip to content

Commit 5115f5e

Browse files
authored
fix: make sure relations are generated (#363)
1 parent 28c699c commit 5115f5e

File tree

7 files changed

+82
-11
lines changed

7 files changed

+82
-11
lines changed

src/AttributeGenerator/DoctrineMongoDBAttributeGenerator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function generateClassAttributes(Class_ $class): array
6868
*/
6969
public function generatePropertyAttributes(Property $property, string $className): array
7070
{
71-
if (null === $property->range) {
71+
if (null === $property->range || null === $property->rangeName) {
7272
return [];
7373
}
7474

@@ -77,11 +77,12 @@ public function generatePropertyAttributes(Property $property, string $className
7777
}
7878

7979
$type = null;
80+
$isDataType = $this->phpTypeConverter->isDatatype($property->range);
8081
if ($property->isEnum) {
8182
$type = $property->isArray ? 'simple_array' : 'string';
82-
} elseif ($property->isArray ?? false) {
83+
} elseif ($property->isArray && $isDataType) {
8384
$type = 'collection';
84-
} elseif (null !== $phpType = $this->phpTypeConverter->getPhpType($property, $this->config, [])) {
85+
} elseif (!$property->isArray && $isDataType && null !== ($phpType = $this->phpTypeConverter->getPhpType($property, $this->config, []))) {
8586
switch ($property->range->getUri()) {
8687
case 'http://www.w3.org/2001/XMLSchema#time':
8788
case 'https://schema.org/Time':

src/AttributeGenerator/DoctrineOrmAttributeGenerator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function generateClassAttributes(Class_ $class): array
9393
*/
9494
public function generatePropertyAttributes(Property $property, string $className): array
9595
{
96-
if (null === $property->rangeName) {
96+
if (null === $property->range || null === $property->rangeName) {
9797
return [];
9898
}
9999

@@ -110,11 +110,12 @@ public function generatePropertyAttributes(Property $property, string $className
110110
}
111111

112112
$type = null;
113+
$isDataType = $this->phpTypeConverter->isDatatype($property->range);
113114
if ($property->isEnum) {
114115
$type = $property->isArray ? 'simple_array' : 'string';
115-
} elseif ($property->isArray) {
116+
} elseif ($property->isArray && $isDataType) {
116117
$type = 'json';
117-
} elseif ($property->range && null !== ($phpType = $this->phpTypeConverter->getPhpType($property, $this->config, []))) {
118+
} elseif (!$property->isArray && $isDataType && null !== ($phpType = $this->phpTypeConverter->getPhpType($property, $this->config, []))) {
118119
switch ($property->range->getUri()) {
119120
// TODO: use more precise types for int (smallint, bigint...)
120121
case 'http://www.w3.org/2001/XMLSchema#time':
@@ -173,7 +174,7 @@ public function generatePropertyAttributes(Property $property, string $className
173174
}
174175

175176
if (null === $relationName = $this->getRelationName($property->rangeName)) {
176-
$this->logger->error('The type "{type}" of the property "{property}" from the class "{class}" doesn\'t exist', ['type' => $property->range ? $property->range->getUri() : $property->rangeName, 'property' => $property->name(), 'class' => $className]);
177+
$this->logger->error('The type "{type}" of the property "{property}" from the class "{class}" doesn\'t exist', ['type' => $property->range->getUri(), 'property' => $property->name(), 'class' => $className]);
177178

178179
return [];
179180
}

tests/AttributeGenerator/DoctrineMongoDBAttributeGeneratorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ protected function setUp(): void
5555
$enumProperty->isArray = true;
5656
$vehicle->addProperty($enumProperty);
5757
$collectionProperty = new Property('collection');
58-
$collectionProperty->rangeName = 'Thing';
59-
$collectionProperty->range = new RdfResource('https://schema.org/Thing');
58+
$collectionProperty->rangeName = 'string';
59+
$collectionProperty->range = new RdfResource('http://www.w3.org/2001/XMLSchema#string');
6060
$collectionProperty->isArray = true;
6161
$vehicle->addProperty($collectionProperty);
6262
$weightProperty = new Property('weight');
@@ -72,6 +72,7 @@ protected function setUp(): void
7272
$relationsProperty->rangeName = 'Person';
7373
$relationsProperty->range = new RdfResource('https://schema.org/Person');
7474
$relationsProperty->cardinality = CardinalitiesExtractor::CARDINALITY_1_N;
75+
$relationsProperty->isArray = true;
7576
$vehicle->addProperty($relationsProperty);
7677

7778
$this->classMap[$vehicle->name()] = $vehicle;

tests/AttributeGenerator/DoctrineOrmAttributeGeneratorTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ protected function setUp(): void
5858
$enumProperty->isArray = true;
5959
$vehicle->addProperty($enumProperty);
6060
$collectionProperty = new Property('collection');
61-
$collectionProperty->rangeName = 'Thing';
62-
$collectionProperty->range = new RdfResource('https://schema.org/Thing');
61+
$collectionProperty->rangeName = 'string';
62+
$collectionProperty->range = new RdfResource('http://www.w3.org/2001/XMLSchema#string');
6363
$collectionProperty->isArray = true;
6464
$vehicle->addProperty($collectionProperty);
6565
$weightProperty = new Property('weight');
@@ -96,16 +96,19 @@ protected function setUp(): void
9696
$relation0NProperty->rangeName = 'QuantitativeValue';
9797
$relation0NProperty->range = new RdfResource('https://schema.org/QuantitativeValue');
9898
$relation0NProperty->cardinality = CardinalitiesExtractor::CARDINALITY_0_N;
99+
$relation0NProperty->isArray = true;
99100
$vehicle->addProperty($relation0NProperty);
100101
$relation1NProperty = new Property('relation1_N');
101102
$relation1NProperty->rangeName = 'QuantitativeValue';
102103
$relation1NProperty->range = new RdfResource('https://schema.org/QuantitativeValue');
103104
$relation1NProperty->cardinality = CardinalitiesExtractor::CARDINALITY_1_N;
105+
$relation1NProperty->isArray = true;
104106
$vehicle->addProperty($relation1NProperty);
105107
$relationNNProperty = new Property('relationN_N');
106108
$relationNNProperty->rangeName = 'QuantitativeValue';
107109
$relationNNProperty->range = new RdfResource('https://schema.org/QuantitativeValue');
108110
$relationNNProperty->cardinality = CardinalitiesExtractor::CARDINALITY_N_N;
111+
$relationNNProperty->isArray = true;
109112
$vehicle->addProperty($relationNNProperty);
110113

111114
$this->classMap[$vehicle->name()] = $vehicle;

tests/e2e/customized/App/Entity/Person.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use App\Enum\GenderType;
1111
use App\Model\MyCustomClass;
1212
use App\Model\MyCustomInterface;
13+
use Doctrine\Common\Collections\ArrayCollection;
14+
use Doctrine\Common\Collections\Collection;
1315
use Doctrine\ORM\Mapping as ORM;
1416
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
1517
use Symfony\Component\Serializer\Annotation\Groups;
@@ -127,10 +129,25 @@ class Person extends MyCustomClass implements MyCustomInterface
127129
#[Assert\Url]
128130
private ?string $url = null;
129131

132+
/**
133+
* A sibling of the person.
134+
*
135+
* @see https://schema.org/siblings
136+
*/
137+
#[ORM\ManyToMany(targetEntity: 'App\Entity\Person')]
138+
#[ORM\InverseJoinColumn(unique: true)]
139+
#[ApiProperty(iri: 'https://schema.org/siblings')]
140+
private ?Collection $siblings = null;
141+
130142
/** @see _:customColumn */
131143
#[ORM\Column(type: 'decimal', precision: 5, scale: 1, options: ['comment' => 'my comment'])]
132144
private ?Person $customColumn = null;
133145

146+
public function __construct()
147+
{
148+
$this->siblings = new ArrayCollection();
149+
}
150+
134151
public function getMyProperty(): string
135152
{
136153
return $this->myProperty;
@@ -231,6 +248,21 @@ public function getUrl(): ?string
231248
return $this->url;
232249
}
233250

251+
public function addSibling(Person $sibling): void
252+
{
253+
$this->siblings[] = $sibling;
254+
}
255+
256+
public function removeSibling(Person $sibling): void
257+
{
258+
$this->siblings->removeElement($sibling);
259+
}
260+
261+
public function getSiblings(): Collection
262+
{
263+
return $this->siblings;
264+
}
265+
234266
public function setCustomColumn(?Person $customColumn): void
235267
{
236268
$this->customColumn = $customColumn;

tests/e2e/original/App/Entity/Person.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use ApiPlatform\Core\Annotation\ApiProperty;
88
use ApiPlatform\Core\Annotation\ApiResource;
99
use App\Enum\GenderType;
10+
use Doctrine\Common\Collections\ArrayCollection;
11+
use Doctrine\Common\Collections\Collection;
1012
use Doctrine\ORM\Mapping as ORM;
1113
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
1214
use Symfony\Component\Serializer\Annotation\Groups;
@@ -118,12 +120,27 @@ class Person extends Thing
118120
#[Assert\Url]
119121
private ?string $url = null;
120122

123+
/**
124+
* A sibling of the person.
125+
*
126+
* @see https://schema.org/siblings
127+
*/
128+
#[ORM\ManyToMany(targetEntity: 'App\Entity\Person')]
129+
#[ORM\InverseJoinColumn(unique: true)]
130+
#[ApiProperty(iri: 'https://schema.org/siblings')]
131+
private ?Collection $siblings = null;
132+
121133
/**
122134
* @see _:customColumn
123135
*/
124136
#[ORM\Column(type: 'decimal', precision: 5, scale: 1, options: ['comment' => 'my comment'])]
125137
private ?Person $customColumn = null;
126138

139+
public function __construct()
140+
{
141+
$this->siblings = new ArrayCollection();
142+
}
143+
127144
public function getId(): ?int
128145
{
129146
return $this->id;
@@ -219,6 +236,21 @@ public function getUrl(): ?string
219236
return $this->url;
220237
}
221238

239+
public function addSibling(Person $sibling): void
240+
{
241+
$this->siblings[] = $sibling;
242+
}
243+
244+
public function removeSibling(Person $sibling): void
245+
{
246+
$this->siblings->removeElement($sibling);
247+
}
248+
249+
public function getSiblings(): Collection
250+
{
251+
return $this->siblings;
252+
}
253+
222254
public function setCustomColumn(?Person $customColumn): void
223255
{
224256
$this->customColumn = $customColumn;

tests/e2e/schema.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ types:
2929
telephone: ~
3030
email: { unique: true, security: "is_granted('ROLE_ADMIN')" }
3131
url: ~
32+
siblings: { cardinality: "(0..*)" }
3233
customColumn: { ormColumn: {type: "decimal", precision: 5, scale: 1, options: {comment: "my comment"}} }
3334
PostalAddress:
3435
properties:

0 commit comments

Comments
 (0)