Skip to content

Commit ecce956

Browse files
fix(schema): generation with default operation (#4818)
* fix: reproduce bug * fix: schema generation * fix: CI Symfony dev * fix(graphql): supports normalize legacy Co-authored-by: soyuka <[email protected]>
1 parent e3f09ed commit ecce956

File tree

12 files changed

+61
-28
lines changed

12 files changed

+61
-28
lines changed

features/main/crud_uri_variables.feature

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,22 @@ Feature: Uri Variables
112112
{
113113
"@id": "/companies/2/employees/2",
114114
"@type": "Employee",
115-
"id": 2,
116115
"name": "foo2",
117-
"company": "/companies/2"
116+
"company": {
117+
"@id": "/companies/2",
118+
"@type": "Company",
119+
"name": "Foo Company 2"
120+
}
118121
},
119122
{
120123
"@id": "/companies/2/employees/3",
121124
"@type": "Employee",
122-
"id": 3,
123125
"name": "foo3",
124-
"company": "/companies/2"
126+
"company": {
127+
"@id": "/companies/2",
128+
"@type": "Company",
129+
"name": "Foo Company 2"
130+
}
125131
}
126132
],
127133
"hydra:totalItems": 2

src/Core/GraphQl/Serializer/ItemNormalizer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
5656
*/
5757
public function supportsNormalization($data, $format = null, array $context = []): bool
5858
{
59-
return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
59+
if (!\is_object($data) || is_iterable($data)) {
60+
return false;
61+
}
62+
63+
$class = $this->getObjectClass($data);
64+
65+
return self::FORMAT === $format && $this->resourceClassResolver->isResourceClass($class);
6066
}
6167

6268
/**

src/JsonSchema/SchemaFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ private function getMetadata(string $className, string $type = Schema::TYPE_OUTP
295295
}
296296

297297
if (!$operation) {
298-
$operation = $op;
298+
$operation = new HttpOperation();
299299
}
300300
}
301301
}

src/Serializer/AbstractItemNormalizer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,19 @@ public function normalize($object, $format = null, array $context = [])
202202
unset($context[self::IS_TRANSFORMED_TO_SAME_CLASS]);
203203
}
204204

205+
$iri = null;
205206
if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
206207
$context = $this->initContext($resourceClass, $context);
208+
209+
if ($this->iriConverter instanceof LegacyIriConverterInterface) {
210+
$iri = $this->iriConverter->getIriFromItem($object);
211+
}
207212
}
208213

209214
if (isset($context['iri'])) {
210215
$iri = $context['iri'];
211-
} else {
212-
$iri = $this->iriConverter instanceof LegacyIriConverterInterface ? $this->iriConverter->getIriFromItem($object) : $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL, $context['operation'] ?? null, $context);
216+
} elseif ($this->iriConverter instanceof IriConverterInterface) {
217+
$iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL, $context['operation'] ?? null, $context);
213218
}
214219

215220
$context['iri'] = $iri;

tests/Api/QueryParameterValidator/QueryParameterValidatorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ public function testOnKernelRequestWithUnsafeMethod()
5858
}
5959

6060
/**
61-
* If the tested filter is non-existant, then nothing should append.
61+
* If the tested filter is non-existent, then nothing should append.
6262
*/
6363
public function testOnKernelRequestWithWrongFilter()
6464
{
6565
$request = [];
6666

67+
$this->filterLocatorProphecy->has('some_inexistent_filter')->willReturn(false);
68+
6769
$this->assertNull(
6870
$this->testedInstance->validateFilters(Dummy::class, ['some_inexistent_filter'], $request)
6971
);

tests/Fixtures/TestBundle/Document/Company.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515

1616
use ApiPlatform\Metadata\ApiResource;
1717
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
1819
use ApiPlatform\Metadata\Link;
1920
use ApiPlatform\Metadata\Post;
2021
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
22+
use Symfony\Component\Serializer\Annotation\Groups;
2123

2224
/**
2325
* @ODM\Document
2426
*/
2527
#[ApiResource]
28+
#[GetCollection]
2629
#[Get]
2730
#[Post]
2831
#[ApiResource(uriTemplate: '/employees/{employeeId}/rooms/{roomId}/company/{companyId}', uriVariables: ['employeeId' => ['from_class' => Employee::class, 'from_property' => 'company']])]
@@ -42,6 +45,7 @@ class Company
4245
*
4346
* @ODM\Field
4447
*/
48+
#[Groups(['company_employees_read'])]
4549
public $name;
4650

4751
/** @var Employee[] */

tests/Fixtures/TestBundle/Document/Employee.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Metadata\GetCollection;
1919
use ApiPlatform\Metadata\Post;
2020
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
21+
use Symfony\Component\Serializer\Annotation\Groups;
2122

2223
/**
2324
* @ODM\Document
@@ -26,7 +27,7 @@
2627
#[Post]
2728
#[ApiResource(uriTemplate: '/companies/{companyId}/employees/{id}', uriVariables: ['companyId' => ['from_class' => Company::class, 'to_property' => 'company'], 'id' => ['from_class' => Employee::class]])]
2829
#[Get]
29-
#[ApiResource(uriTemplate: '/companies/{companyId}/employees', uriVariables: ['companyId' => ['from_class' => Company::class, 'to_property' => 'company']])]
30+
#[ApiResource(uriTemplate: '/companies/{companyId}/employees', uriVariables: ['companyId' => ['from_class' => Company::class, 'to_property' => 'company']], normalizationContext: ['groups' => ['company_employees_read']])]
3031
#[GetCollection]
3132
class Employee
3233
{
@@ -42,13 +43,15 @@ class Employee
4243
*
4344
* @ODM\Field
4445
*/
46+
#[Groups(['company_employees_read'])]
4547
public $name;
4648

4749
/**
4850
* @var ?Company
4951
*
5052
* @ODM\ReferenceOne(targetDocument=Company::class, storeAs="id")
5153
*/
54+
#[Groups(['company_employees_read'])]
5255
public $company;
5356

5457
public function getId()

tests/Fixtures/TestBundle/Entity/Company.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515

1616
use ApiPlatform\Metadata\ApiResource;
1717
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
1819
use ApiPlatform\Metadata\Link;
1920
use ApiPlatform\Metadata\Post;
2021
use Doctrine\ORM\Mapping as ORM;
22+
use Symfony\Component\Serializer\Annotation\Groups;
2123

2224
/**
2325
* @ORM\Entity
2426
*/
2527
#[ApiResource]
28+
#[GetCollection]
2629
#[Get]
2730
#[Post]
2831
#[ApiResource(
@@ -54,6 +57,7 @@ class Company
5457
*
5558
* @ORM\Column
5659
*/
60+
#[Groups(['company_employees_read'])]
5761
public string $name;
5862

5963
/** @var Employee[] */

tests/Fixtures/TestBundle/Entity/Employee.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Metadata\GetCollection;
1919
use ApiPlatform\Metadata\Post;
2020
use Doctrine\ORM\Mapping as ORM;
21+
use Symfony\Component\Serializer\Annotation\Groups;
2122

2223
/**
2324
* @ORM\Entity
@@ -36,7 +37,8 @@
3637
uriTemplate: '/companies/{companyId}/employees',
3738
uriVariables: [
3839
'companyId' => ['from_class' => Company::class, 'to_property' => 'company'],
39-
]
40+
],
41+
normalizationContext: ['groups' => ['company_employees_read']]
4042
)]
4143
#[GetCollection]
4244
class Employee
@@ -55,11 +57,13 @@ class Employee
5557
*
5658
* @ORM\Column
5759
*/
60+
#[Groups(['company_employees_read'])]
5861
public string $name;
5962

6063
/**
6164
* @ORM\ManyToOne(targetEntity="ApiPlatform\Tests\Fixtures\TestBundle\Entity\Company")
6265
*/
66+
#[Groups(['company_employees_read'])]
6367
public ?Company $company;
6468

6569
public function getId()

tests/Hydra/Serializer/CollectionFiltersNormalizerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,14 @@ public function testDoNothingIfNoRequestUri()
239239
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
240240
$resourceClassResolverProphecy->getResourceClass($dummy, Dummy::class)->willReturn(Dummy::class);
241241

242+
$filterLocatorProphecy = $this->prophesize(ContainerInterface::class);
243+
$filterLocatorProphecy->has('foo')->willReturn(false);
244+
242245
$normalizer = new CollectionFiltersNormalizer(
243246
$decoratedProphecy->reveal(),
244247
$resourceMetadataFactoryProphecy->reveal(),
245248
$resourceClassResolverProphecy->reveal(),
246-
$this->prophesize(ContainerInterface::class)->reveal()
249+
$filterLocatorProphecy->reveal()
247250
);
248251

249252
$this->assertEquals(['name' => 'foo'], $normalizer->normalize($dummy, CollectionNormalizer::FORMAT, [

0 commit comments

Comments
 (0)