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
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ indent_size = 4
indent_style = space
indent_size = 4

[*.yml]
[*.{yaml,yml}]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0",
"phpdocumentor/type-resolver": "^0.3 || ^0.4",
"phpspec/prophecy": "^1.8",
"phpstan/phpstan": "^0.11.3",
"phpstan/phpstan-doctrine": "^0.11.2",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-doctrine": "^0.11",
"phpstan/phpstan-phpunit": "^0.11",
"phpstan/phpstan-symfony": "^0.11.2",
"phpstan/phpstan-symfony": "^0.11",
"phpunit/phpunit": "^7.5.2",
"psr/log": "^1.0",
"ramsey/uuid": "^3.7",
Expand Down
179 changes: 110 additions & 69 deletions features/bootstrap/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Person as PersonDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\PersonToPet as PersonToPetDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Pet as PetDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Product as ProductDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Question as QuestionDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\RelatedDummy as RelatedDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\RelatedOwnedDummy as RelatedOwnedDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\RelatedOwningDummy as RelatedOwningDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\RelatedToDummyFriend as RelatedToDummyFriendDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\RelationEmbedder as RelationEmbedderDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\SecuredDummy as SecuredDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Taxon as TaxonDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ThirdLevel as ThirdLevelDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Address;
Expand Down Expand Up @@ -78,17 +80,20 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTableInheritanceNotApiResourceChild;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\EmbeddableDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\EmbeddedDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ExternalUser;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Foo;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FooDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FourthLevel;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Greeting;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\InternalUser;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Node;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Order;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Person;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\PersonToPet;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Pet;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Product;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Question;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RamseyUuidDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
Expand All @@ -97,10 +102,13 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedToDummyFriend;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelationEmbedder;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SecuredDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Site;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Taxon;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ThirdLevel;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\User;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UuidIdentifierDummy;
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -1227,6 +1235,108 @@ public function thereAreNbDummyDtoCustom($nb)
$this->manager->clear();
}

/**
* @Given there is an order with same customer and recipient
*/
public function thereIsAnOrderWithSameCustomerAndRecipient()
{
$customer = $this->isOrm() ? new Customer() : new CustomerDocument();
$customer->name = 'customer_name';

$address1 = $this->isOrm() ? new Address() : new AddressDocument();
$address1->name = 'foo';
$address2 = $this->isOrm() ? new Address() : new AddressDocument();
$address2->name = 'bar';

$order = $this->isOrm() ? new Order() : new OrderDocument();
$order->recipient = $customer;
$order->customer = $customer;

$customer->addresses->add($address1);
$customer->addresses->add($address2);

$this->manager->persist($address1);
$this->manager->persist($address2);
$this->manager->persist($customer);
$this->manager->persist($order);

$this->manager->flush();
$this->manager->clear();
}

/**
* @Given there are :nb sites with internal owner
*/
public function thereAreSitesWithInternalOwner(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$internalUser = new InternalUser();
$internalUser->setFirstname('Internal');
$internalUser->setLastname('User');
$internalUser->setEmail('[email protected]');
$internalUser->setInternalId('INT');
$site = new Site();
$site->setTitle('title');
$site->setDescription('description');
$site->setOwner($internalUser);
$this->manager->persist($site);
}
$this->manager->flush();
}

/**
* @Given there are :nb sites with external owner
*/
public function thereAreSitesWithExternalOwner(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$externalUser = new ExternalUser();
$externalUser->setFirstname('External');
$externalUser->setLastname('User');
$externalUser->setEmail('[email protected]');
$externalUser->setExternalId('EXT');
$site = new Site();
$site->setTitle('title');
$site->setDescription('description');
$site->setOwner($externalUser);
$this->manager->persist($site);
}
$this->manager->flush();
}

/**
* @Given there is the following taxon:
*/
public function thereIsTheFollowingTaxon(PyStringNode $dataNode): void
{
$data = json_decode((string) $dataNode, true);

$taxon = $this->isOrm() ? new Taxon() : new TaxonDocument();
$taxon->setCode($data['code']);
$this->manager->persist($taxon);

$this->manager->flush();
}

/**
* @Given there is the following product:
*/
public function thereIsTheFollowingProduct(PyStringNode $dataNode): void
{
$data = json_decode((string) $dataNode, true);

$product = $this->isOrm() ? new Product() : new ProductDocument();
$product->setCode($data['code']);
if (isset($data['mainTaxon'])) {
$mainTaxonId = (int) str_replace('/taxons/', '', $data['mainTaxon']);
$mainTaxon = $this->manager->getRepository($this->isOrm() ? Taxon::class : TaxonDocument::class)->find($mainTaxonId);
$product->setMainTaxon($mainTaxon);
}
$this->manager->persist($product);

$this->manager->flush();
}

private function isOrm(): bool
{
return null !== $this->schemaTool;
Expand Down Expand Up @@ -1532,73 +1642,4 @@ private function buildThirdLevel()
{
return $this->isOrm() ? new ThirdLevel() : new ThirdLevelDocument();
}

/**
* @Given there is a order with same customer and receiver
*/
public function testEagerLoadingNotDuplicateRelation()
{
$customer = $this->isOrm() ? new Customer() : new CustomerDocument();
$customer->name = 'customer_name';

$address1 = $this->isOrm() ? new Address() : new AddressDocument();
$address1->name = 'foo';
$address2 = $this->isOrm() ? new Address() : new AddressDocument();
$address2->name = 'bar';

$order = $this->isOrm() ? new Order() : new OrderDocument();
$order->recipient = $customer;
$order->customer = $customer;

$customer->addresses->add($address1);
$customer->addresses->add($address2);

$this->manager->persist($address1);
$this->manager->persist($address2);
$this->manager->persist($customer);
$this->manager->persist($order);

$this->manager->flush();
$this->manager->clear();
}

/**
* @Given there are :nb sites with internal owner
*/
public function thereAreSitesWithInternalOwner(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$internalUser = new \ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\InternalUser();
$internalUser->setFirstname('Internal');
$internalUser->setLastname('User');
$internalUser->setEmail('[email protected]');
$internalUser->setInternalId('INT');
$site = new \ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Site();
$site->setTitle('title');
$site->setDescription('description');
$site->setOwner($internalUser);
$this->manager->persist($site);
}
$this->manager->flush();
}

/**
* @Given there are :nb sites with external owner
*/
public function thereAreSitesWithExternalOwner(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$externalUser = new \ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ExternalUser();
$externalUser->setFirstname('External');
$externalUser->setLastname('User');
$externalUser->setEmail('[email protected]');
$externalUser->setExternalId('EXT');
$site = new \ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Site();
$site->setTitle('title');
$site->setDescription('description');
$site->setOwner($externalUser);
$this->manager->persist($site);
}
$this->manager->flush();
}
}
57 changes: 57 additions & 0 deletions features/jsonld/interface_as_resource.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Feature: JSON-LD using interface as resource
In order to use interface as resource
As a developer
I should be able to serialize objects of an interface as API resource.

Background:
Given I add "Accept" header equal to "application/ld+json"
And I add "Content-Type" header equal to "application/ld+json"

@createSchema
Scenario: Retrieve a taxon
Given there is the following taxon:
"""
{
"code": "WONDERFUL_TAXON"
}
"""
When I send a "GET" request to "/taxons/1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Taxon",
"@id": "/taxons/1",
"@type": "Taxon",
"code": "WONDERFUL_TAXON"
}
"""

Scenario: Retrieve a product with a main taxon
Given there is the following product:
"""
{
"code": "GREAT_PRODUCT",
"mainTaxon": "/taxons/1"
}
"""
When I send a "GET" request to "/products/1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Product",
"@id": "/products/1",
"@type": "Product",
"code": "GREAT_PRODUCT",
"mainTaxon": {
"@id": "/taxons/1",
"@type": "Taxon",
"code": "WONDERFUL_TAXON"
}
}
"""
2 changes: 1 addition & 1 deletion features/main/relation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ Feature: Relations support
"""

Scenario: Eager load relations should not be duplicated
Given there is a order with same customer and receiver
Given there is an order with same customer and recipient
When I add "Content-Type" header equal to "application/ld+json"
And I send a "GET" request to "/orders"
Then the response status code should be 200
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
includes:
- vendor/jangregor/phpstan-prophecy/src/extension.neon
- vendor/phpstan/phpstan-doctrine/extension.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-symfony/extension.neon

parameters:
level: 6
Expand Down
17 changes: 9 additions & 8 deletions src/Api/ResourceClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,33 @@ public function getResourceClass($value, string $resourceClass = null, bool $str
throw new InvalidArgumentException('Resource type could not be determined. Resource class must be specified.');
}

if (null !== $resourceClass && !$this->isResourceClass($resourceClass)) {
throw new InvalidArgumentException(sprintf('Specified class "%s" is not a resource class.', $resourceClass));
if (null !== $actualClass && !$this->isResourceClass($actualClass)) {
throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $actualClass));
}

if (null === $actualClass) {
return $resourceClass;
if (null !== $resourceClass && !$this->isResourceClass($resourceClass)) {
throw new InvalidArgumentException(sprintf('Specified class "%s" is not a resource class.', $resourceClass));
}

if ($strict && !is_a($actualClass, $resourceClass, true)) {
if ($strict && null !== $actualClass && !is_a($actualClass, $resourceClass, true)) {
throw new InvalidArgumentException(sprintf('Object of type "%s" does not match "%s" resource class.', $actualClass, $resourceClass));
}

$targetClass = $actualClass ?? $resourceClass;
$mostSpecificResourceClass = null;

foreach ($this->resourceNameCollectionFactory->create() as $resourceClassName) {
if (!is_a($actualClass, $resourceClassName, true)) {
if (!is_a($targetClass, $resourceClassName, true)) {
continue;
}

if (null === $mostSpecificResourceClass || is_subclass_of($resourceClassName, $mostSpecificResourceClass, true)) {
if (null === $mostSpecificResourceClass || is_subclass_of($resourceClassName, $mostSpecificResourceClass)) {
$mostSpecificResourceClass = $resourceClassName;
}
}

if (null === $mostSpecificResourceClass) {
throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $actualClass));
throw new \LogicException('Unexpected execution flow.');
}

return $mostSpecificResourceClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="serializer.mapping.class_metadata_factory" />
<argument type="service" id="api_platform.metadata.property.metadata_factory.serializer.inner" />
<argument type="service" id="api_platform.resource_class_resolver" />
</service>

<service id="api_platform.metadata.property.metadata_factory.cached" class="ApiPlatform\Core\Metadata\Property\Factory\CachedPropertyMetadataFactory" decorates="api_platform.metadata.property.metadata_factory" decoration-priority="-10" public="false">
Expand Down
2 changes: 1 addition & 1 deletion src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private function getProperty(PropertyMetadata $propertyMetadata, string $propert
{
$propertyData = [
'@id' => $propertyMetadata->getIri() ?? "#$shortName/$propertyName",
'@type' => $propertyMetadata->isReadableLink() ? 'rdf:Property' : 'hydra:Link',
'@type' => false === $propertyMetadata->isReadableLink() ? 'hydra:Link' : 'rdf:Property',
'rdfs:label' => $propertyName,
'domain' => $prefixedShortName,
];
Expand Down
Loading