Skip to content

Commit b25f8ed

Browse files
committed
fix(metadata): generate skolem IRI by default
1 parent f0995c3 commit b25f8ed

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

features/jsonld/non_resource.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Feature: JSON-LD non-resource handling
3838
}
3939
}
4040
"""
41-
And the JSON node "notAResource.@id" should not exist
41+
And the JSON node "notAResource.@id" should exist
4242

4343
Scenario: Get a resource containing a raw object with selected properties
4444
Given there are 1 dummy objects with relatedDummy and its thirdLevel
@@ -131,4 +131,4 @@ Feature: JSON-LD non-resource handling
131131
Then the response status code should be 200
132132
And the response should be in JSON
133133
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
134-
And the JSON node "totalPrice.@id" should exist
134+
And the JSON node "totalPrice.@id" should not exist

src/JsonLd/Serializer/ObjectNormalizer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\JsonLd\Serializer;
1515

1616
use ApiPlatform\Api\IriConverterInterface;
17+
use ApiPlatform\Exception\InvalidArgumentException;
1718
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
1819
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
1920
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
@@ -73,6 +74,11 @@ public function normalize(mixed $object, string $format = null, array $context =
7374
}
7475

7576
if (isset($originalResource)) {
77+
try {
78+
$context['output']['iri'] = $this->iriConverter->getIriFromResource($originalResource);
79+
} catch (InvalidArgumentException) {
80+
// The original resource has no identifiers
81+
}
7682
$context['api_resource'] = $originalResource;
7783
}
7884

src/Serializer/AbstractItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ protected function getAttributeValue(object $object, string $attribute, string $
592592
if ($type && $type->getClassName()) {
593593
$childContext = $this->createChildContext($context, $attribute, $format);
594594
unset($childContext['iri'], $childContext['uri_variables']);
595-
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? false;
595+
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;
596596

597597
return $this->serializer->normalize($attributeValue, $format, $childContext);
598598
}

tests/Fixtures/TestBundle/Entity/JsonSchemaContextDummy.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,37 @@
1313

1414
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;
1515

16-
use ApiPlatform\Core\Annotation\ApiProperty;
17-
use ApiPlatform\Core\Annotation\ApiResource;
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
1818
use Doctrine\ORM\Mapping as ORM;
1919

2020
/**
2121
* JSON Schema Context Dummy.
22-
*
23-
* @ApiResource
24-
*
25-
* @ORM\Entity
2622
*/
23+
#[ORM\Entity]
24+
#[ApiResource]
2725
class JsonSchemaContextDummy
2826
{
2927
/**
3028
* @var int The id
31-
*
32-
* @ApiProperty(identifier=true)
33-
* @ORM\Column(type="integer")
34-
* @ORM\Id
35-
* @ORM\GeneratedValue(strategy="AUTO")
3629
*/
37-
private $id;
30+
#[ApiProperty(identifier: true)]
31+
#[ORM\Column(type: 'integer')]
32+
#[ORM\Id]
33+
#[ORM\GeneratedValue(strategy: 'AUTO')]
34+
public $id;
3835

3936
/**
4037
* @var array
41-
*
42-
* @ApiProperty(
43-
* attributes={
44-
* "json_schema_context"={
45-
* "type"="array",
46-
* "items"={"type"="string"},
47-
* "minItems"=2,
48-
* "maxItems"=2
49-
* }
50-
* },
51-
* )
5238
*/
39+
#[ApiProperty(
40+
jsonSchemaContext: [
41+
'type' => 'array',
42+
'items' => ['type' => 'string'],
43+
'minItems' => 2,
44+
'maxItems' => 2,
45+
]
46+
)]
5347
private $things = ['pool', 'bag'];
5448

5549
public function getId()

tests/Fixtures/TestBundle/Model/GenId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#[Get('/genids/{id}', provider: [GenId::class, 'getData'])]
2121
class GenId
2222
{
23-
#[ApiProperty(genId: true)]
23+
#[ApiProperty(genId: false)]
2424
public MonetaryAmount $totalPrice;
2525

2626
public function __construct(public int $id)

tests/JsonLd/Serializer/ObjectNormalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function testNormalizeWithOutput(): void
9999
$serializerProphecy->normalize($dummy, null, Argument::type('array'))->willReturn(['name' => 'hello']);
100100

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

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

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

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

0 commit comments

Comments
 (0)