Skip to content

Commit 0d8b516

Browse files
authored
fix(metadata): no skolem iri by default (#5046)
* fix(metadata): no skolem iri by default * temp * genid
1 parent a433968 commit 0d8b516

File tree

15 files changed

+108
-5
lines changed

15 files changed

+108
-5
lines changed

features/jsonld/non_resource.feature

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

4243
Scenario: Get a resource containing a raw object with selected properties
4344
Given there are 1 dummy objects with relatedDummy and its thirdLevel
@@ -123,3 +124,11 @@ Feature: JSON-LD non-resource handling
123124
"id": 1
124125
}
125126
"""
127+
128+
@php8
129+
Scenario: Get a generated id
130+
When I send a "GET" request to "/genids/1"
131+
Then the response status code should be 200
132+
And the response should be in JSON
133+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
134+
And the JSON node "totalPrice.@id" should exist

src/Hydra/Serializer/CollectionNormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function normalize($object, $format = null, array $context = []): array
8787

8888
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
8989
$context = $this->initContext($resourceClass, $context);
90+
$context['api_collection_sub_level'] = true;
9091
$data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
9192

9293
if ($this->iriConverter instanceof LegacyIriConverterInterface) {

src/JsonLd/ContextBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ public function getAnonymousResourceContext($object, array $context = [], int $r
200200
}
201201
}
202202

203+
if ($this->iriConverter && isset($context['gen_id']) && true === $context['gen_id']) {
204+
$jsonLdContext['@id'] = $this->iriConverter->getIriFromResource($object);
205+
}
206+
207+
if (false === ($context['iri'] ?? null)) {
208+
trigger_deprecation('api-platform/core', '2.7', 'An anonymous resource will use a Skolem IRI in API Platform 3.0. Use #[ApiProperty(genId: false)] to keep this behavior in 3.0.');
209+
}
210+
203211
if ($context['has_context'] ?? false) {
204212
unset($jsonLdContext['@context']);
205213
}

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public function normalize($object, $format = null, array $context = [])
8989
$context = $this->initContext($resourceClass, $context);
9090
$metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
9191
} elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
92+
if ($context['api_collection_sub_level'] ?? false) {
93+
unset($context['api_collection_sub_level']);
94+
$context['output']['genid'] = true;
95+
$context['output']['iri'] = null;
96+
}
97+
9298
// We should improve what's behind the context creation, its probably more complicated then it should
9399
$metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
94100
}

src/JsonLd/Serializer/JsonLdContextTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private function createJsonLdContext(AnonymousContextBuilderInterface $contextBu
5151
{
5252
// We're in a collection, don't add the @context part
5353
if (isset($context['jsonld_has_context'])) {
54-
return $contextBuilder->getAnonymousResourceContext($object, ($context['output'] ?? []) + ['api_resource' => $context['api_resource'] ?? null, 'has_context' => true]);
54+
return $contextBuilder->getAnonymousResourceContext($object, ($context['output'] ?? []) + ['api_resource' => $context['api_resource'] ?? null, 'has_context' => true, 'iri' => false]);
5555
}
5656

5757
$context['jsonld_has_context'] = true;

src/Metadata/ApiProperty.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ final class ApiProperty
9292

9393
private $schema;
9494
private $initializable;
95+
private $genId;
9596

9697
/**
9798
* @var string[]
@@ -146,6 +147,7 @@ public function __construct(
146147
?array $builtinTypes = null,
147148
?array $schema = null,
148149
?bool $initializable = null,
150+
?bool $genId = null,
149151

150152
$iris = null,
151153

@@ -175,6 +177,7 @@ public function __construct(
175177
$this->builtinTypes = $builtinTypes;
176178
$this->schema = $schema;
177179
$this->initializable = $initializable;
180+
$this->genId = $genId;
178181
$this->iris = $iris;
179182
$this->extraProperties = $extraProperties;
180183
}
@@ -507,4 +510,20 @@ public function withIris($iris): self
507510

508511
return $metadata;
509512
}
513+
514+
/**
515+
* Whether to generate a skolem iri on anonymous resources.
516+
*/
517+
public function getGenId()
518+
{
519+
return $this->genId;
520+
}
521+
522+
public function withGenId(bool $genId): self
523+
{
524+
$metadata = clone $this;
525+
$metadata->genId = $genId;
526+
527+
return $metadata;
528+
}
510529
}

src/Metadata/Extractor/XmlPropertyExtractor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ protected function extractPath(string $path)
7171
'initializable' => $this->phpize($property, 'initializable', 'bool'),
7272
'extraProperties' => $this->buildExtraProperties($property, 'extraProperties'),
7373
'iris' => $this->buildArrayValue($property, 'iri'),
74+
'genId' => $this->phpize($property, 'genId', 'bool'),
7475
];
7576
}
7677
}

src/Metadata/Extractor/YamlPropertyExtractor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private function buildProperties(array $resourcesYaml): void
9292
'example' => $propertyValues['example'] ?? null,
9393
'builtinTypes' => $this->buildAttribute($propertyValues, 'builtinTypes'),
9494
'schema' => $this->buildAttribute($propertyValues, 'schema'),
95+
'genId' => $this->phpize($propertyValues, 'genId', 'bool'),
9596
];
9697
}
9798
}

src/Metadata/Extractor/schema/properties.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<xsd:attribute name="security" type="xsd:string"/>
4444
<xsd:attribute name="securityPostDenormalize" type="xsd:string"/>
4545
<xsd:attribute name="initializable" type="xsd:boolean"/>
46+
<xsd:attribute name="genId" type="xsd:boolean"/>
4647
</xsd:complexType>
4748

4849
<xsd:complexType name="types">

src/Serializer/AbstractItemNormalizer.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,9 @@ protected function getAttributeValue($object, $attribute, $format = null, array
831831
unset($childContext['iri'], $childContext['uri_variables']);
832832

833833
if ($propertyMetadata instanceof PropertyMetadata) {
834-
$childContext['output']['iri'] = $propertyMetadata->getIri();
834+
$childContext['output']['iri'] = $propertyMetadata->getIri() ?? false;
835835
} else {
836-
if (null !== ($propertyIris = $propertyMetadata->getIris())) {
837-
$childContext['output']['iri'] = 1 === \count($propertyIris) ? $propertyIris[0] : $propertyIris;
838-
}
836+
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? false;
839837
}
840838

841839
return $this->serializer->normalize($attributeValue, $format, $childContext);

0 commit comments

Comments
 (0)