Skip to content

Commit

Permalink
fix(jsonapi): re-add continue once relation is determined (#6325)
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch committed Apr 21, 2024
1 parent 97c8ae2 commit fce42e0
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/JsonApi/JsonSchema/SchemaFactory.php
Expand Up @@ -207,6 +207,7 @@ private function buildDefinitionPropertiesSchema(string $key, string $className,
'type' => 'array',
'items' => self::RELATION_PROPS,
];
continue;
}
if ('id' === $propertyName) {
$attributes['_id'] = $property;
Expand Down
43 changes: 43 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Animal.php
@@ -0,0 +1,43 @@
<?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\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;

#[ApiResource]
class Animal
{
private ?int $id = null;

#[ApiProperty(required: true)]
private ?Species $species = null;

public function getId(): ?int
{
return $this->id;
}

public function getSpecies(): ?Species
{
return $this->species;
}

public function setSpecies(?Species $species): static
{
$this->species = $species;

return $this;
}
}
60 changes: 60 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/AnimalObservation.php
@@ -0,0 +1,60 @@
<?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\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

#[ApiResource]
class AnimalObservation
{
private ?int $id = null;

#[ApiProperty(required: true)]
private Collection $individuals;

public function __construct()
{
$this->individuals = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
}

/** @return Collection<int, Animal> */
public function getIndividuals(): Collection
{
return $this->individuals;
}

public function addIndividual(Animal $individual): static
{
if (!$this->individuals->contains($individual)) {
$this->individuals->add($individual);
}

return $this;
}

public function removeIndividual(Animal $individual): static
{
$this->individuals->removeElement($individual);

return $this;
}
}
38 changes: 38 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Species.php
@@ -0,0 +1,38 @@
<?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\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;

#[ApiResource]
#[GetCollection(
uriTemplate: '/species',
)]
#[Get(
uriTemplate: '/species/{key}',
)]
final class Species
{
#[ApiProperty(identifier: true)]
public ?int $key = null;
public ?string $kingdom = null;
public ?string $phylum = null;
public ?string $order = null;
public ?string $family = null;
public ?string $genus = null;
public ?string $species = null;
}
43 changes: 39 additions & 4 deletions tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Expand Up @@ -228,10 +228,45 @@ public function testJsonApiIncludesSchema(): void
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Question', '--type' => 'output', '--format' => 'jsonapi']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['Question.jsonapi']['properties']['data']['properties'];
$included = $json['definitions']['Question.jsonapi']['properties']['included'];

$this->assertArrayHasKey('answer', $json['definitions']['Question.jsonapi']['properties']['data']['properties']['relationships']['properties']);
$this->assertArrayHasKey('anyOf', $json['definitions']['Question.jsonapi']['properties']['included']['items']);
$this->assertArrayHasKey('$ref', $json['definitions']['Question.jsonapi']['properties']['included']['items']['anyOf'][0]);
$this->assertSame('#/definitions/Answer.jsonapi', $json['definitions']['Question.jsonapi']['properties']['included']['items']['anyOf'][0]['$ref']);
$this->assertArrayHasKey('answer', $properties['relationships']['properties']);
$this->assertArrayHasKey('anyOf', $included['items']);
$this->assertCount(1, $included['items']['anyOf']);
$this->assertArrayHasKey('$ref', $included['items']['anyOf'][0]);
$this->assertSame('#/definitions/Answer.jsonapi', $included['items']['anyOf'][0]['$ref']);

$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\AnimalObservation', '--type' => 'output', '--format' => 'jsonapi']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['AnimalObservation.jsonapi']['properties']['data']['properties'];
$included = $json['definitions']['AnimalObservation.jsonapi']['properties']['included'];

$this->assertArrayHasKey('individuals', $properties['relationships']['properties']);
$this->assertArrayNotHasKey('individuals', $properties['attributes']['properties']);
$this->assertArrayHasKey('anyOf', $included['items']);
$this->assertCount(1, $included['items']['anyOf']);
$this->assertSame('#/definitions/Animal.jsonapi', $included['items']['anyOf'][0]['$ref']);

$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Animal', '--type' => 'output', '--format' => 'jsonapi']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['Animal.jsonapi']['properties']['data']['properties'];
$included = $json['definitions']['Animal.jsonapi']['properties']['included'];

$this->assertArrayHasKey('species', $properties['relationships']['properties']);
$this->assertArrayNotHasKey('species', $properties['attributes']['properties']);
$this->assertArrayHasKey('anyOf', $included['items']);
$this->assertCount(1, $included['items']['anyOf']);
$this->assertSame('#/definitions/Species.jsonapi', $included['items']['anyOf'][0]['$ref']);

$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Species', '--type' => 'output', '--format' => 'jsonapi']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['Species.jsonapi']['properties']['data']['properties'];

$this->assertArrayHasKey('kingdom', $properties['attributes']['properties']);
$this->assertArrayHasKey('phylum', $properties['attributes']['properties']);
}
}

0 comments on commit fce42e0

Please sign in to comment.