Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/JsonLd/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ private function generateContextUri(?string $shortName, ?int $referenceType): st
private function getResourceContextWithShortname(string $resourceClass, int $referenceType, string $shortName, ?HttpOperation $operation = null): array
{
$context = $this->getBaseContext($referenceType);

if ($operation && $jsonldContext = $operation->getJsonldContext()) {
$context = array_merge($context, $jsonldContext);
}

$propertyContext = $operation ? ['normalization_groups' => $operation->getNormalizationContext()['groups'] ?? null, 'denormalization_groups' => $operation->getDenormalizationContext()['groups'] ?? null] : ['normalization_groups' => [], 'denormalization_groups' => []];

foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
Expand Down
21 changes: 21 additions & 0 deletions src/Metadata/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ public function __construct(
protected ?array $denormalizationContext = null,
protected ?bool $collectDenormalizationErrors = null,
protected ?array $hydraContext = null,
/**
* Extra entries to merge into the JSON-LD `@context` for this resource (e.g. namespace prefix declarations).
*
* Example: `jsonldContext: ['dct' => 'http://purl.org/dc/terms/']`
*
* @see https://api-platform.com/docs/core/extending-jsonld-context/
*/
protected ?array $jsonldContext = null,
protected bool|OpenApiOperation|null $openapi = null,
/**
* The `validationContext` option configures the context of validation for the current ApiResource.
Expand Down Expand Up @@ -1373,6 +1381,19 @@ public function withHydraContext(array $hydraContext): static
return $self;
}

public function getJsonldContext(): ?array
{
return $this->jsonldContext;
}

public function withJsonldContext(array $jsonldContext): static
{
$self = clone $this;
$self->jsonldContext = $jsonldContext;

return $self;
}

public function getOpenapi(): bool|OpenApiOperation|null
{
return $this->openapi;
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(
?array $cacheHeaders = null,
?array $paginationViaCursor = null,
?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = null,
?array $exceptionToStatus = null,
?bool $queryParameterValidationEnabled = null,
Expand Down Expand Up @@ -129,6 +130,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(
?array $cacheHeaders = null,
?array $paginationViaCursor = null,
?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = null,
?array $exceptionToStatus = null,
?bool $queryParameterValidationEnabled = null,
Expand Down Expand Up @@ -123,6 +124,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/ErrorResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function __construct(
?array $denormalizationContext = null,
?bool $collectDenormalizationErrors = null,
?array $hydraContext = null,
?array $jsonldContext = null,
OpenApiOperation|bool|null $openapi = null,
?array $validationContext = null,
?array $filters = null,
Expand Down Expand Up @@ -116,6 +117,7 @@ class: $class,
denormalizationContext: $denormalizationContext,
collectDenormalizationErrors: $collectDenormalizationErrors,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
validationContext: $validationContext,
filters: $filters,
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Extractor/XmlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private function buildExtendedBase(\SimpleXMLElement $resource): array
'schemes' => $this->buildArrayValue($resource, 'scheme'),
'cacheHeaders' => $this->buildCacheHeaders($resource),
'hydraContext' => isset($resource->hydraContext->values) ? $this->buildValues($resource->hydraContext->values) : null,
'jsonldContext' => isset($resource->jsonldContext->values) ? $this->buildValues($resource->jsonldContext->values) : null,
'openapi' => $this->buildOpenapi($resource),
'paginationViaCursor' => $this->buildPaginationViaCursor($resource),
'exceptionToStatus' => $this->buildExceptionToStatus($resource),
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Extractor/YamlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ private function buildExtendedBase(array $resource): array
'types' => $this->buildArrayValue($resource, 'types'),
'cacheHeaders' => $this->buildArrayValue($resource, 'cacheHeaders'),
'hydraContext' => $this->buildArrayValue($resource, 'hydraContext'),
'jsonldContext' => $this->buildArrayValue($resource, 'jsonldContext'),
'openapi' => $this->buildOpenapi($resource),
'paginationViaCursor' => $this->buildArrayValue($resource, 'paginationViaCursor'),
'exceptionToStatus' => $this->buildArrayValue($resource, 'exceptionToStatus'),
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(
?array $cacheHeaders = null,
?array $paginationViaCursor = null,
?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = null,
?array $exceptionToStatus = null,
?bool $queryParameterValidationEnabled = null,
Expand Down Expand Up @@ -128,6 +129,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/GetCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(
?array $cacheHeaders = null,
?array $paginationViaCursor = null,
?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = null,
?array $exceptionToStatus = null,
?bool $queryParameterValidationEnabled = null,
Expand Down Expand Up @@ -129,6 +130,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
Expand Down
14 changes: 14 additions & 0 deletions src/Metadata/HttpOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public function __construct(
protected ?array $cacheHeaders = null,
protected ?array $paginationViaCursor = null,
protected ?array $hydraContext = null,
protected ?array $jsonldContext = null,
protected bool|OpenApiOperation|Webhook|null $openapi = null,
protected ?array $exceptionToStatus = null,
protected ?array $links = null,
Expand Down Expand Up @@ -629,6 +630,19 @@ public function withHydraContext(array $hydraContext): static
return $self;
}

public function getJsonldContext(): ?array
{
return $this->jsonldContext;
}

public function withJsonldContext(array $jsonldContext): static
{
$self = clone $this;
$self->jsonldContext = $jsonldContext;

return $self;
}

public function getOpenapi(): bool|OpenApiOperation|Webhook|null
{
return $this->openapi;
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/McpResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function __construct(
?array $cacheHeaders = null,
?array $paginationViaCursor = null,
?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = null,
?array $exceptionToStatus = null,
?array $links = null,
Expand Down Expand Up @@ -209,6 +210,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
links: $links,
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/McpTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public function __construct(
?array $cacheHeaders = null,
?array $paginationViaCursor = null,
?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = null,
?array $exceptionToStatus = null,
?array $links = null,
Expand Down Expand Up @@ -205,6 +206,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
links: $links,
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/NotExposed.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function __construct(
?array $paginationViaCursor = null,

?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = false,
?array $exceptionToStatus = null,

Expand Down Expand Up @@ -135,6 +136,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(
?array $cacheHeaders = null,
?array $paginationViaCursor = null,
?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = null,
?array $exceptionToStatus = null,
?bool $queryParameterValidationEnabled = null,
Expand Down Expand Up @@ -129,6 +130,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(
?array $cacheHeaders = null,
?array $paginationViaCursor = null,
?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = null,
?array $exceptionToStatus = null,
?bool $queryParameterValidationEnabled = null,
Expand Down Expand Up @@ -130,6 +131,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Put.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct(
?array $cacheHeaders = null,
?array $paginationViaCursor = null,
?array $hydraContext = null,
?array $jsonldContext = null,
bool|OpenApiOperation|Webhook|null $openapi = null,
?array $exceptionToStatus = null,
?bool $queryParameterValidationEnabled = null,
Expand Down Expand Up @@ -130,6 +131,7 @@ public function __construct(
cacheHeaders: $cacheHeaders,
paginationViaCursor: $paginationViaCursor,
hydraContext: $hydraContext,
jsonldContext: $jsonldContext,
openapi: $openapi,
exceptionToStatus: $exceptionToStatus,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
shortName: 'JsonLdContextDummy',
provider: [self::class, 'provide'],
processor: [self::class, 'process'],
jsonldContext: ['dct' => 'http://purl.org/dc/terms/'],
)]
class JsonLdContextDummy
{
Expand All @@ -29,6 +30,9 @@ class JsonLdContextDummy
#[ApiProperty(iris: ['https://schema.org/name'])]
public ?string $name = null;

#[ApiProperty(iris: ['dct:title'])]
public ?string $title = null;

#[ApiProperty(iris: ['https://schema.org/alternateName'])]
public ?string $alias = null;

Expand Down
9 changes: 9 additions & 0 deletions tests/Functional/JsonLd/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,13 @@ public function testEmbeddedRelationMappingIsPlainString(): void
$body = $response->toArray();
$this->assertSame('JsonLdContextDummy/embedded', $body['@context']['embedded']);
}

public function testResourceLevelJsonLdContextAddsNamespacePrefixes(): void
{
$response = self::createClient()->request('GET', '/contexts/JsonLdContextDummy');
$this->assertResponseIsSuccessful();
$body = $response->toArray();
$this->assertSame('http://purl.org/dc/terms/', $body['@context']['dct']);
$this->assertSame('dct:title', $body['@context']['title']);
}
}
Loading