Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions features/main/crud_uri_variables.feature
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,22 @@ Feature: Uri Variables
{
"@id": "/companies/2/employees/2",
"@type": "Employee",
"id": 2,
"name": "foo2",
"company": "/companies/2"
"company": {
"@id": "/companies/2",
"@type": "Company",
"name": "Foo Company 2"
}
},
{
"@id": "/companies/2/employees/3",
"@type": "Employee",
"id": 3,
"name": "foo3",
"company": "/companies/2"
"company": {
"@id": "/companies/2",
"@type": "Company",
"name": "Foo Company 2"
}
}
],
"hydra:totalItems": 2
Expand Down
8 changes: 7 additions & 1 deletion src/Core/GraphQl/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
*/
public function supportsNormalization($data, $format = null, array $context = []): bool
{
return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
if (!\is_object($data) || is_iterable($data)) {
return false;
}

$class = $this->getObjectClass($data);

return self::FORMAT === $format && $this->resourceClassResolver->isResourceClass($class);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private function getMetadata(string $className, string $type = Schema::TYPE_OUTP
}

if (!$operation) {
$operation = $op;
$operation = new HttpOperation();
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,19 @@ public function normalize($object, $format = null, array $context = [])
unset($context[self::IS_TRANSFORMED_TO_SAME_CLASS]);
}

$iri = null;
if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
$context = $this->initContext($resourceClass, $context);

if ($this->iriConverter instanceof LegacyIriConverterInterface) {
$iri = $this->iriConverter->getIriFromItem($object);
}
}

if (isset($context['iri'])) {
$iri = $context['iri'];
} else {
$iri = $this->iriConverter instanceof LegacyIriConverterInterface ? $this->iriConverter->getIriFromItem($object) : $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL, $context['operation'] ?? null, $context);
} elseif ($this->iriConverter instanceof IriConverterInterface) {
$iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL, $context['operation'] ?? null, $context);
}

$context['iri'] = $iri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ public function testOnKernelRequestWithUnsafeMethod()
}

/**
* If the tested filter is non-existant, then nothing should append.
* If the tested filter is non-existent, then nothing should append.
*/
public function testOnKernelRequestWithWrongFilter()
{
$request = [];

$this->filterLocatorProphecy->has('some_inexistent_filter')->willReturn(false);

$this->assertNull(
$this->testedInstance->validateFilters(Dummy::class, ['some_inexistent_filter'], $request)
);
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/TestBundle/Document/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Post;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ODM\Document
*/
#[ApiResource]
#[GetCollection]
#[Get]
#[Post]
#[ApiResource(uriTemplate: '/employees/{employeeId}/rooms/{roomId}/company/{companyId}', uriVariables: ['employeeId' => ['from_class' => Employee::class, 'from_property' => 'company']])]
Expand All @@ -42,6 +45,7 @@ class Company
*
* @ODM\Field
*/
#[Groups(['company_employees_read'])]
public $name;

/** @var Employee[] */
Expand Down
5 changes: 4 additions & 1 deletion tests/Fixtures/TestBundle/Document/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ODM\Document
Expand All @@ -26,7 +27,7 @@
#[Post]
#[ApiResource(uriTemplate: '/companies/{companyId}/employees/{id}', uriVariables: ['companyId' => ['from_class' => Company::class, 'to_property' => 'company'], 'id' => ['from_class' => Employee::class]])]
#[Get]
#[ApiResource(uriTemplate: '/companies/{companyId}/employees', uriVariables: ['companyId' => ['from_class' => Company::class, 'to_property' => 'company']])]
#[ApiResource(uriTemplate: '/companies/{companyId}/employees', uriVariables: ['companyId' => ['from_class' => Company::class, 'to_property' => 'company']], normalizationContext: ['groups' => ['company_employees_read']])]
#[GetCollection]
class Employee
{
Expand All @@ -42,13 +43,15 @@ class Employee
*
* @ODM\Field
*/
#[Groups(['company_employees_read'])]
public $name;

/**
* @var ?Company
*
* @ODM\ReferenceOne(targetDocument=Company::class, storeAs="id")
*/
#[Groups(['company_employees_read'])]
public $company;

public function getId()
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Post;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ORM\Entity
*/
#[ApiResource]
#[GetCollection]
#[Get]
#[Post]
#[ApiResource(
Expand Down Expand Up @@ -54,6 +57,7 @@ class Company
*
* @ORM\Column
*/
#[Groups(['company_employees_read'])]
public string $name;

/** @var Employee[] */
Expand Down
6 changes: 5 additions & 1 deletion tests/Fixtures/TestBundle/Entity/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ORM\Entity
Expand All @@ -36,7 +37,8 @@
uriTemplate: '/companies/{companyId}/employees',
uriVariables: [
'companyId' => ['from_class' => Company::class, 'to_property' => 'company'],
]
],
normalizationContext: ['groups' => ['company_employees_read']]
)]
#[GetCollection]
class Employee
Expand All @@ -55,11 +57,13 @@ class Employee
*
* @ORM\Column
*/
#[Groups(['company_employees_read'])]
public string $name;

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

public function getId()
Expand Down
5 changes: 4 additions & 1 deletion tests/Hydra/Serializer/CollectionFiltersNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,14 @@ public function testDoNothingIfNoRequestUri()
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($dummy, Dummy::class)->willReturn(Dummy::class);

$filterLocatorProphecy = $this->prophesize(ContainerInterface::class);
$filterLocatorProphecy->has('foo')->willReturn(false);

$normalizer = new CollectionFiltersNormalizer(
$decoratedProphecy->reveal(),
$resourceMetadataFactoryProphecy->reveal(),
$resourceClassResolverProphecy->reveal(),
$this->prophesize(ContainerInterface::class)->reveal()
$filterLocatorProphecy->reveal()
);

$this->assertEquals(['name' => 'foo'], $normalizer->normalize($dummy, CollectionNormalizer::FORMAT, [
Expand Down
22 changes: 6 additions & 16 deletions tests/JsonSchema/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testBuildSchemaForNonResourceClass(): void
$this->assertSame('example_bar', $definitions[$rootDefinitionKey]['properties']['bar']['example']);
}

public function testBuildSchemaForOperationWithOverriddenSerializerGroups(): void
public function testBuildSchemaWithSerializerGroups(): void
{
$typeFactoryProphecy = $this->prophesize(TypeFactoryInterface::class);
$typeFactoryProphecy->getType(Argument::allOf(
Expand All @@ -115,30 +115,20 @@ public function testBuildSchemaForOperationWithOverriddenSerializerGroups(): voi
])
);

$serializerGroup = 'overridden_operation_dummy_put';
$validationGroups = 'validation_groups_dummy_put';
$serializerGroup = 'custom_operation_dummy';

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(OverriddenOperationDummy::class, Argument::allOf(
Argument::type('array'),
Argument::allOf(Argument::withEntry('serializer_groups', [$serializerGroup]), Argument::withEntry('validation_groups', [$validationGroups]))
))->willReturn(new PropertyNameCollection(['alias', 'description']));
$propertyNameCollectionFactoryProphecy->create(OverriddenOperationDummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['alias', 'description']));

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'alias', Argument::allOf(
Argument::type('array'),
Argument::allOf(Argument::withEntry('serializer_groups', [$serializerGroup]), Argument::withEntry('validation_groups', [$validationGroups]))
))->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withReadable(true));
$propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'description', Argument::allOf(
Argument::type('array'),
Argument::allOf(Argument::withEntry('serializer_groups', [$serializerGroup]), Argument::withEntry('validation_groups', [$validationGroups]))
))->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withReadable(true));
$propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'alias', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withReadable(true));
$propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'description', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withReadable(true));

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->isResourceClass(OverriddenOperationDummy::class)->willReturn(true);

$schemaFactory = new SchemaFactory($typeFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
$resultSchema = $schemaFactory->buildSchema(OverriddenOperationDummy::class, 'json', Schema::TYPE_OUTPUT);
$resultSchema = $schemaFactory->buildSchema(OverriddenOperationDummy::class, 'json', Schema::TYPE_OUTPUT, null, null, ['groups' => $serializerGroup, AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false]);

$rootDefinitionKey = $resultSchema->getRootDefinitionKey();
$definitions = $resultSchema->getDefinitions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public function testWithResourceWithTraceables()
{
$this->apiResourceClassWillReturn(DummyEntity::class);

$this->filterLocator->has('a_filter')->willReturn(false);
$this->filterLocator->has('foo')->willReturn(false);

$dataCollector = new RequestDataCollector(
$this->metadataFactory->reveal(),
$this->filterLocator->reveal(),
Expand Down Expand Up @@ -220,6 +223,9 @@ public function testVersionCollection()
{
$this->apiResourceClassWillReturn(DummyEntity::class);

$this->filterLocator->has('a_filter')->willReturn(false);
$this->filterLocator->has('foo')->willReturn(false);

$dataCollector = new RequestDataCollector(
$this->metadataFactory->reveal(),
$this->filterLocator->reveal(),
Expand Down