Skip to content

Commit c2b0997

Browse files
committed
test: add functional test for JsonSchema generation
1 parent 4691c25 commit c2b0997

3 files changed

Lines changed: 179 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use Doctrine\ORM\Mapping as ORM;
18+
19+
#[ORM\Entity]
20+
#[ApiResource]
21+
class ChildAttribute
22+
{
23+
#[ORM\Id]
24+
#[ORM\GeneratedValue]
25+
#[ORM\Column]
26+
private ?int $id = null;
27+
28+
#[ORM\Column(length: 255)]
29+
private ?string $label = null;
30+
31+
#[ORM\Column(length: 255)]
32+
private ?string $hiddenData = null;
33+
34+
public function getId(): ?int
35+
{
36+
return $this->id;
37+
}
38+
39+
public function getLabel(): ?string
40+
{
41+
return $this->label;
42+
}
43+
44+
public function setLabel(string $label): self
45+
{
46+
$this->label = $label;
47+
48+
return $this;
49+
}
50+
51+
public function getHiddenData(): ?string
52+
{
53+
return $this->hiddenData;
54+
}
55+
56+
public function setHiddenData(string $hiddenData): self
57+
{
58+
$this->hiddenData = $hiddenData;
59+
60+
return $this;
61+
}
62+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use Doctrine\ORM\Mapping as ORM;
19+
20+
#[ORM\Entity]
21+
#[ApiResource(
22+
operations: [
23+
new Get(
24+
normalizationContext: ['attributes' => ['name', 'child' => ['label']]]
25+
),
26+
]
27+
)]
28+
class ParentAttribute
29+
{
30+
#[ORM\Id]
31+
#[ORM\GeneratedValue]
32+
#[ORM\Column]
33+
private ?int $id = null;
34+
35+
#[ORM\Column(length: 255)]
36+
private ?string $name = null;
37+
38+
#[ORM\Column(type: 'text')]
39+
private ?string $description = null;
40+
41+
#[ORM\ManyToOne(targetEntity: ChildAttribute::class)]
42+
private ?ChildAttribute $child = null;
43+
44+
public function getId(): ?int
45+
{
46+
return $this->id;
47+
}
48+
49+
public function getName(): ?string
50+
{
51+
return $this->name;
52+
}
53+
54+
public function setName(string $name): self
55+
{
56+
$this->name = $name;
57+
58+
return $this;
59+
}
60+
61+
public function getDescription(): ?string
62+
{
63+
return $this->description;
64+
}
65+
66+
public function setDescription(string $description): self
67+
{
68+
$this->description = $description;
69+
70+
return $this;
71+
}
72+
73+
public function getChild(): ?ChildAttribute
74+
{
75+
return $this->child;
76+
}
77+
78+
public function setChild(?ChildAttribute $child): self
79+
{
80+
$this->child = $child;
81+
82+
return $this;
83+
}
84+
}

tests/Functional/JsonSchema/JsonSchemaTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
2020
use ApiPlatform\Symfony\Bundle\Test\Constraint\MatchesJsonSchema;
2121
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\AggregateRating;
22+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ChildAttribute;
2223
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5452\Book;
2324
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5501\BrokenDocs;
2425
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5501\Related;
26+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ParentAttribute;
2527
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Product;
2628
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ResourceWithEnumProperty;
2729
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5793\BagOfTests;
@@ -66,6 +68,8 @@ public static function getResources(): array
6668
JsonSchemaResourceRelated::class,
6769
Product::class,
6870
AggregateRating::class,
71+
ParentAttribute::class,
72+
ChildAttribute::class,
6973
];
7074
}
7175

@@ -223,4 +227,33 @@ public function testGenIdFalse()
223227
$schema = $this->schemaFactory->buildSchema(Product::class, 'jsonld', Schema::TYPE_OUTPUT, $this->operationMetadataFactory->create('_api_/json-stream-products_get_collection'));
224228
$this->assertThat(['member' => [['aggregateRating' => ['ratingValue' => '1.0', 'reviewCount' => 1]]]], new MatchesJsonSchema($schema));
225229
}
230+
231+
public function testSchemaWithAttributes(): void
232+
{
233+
$operation = $this->operationMetadataFactory->create('_api_/parent_attributes/{id}{._format}_get');
234+
$context = $operation->getNormalizationContext() ?? [];
235+
236+
$schema = $this->schemaFactory->buildSchema(ParentAttribute::class, 'json', Schema::TYPE_OUTPUT, $operation, null, $context);
237+
238+
$parentDefinitionName = 'ParentAttribute-name_child.label';
239+
$this->assertArrayHasKey($parentDefinitionName, $schema['definitions']);
240+
241+
$parentProperties = $schema['definitions'][$parentDefinitionName]['properties'];
242+
243+
$this->assertArrayHasKey('name', $parentProperties);
244+
$this->assertArrayHasKey('child', $parentProperties);
245+
246+
$this->assertArrayNotHasKey('id', $parentProperties);
247+
$this->assertArrayNotHasKey('description', $parentProperties);
248+
249+
$childDefinitionName = 'ChildAttribute-label';
250+
$this->assertArrayHasKey($childDefinitionName, $schema['definitions']);
251+
252+
$childProperties = $schema['definitions'][$childDefinitionName]['properties'];
253+
254+
$this->assertArrayHasKey('label', $childProperties);
255+
256+
$this->assertArrayNotHasKey('hiddenData', $childProperties);
257+
$this->assertArrayNotHasKey('id', $childProperties);
258+
}
226259
}

0 commit comments

Comments
 (0)