Skip to content

Commit

Permalink
fix(metadata): generate skolem IRI by default
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 7, 2022
1 parent f0995c3 commit b25f8ed
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions features/jsonld/non_resource.feature
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Feature: JSON-LD non-resource handling
}
}
"""
And the JSON node "notAResource.@id" should not exist
And the JSON node "notAResource.@id" should exist

Scenario: Get a resource containing a raw object with selected properties
Given there are 1 dummy objects with relatedDummy and its thirdLevel
Expand Down Expand Up @@ -131,4 +131,4 @@ Feature: JSON-LD non-resource handling
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 node "totalPrice.@id" should exist
And the JSON node "totalPrice.@id" should not exist
6 changes: 6 additions & 0 deletions src/JsonLd/Serializer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\JsonLd\Serializer;

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand Down Expand Up @@ -73,6 +74,11 @@ public function normalize(mixed $object, string $format = null, array $context =
}

if (isset($originalResource)) {
try {
$context['output']['iri'] = $this->iriConverter->getIriFromResource($originalResource);
} catch (InvalidArgumentException) {
// The original resource has no identifiers
}
$context['api_resource'] = $originalResource;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ protected function getAttributeValue(object $object, string $attribute, string $
if ($type && $type->getClassName()) {
$childContext = $this->createChildContext($context, $attribute, $format);
unset($childContext['iri'], $childContext['uri_variables']);
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? false;
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;

return $this->serializer->normalize($attributeValue, $format, $childContext);
}
Expand Down
40 changes: 17 additions & 23 deletions tests/Fixtures/TestBundle/Entity/JsonSchemaContextDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,37 @@

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
* JSON Schema Context Dummy.
*
* @ApiResource
*
* @ORM\Entity
*/
#[ORM\Entity]
#[ApiResource]
class JsonSchemaContextDummy
{
/**
* @var int The id
*
* @ApiProperty(identifier=true)
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
#[ApiProperty(identifier: true)]
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
public $id;

/**
* @var array
*
* @ApiProperty(
* attributes={
* "json_schema_context"={
* "type"="array",
* "items"={"type"="string"},
* "minItems"=2,
* "maxItems"=2
* }
* },
* )
*/
#[ApiProperty(
jsonSchemaContext: [
'type' => 'array',
'items' => ['type' => 'string'],
'minItems' => 2,
'maxItems' => 2,
]
)]
private $things = ['pool', 'bag'];

public function getId()
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/Model/GenId.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#[Get('/genids/{id}', provider: [GenId::class, 'getData'])]
class GenId
{
#[ApiProperty(genId: true)]
#[ApiProperty(genId: false)]
public MonetaryAmount $totalPrice;

public function __construct(public int $id)
Expand Down
4 changes: 2 additions & 2 deletions tests/JsonLd/Serializer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testNormalizeWithOutput(): void
$serializerProphecy->normalize($dummy, null, Argument::type('array'))->willReturn(['name' => 'hello']);

$contextBuilderProphecy = $this->prophesize(AnonymousContextBuilderInterface::class);
$contextBuilderProphecy->getAnonymousResourceContext($dummy, ['api_resource' => $dummy])->shouldBeCalled()->willReturn(['@id' => '/dummy/1234', '@type' => 'Dummy', '@context' => []]);
$contextBuilderProphecy->getAnonymousResourceContext($dummy, ['api_resource' => $dummy, 'iri' => '/dummy/1234'])->shouldBeCalled()->willReturn(['@id' => '/dummy/1234', '@type' => 'Dummy', '@context' => []]);

$normalizer = new ObjectNormalizer(
$serializerProphecy->reveal(), // @phpstan-ignore-line
Expand Down Expand Up @@ -129,7 +129,7 @@ public function testNormalizeWithContext(): void
$serializerProphecy->normalize($dummy, null, Argument::type('array'))->willReturn(['name' => 'hello']);

$contextBuilderProphecy = $this->prophesize(AnonymousContextBuilderInterface::class);
$contextBuilderProphecy->getAnonymousResourceContext($dummy, ['api_resource' => $dummy, 'has_context' => true])->shouldBeCalled()->willReturn(['@id' => '/dummy/1234', '@type' => 'Dummy']);
$contextBuilderProphecy->getAnonymousResourceContext($dummy, ['api_resource' => $dummy, 'has_context' => true, 'iri' => '/dummy/1234'])->shouldBeCalled()->willReturn(['@id' => '/dummy/1234', '@type' => 'Dummy']);

$normalizer = new ObjectNormalizer(
$serializerProphecy->reveal(), // @phpstan-ignore-line
Expand Down

0 comments on commit b25f8ed

Please sign in to comment.