Skip to content

Commit fce42e0

Browse files
fix(jsonapi): re-add continue once relation is determined (#6325)
1 parent 97c8ae2 commit fce42e0

File tree

5 files changed

+181
-4
lines changed

5 files changed

+181
-4
lines changed

src/JsonApi/JsonSchema/SchemaFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ private function buildDefinitionPropertiesSchema(string $key, string $className,
207207
'type' => 'array',
208208
'items' => self::RELATION_PROPS,
209209
];
210+
continue;
210211
}
211212
if ('id' === $propertyName) {
212213
$attributes['_id'] = $property;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
19+
#[ApiResource]
20+
class Animal
21+
{
22+
private ?int $id = null;
23+
24+
#[ApiProperty(required: true)]
25+
private ?Species $species = null;
26+
27+
public function getId(): ?int
28+
{
29+
return $this->id;
30+
}
31+
32+
public function getSpecies(): ?Species
33+
{
34+
return $this->species;
35+
}
36+
37+
public function setSpecies(?Species $species): static
38+
{
39+
$this->species = $species;
40+
41+
return $this;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use Doctrine\Common\Collections\ArrayCollection;
19+
use Doctrine\Common\Collections\Collection;
20+
21+
#[ApiResource]
22+
class AnimalObservation
23+
{
24+
private ?int $id = null;
25+
26+
#[ApiProperty(required: true)]
27+
private Collection $individuals;
28+
29+
public function __construct()
30+
{
31+
$this->individuals = new ArrayCollection();
32+
}
33+
34+
public function getId(): ?int
35+
{
36+
return $this->id;
37+
}
38+
39+
/** @return Collection<int, Animal> */
40+
public function getIndividuals(): Collection
41+
{
42+
return $this->individuals;
43+
}
44+
45+
public function addIndividual(Animal $individual): static
46+
{
47+
if (!$this->individuals->contains($individual)) {
48+
$this->individuals->add($individual);
49+
}
50+
51+
return $this;
52+
}
53+
54+
public function removeIndividual(Animal $individual): static
55+
{
56+
$this->individuals->removeElement($individual);
57+
58+
return $this;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\Get;
19+
use ApiPlatform\Metadata\GetCollection;
20+
21+
#[ApiResource]
22+
#[GetCollection(
23+
uriTemplate: '/species',
24+
)]
25+
#[Get(
26+
uriTemplate: '/species/{key}',
27+
)]
28+
final class Species
29+
{
30+
#[ApiProperty(identifier: true)]
31+
public ?int $key = null;
32+
public ?string $kingdom = null;
33+
public ?string $phylum = null;
34+
public ?string $order = null;
35+
public ?string $family = null;
36+
public ?string $genus = null;
37+
public ?string $species = null;
38+
}

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

+39-4
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,45 @@ public function testJsonApiIncludesSchema(): void
228228
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Question', '--type' => 'output', '--format' => 'jsonapi']);
229229
$result = $this->tester->getDisplay();
230230
$json = json_decode($result, associative: true);
231+
$properties = $json['definitions']['Question.jsonapi']['properties']['data']['properties'];
232+
$included = $json['definitions']['Question.jsonapi']['properties']['included'];
231233

232-
$this->assertArrayHasKey('answer', $json['definitions']['Question.jsonapi']['properties']['data']['properties']['relationships']['properties']);
233-
$this->assertArrayHasKey('anyOf', $json['definitions']['Question.jsonapi']['properties']['included']['items']);
234-
$this->assertArrayHasKey('$ref', $json['definitions']['Question.jsonapi']['properties']['included']['items']['anyOf'][0]);
235-
$this->assertSame('#/definitions/Answer.jsonapi', $json['definitions']['Question.jsonapi']['properties']['included']['items']['anyOf'][0]['$ref']);
234+
$this->assertArrayHasKey('answer', $properties['relationships']['properties']);
235+
$this->assertArrayHasKey('anyOf', $included['items']);
236+
$this->assertCount(1, $included['items']['anyOf']);
237+
$this->assertArrayHasKey('$ref', $included['items']['anyOf'][0]);
238+
$this->assertSame('#/definitions/Answer.jsonapi', $included['items']['anyOf'][0]['$ref']);
239+
240+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\AnimalObservation', '--type' => 'output', '--format' => 'jsonapi']);
241+
$result = $this->tester->getDisplay();
242+
$json = json_decode($result, associative: true);
243+
$properties = $json['definitions']['AnimalObservation.jsonapi']['properties']['data']['properties'];
244+
$included = $json['definitions']['AnimalObservation.jsonapi']['properties']['included'];
245+
246+
$this->assertArrayHasKey('individuals', $properties['relationships']['properties']);
247+
$this->assertArrayNotHasKey('individuals', $properties['attributes']['properties']);
248+
$this->assertArrayHasKey('anyOf', $included['items']);
249+
$this->assertCount(1, $included['items']['anyOf']);
250+
$this->assertSame('#/definitions/Animal.jsonapi', $included['items']['anyOf'][0]['$ref']);
251+
252+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Animal', '--type' => 'output', '--format' => 'jsonapi']);
253+
$result = $this->tester->getDisplay();
254+
$json = json_decode($result, associative: true);
255+
$properties = $json['definitions']['Animal.jsonapi']['properties']['data']['properties'];
256+
$included = $json['definitions']['Animal.jsonapi']['properties']['included'];
257+
258+
$this->assertArrayHasKey('species', $properties['relationships']['properties']);
259+
$this->assertArrayNotHasKey('species', $properties['attributes']['properties']);
260+
$this->assertArrayHasKey('anyOf', $included['items']);
261+
$this->assertCount(1, $included['items']['anyOf']);
262+
$this->assertSame('#/definitions/Species.jsonapi', $included['items']['anyOf'][0]['$ref']);
263+
264+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Species', '--type' => 'output', '--format' => 'jsonapi']);
265+
$result = $this->tester->getDisplay();
266+
$json = json_decode($result, associative: true);
267+
$properties = $json['definitions']['Species.jsonapi']['properties']['data']['properties'];
268+
269+
$this->assertArrayHasKey('kingdom', $properties['attributes']['properties']);
270+
$this->assertArrayHasKey('phylum', $properties['attributes']['properties']);
236271
}
237272
}

0 commit comments

Comments
 (0)