Skip to content

Commit 5ddf94a

Browse files
authored
feat(jsonld): add resource-level jsonldContext for namespace prefixes (#8204)
1 parent d219071 commit 5ddf94a

25 files changed

Lines changed: 105 additions & 1 deletion

src/JsonLd/ContextBuilder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ private function generateContextUri(?string $shortName, ?int $referenceType): st
180180
private function getResourceContextWithShortname(string $resourceClass, int $referenceType, string $shortName, ?HttpOperation $operation = null): array
181181
{
182182
$context = $this->getBaseContext($referenceType);
183+
184+
if ($operation && $jsonldContext = $operation->getJsonldContext()) {
185+
$context = array_merge($context, $jsonldContext);
186+
}
187+
183188
$propertyContext = $operation ? ['normalization_groups' => $operation->getNormalizationContext()['groups'] ?? null, 'denormalization_groups' => $operation->getDenormalizationContext()['groups'] ?? null] : ['normalization_groups' => [], 'denormalization_groups' => []];
184189

185190
foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {

src/Metadata/ApiResource.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ public function __construct(
326326
protected ?array $denormalizationContext = null,
327327
protected ?bool $collectDenormalizationErrors = null,
328328
protected ?array $hydraContext = null,
329+
/**
330+
* Extra entries to merge into the JSON-LD `@context` for this resource (e.g. namespace prefix declarations).
331+
*
332+
* Example: `jsonldContext: ['dct' => 'http://purl.org/dc/terms/']`
333+
*
334+
* @see https://api-platform.com/docs/core/extending-jsonld-context/
335+
*/
336+
protected ?array $jsonldContext = null,
329337
protected bool|OpenApiOperation|null $openapi = null,
330338
/**
331339
* The `validationContext` option configures the context of validation for the current ApiResource.
@@ -1373,6 +1381,19 @@ public function withHydraContext(array $hydraContext): static
13731381
return $self;
13741382
}
13751383

1384+
public function getJsonldContext(): ?array
1385+
{
1386+
return $this->jsonldContext;
1387+
}
1388+
1389+
public function withJsonldContext(array $jsonldContext): static
1390+
{
1391+
$self = clone $this;
1392+
$self->jsonldContext = $jsonldContext;
1393+
1394+
return $self;
1395+
}
1396+
13761397
public function getOpenapi(): bool|OpenApiOperation|null
13771398
{
13781399
return $this->openapi;

src/Metadata/Delete.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function __construct(
4444
?array $cacheHeaders = null,
4545
?array $paginationViaCursor = null,
4646
?array $hydraContext = null,
47+
?array $jsonldContext = null,
4748
bool|OpenApiOperation|Webhook|null $openapi = null,
4849
?array $exceptionToStatus = null,
4950
?bool $queryParameterValidationEnabled = null,
@@ -129,6 +130,7 @@ public function __construct(
129130
cacheHeaders: $cacheHeaders,
130131
paginationViaCursor: $paginationViaCursor,
131132
hydraContext: $hydraContext,
133+
jsonldContext: $jsonldContext,
132134
openapi: $openapi,
133135
exceptionToStatus: $exceptionToStatus,
134136
queryParameterValidationEnabled: $queryParameterValidationEnabled,

src/Metadata/Error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function __construct(
4444
?array $cacheHeaders = null,
4545
?array $paginationViaCursor = null,
4646
?array $hydraContext = null,
47+
?array $jsonldContext = null,
4748
bool|OpenApiOperation|Webhook|null $openapi = null,
4849
?array $exceptionToStatus = null,
4950
?bool $queryParameterValidationEnabled = null,
@@ -123,6 +124,7 @@ public function __construct(
123124
cacheHeaders: $cacheHeaders,
124125
paginationViaCursor: $paginationViaCursor,
125126
hydraContext: $hydraContext,
127+
jsonldContext: $jsonldContext,
126128
openapi: $openapi,
127129
exceptionToStatus: $exceptionToStatus,
128130
queryParameterValidationEnabled: $queryParameterValidationEnabled,

src/Metadata/ErrorResource.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function __construct(
4949
?array $denormalizationContext = null,
5050
?bool $collectDenormalizationErrors = null,
5151
?array $hydraContext = null,
52+
?array $jsonldContext = null,
5253
OpenApiOperation|bool|null $openapi = null,
5354
?array $validationContext = null,
5455
?array $filters = null,
@@ -116,6 +117,7 @@ class: $class,
116117
denormalizationContext: $denormalizationContext,
117118
collectDenormalizationErrors: $collectDenormalizationErrors,
118119
hydraContext: $hydraContext,
120+
jsonldContext: $jsonldContext,
119121
openapi: $openapi,
120122
validationContext: $validationContext,
121123
filters: $filters,

src/Metadata/Extractor/XmlResourceExtractor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ private function buildExtendedBase(\SimpleXMLElement $resource): array
9393
'schemes' => $this->buildArrayValue($resource, 'scheme'),
9494
'cacheHeaders' => $this->buildCacheHeaders($resource),
9595
'hydraContext' => isset($resource->hydraContext->values) ? $this->buildValues($resource->hydraContext->values) : null,
96+
'jsonldContext' => isset($resource->jsonldContext->values) ? $this->buildValues($resource->jsonldContext->values) : null,
9697
'openapi' => $this->buildOpenapi($resource),
9798
'paginationViaCursor' => $this->buildPaginationViaCursor($resource),
9899
'exceptionToStatus' => $this->buildExceptionToStatus($resource),

src/Metadata/Extractor/YamlResourceExtractor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ private function buildExtendedBase(array $resource): array
114114
'types' => $this->buildArrayValue($resource, 'types'),
115115
'cacheHeaders' => $this->buildArrayValue($resource, 'cacheHeaders'),
116116
'hydraContext' => $this->buildArrayValue($resource, 'hydraContext'),
117+
'jsonldContext' => $this->buildArrayValue($resource, 'jsonldContext'),
117118
'openapi' => $this->buildOpenapi($resource),
118119
'paginationViaCursor' => $this->buildArrayValue($resource, 'paginationViaCursor'),
119120
'exceptionToStatus' => $this->buildArrayValue($resource, 'exceptionToStatus'),

src/Metadata/Extractor/schema/resources.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@
479479
<xsd:element name="defaults" minOccurs="0" type="sequenceWithValues"/>
480480
<xsd:element name="formats" minOccurs="0" type="formats"/>
481481
<xsd:element name="hydraContext" minOccurs="0" type="sequenceWithValues"/>
482+
<xsd:element name="jsonldContext" minOccurs="0" type="sequenceWithValues"/>
482483
<xsd:element name="inputFormats" minOccurs="0" type="formats"/>
483484
<xsd:element name="openapi" minOccurs="0" type="openApiOperation"/>
484485
<xsd:element name="parameters" minOccurs="0" type="parameters"/>

src/Metadata/Get.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function __construct(
4444
?array $cacheHeaders = null,
4545
?array $paginationViaCursor = null,
4646
?array $hydraContext = null,
47+
?array $jsonldContext = null,
4748
bool|OpenApiOperation|Webhook|null $openapi = null,
4849
?array $exceptionToStatus = null,
4950
?bool $queryParameterValidationEnabled = null,
@@ -128,6 +129,7 @@ public function __construct(
128129
cacheHeaders: $cacheHeaders,
129130
paginationViaCursor: $paginationViaCursor,
130131
hydraContext: $hydraContext,
132+
jsonldContext: $jsonldContext,
131133
openapi: $openapi,
132134
exceptionToStatus: $exceptionToStatus,
133135
queryParameterValidationEnabled: $queryParameterValidationEnabled,

src/Metadata/GetCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function __construct(
4444
?array $cacheHeaders = null,
4545
?array $paginationViaCursor = null,
4646
?array $hydraContext = null,
47+
?array $jsonldContext = null,
4748
bool|OpenApiOperation|Webhook|null $openapi = null,
4849
?array $exceptionToStatus = null,
4950
?bool $queryParameterValidationEnabled = null,
@@ -129,6 +130,7 @@ public function __construct(
129130
cacheHeaders: $cacheHeaders,
130131
paginationViaCursor: $paginationViaCursor,
131132
hydraContext: $hydraContext,
133+
jsonldContext: $jsonldContext,
132134
openapi: $openapi,
133135
exceptionToStatus: $exceptionToStatus,
134136
queryParameterValidationEnabled: $queryParameterValidationEnabled,

0 commit comments

Comments
 (0)