Skip to content

Commit

Permalink
fix!(state): precise format on content-location
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 19, 2024
1 parent 4a22716 commit 525171a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions features/hydra/collection.feature
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Feature: Collections support
When I send a "GET" request to "/dummies?page=7"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Location" should be equal to "/dummies.jsonld?page=7"
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
Expand Down
15 changes: 11 additions & 4 deletions src/State/Processor/RespondProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
&& ($isAlternateResourceMetadata || $canonicalUriTemplate)
) {
$canonicalOperation = $operation;
if ($this->operationMetadataFactory && null !== ($canonicalUriTemplate)) {
if ($this->operationMetadataFactory && null !== $canonicalUriTemplate) {
$canonicalOperation = $this->operationMetadataFactory->create($canonicalUriTemplate, $context);
}

Expand All @@ -119,6 +119,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =

$status ??= self::METHOD_TO_CODE[$method] ?? 200;

$requestParts = parse_url($request->getRequestUri());
if ($this->iriConverter && !isset($headers['Content-Location'])) {
try {
if ($hasData) {
Expand All @@ -127,10 +128,16 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
$iri = $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation);
}

$headers['Content-Location'] = sprintf('%s.%s', $iri, $request->getRequestFormat());
if ($iri) {
$location = \sprintf('%s.%s', $iri, $request->getRequestFormat());
if (isset($requestParts['query'])) {
$location .= '?'.$requestParts['query'];
}

if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method && !isset($headers['Location'])) {
$headers['Location'] = $iri;
$headers['Content-Location'] = $location;
if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method && !isset($headers['Location'])) {
$headers['Location'] = $iri;
}
}
} catch (InvalidArgumentException|ItemNotFoundException|RuntimeException) {
}
Expand Down

0 comments on commit 525171a

Please sign in to comment.