Skip to content

Commit bf1d9fd

Browse files
committed
fix!(state): precise format on content-location
1 parent 6d1e7cf commit bf1d9fd

File tree

8 files changed

+48
-31
lines changed

8 files changed

+48
-31
lines changed

features/main/crud.feature

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Feature: Create-Retrieve-Update-Delete
2222
Then the response status code should be 201
2323
And the response should be in JSON
2424
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
25-
And the header "Content-Location" should be equal to "/dummies/1"
25+
And the header "Content-Location" should be equal to "/dummies/1.jsonld"
2626
And the header "Location" should be equal to "/dummies/1"
2727
And the JSON should be equal to:
2828
"""
@@ -106,6 +106,7 @@ Feature: Create-Retrieve-Update-Delete
106106
Then the response status code should be 200
107107
And the response should be in JSON
108108
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
109+
And the header "Content-Location" should be equal to "/dummies.jsonld"
109110
And the JSON should be equal to:
110111
"""
111112
{
@@ -513,7 +514,7 @@ Feature: Create-Retrieve-Update-Delete
513514
Then the response status code should be 200
514515
And the response should be in JSON
515516
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
516-
And the header "Content-Location" should be equal to "/dummies/1"
517+
And the header "Content-Location" should be equal to "/dummies/1.jsonld"
517518
And the JSON should be equal to:
518519
"""
519520
{
@@ -571,7 +572,7 @@ Feature: Create-Retrieve-Update-Delete
571572
Then the response status code should be 201
572573
And the response should be in JSON
573574
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
574-
And the header "Content-Location" should be equal to "/processor_entities/1"
575+
And the header "Content-Location" should be equal to "/processor_entities/1.jsonld"
575576
And the header "Location" should be equal to "/processor_entities/1"
576577
And the JSON should be equal to:
577578
"""
@@ -596,7 +597,7 @@ Feature: Create-Retrieve-Update-Delete
596597
Then the response status code should be 201
597598
And the response should be in JSON
598599
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
599-
And the header "Content-Location" should be equal to "/provider_entities/1"
600+
And the header "Content-Location" should be equal to "/provider_entities/1.jsonld"
600601
And the header "Location" should be equal to "/provider_entities/1"
601602
And the JSON should be equal to:
602603
"""

features/main/crud_abstract.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Feature: Create-Retrieve-Update-Delete on abstract resource
1616
Then the response status code should be 201
1717
And the response should be in JSON
1818
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
19-
And the header "Content-Location" should be equal to "/concrete_dummies/1"
19+
And the header "Content-Location" should be equal to "/concrete_dummies/1.jsonld"
2020
And the header "Location" should be equal to "/concrete_dummies/1"
2121
And the JSON should be equal to:
2222
"""
@@ -92,7 +92,7 @@ Feature: Create-Retrieve-Update-Delete on abstract resource
9292
Then the response status code should be 200
9393
And the response should be in JSON
9494
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
95-
And the header "Content-Location" should be equal to "/concrete_dummies/1"
95+
And the header "Content-Location" should be equal to "/concrete_dummies/1.jsonld"
9696
And the JSON should be equal to:
9797
"""
9898
{
@@ -118,7 +118,7 @@ Feature: Create-Retrieve-Update-Delete on abstract resource
118118
Then the response status code should be 200
119119
And the response should be in JSON
120120
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
121-
And the header "Content-Location" should be equal to "/concrete_dummies/1"
121+
And the header "Content-Location" should be equal to "/concrete_dummies/1.jsonld"
122122
And the JSON should be equal to:
123123
"""
124124
{
@@ -150,7 +150,7 @@ Feature: Create-Retrieve-Update-Delete on abstract resource
150150
Then the response status code should be 201
151151
And the response should be in JSON
152152
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
153-
And the header "Content-Location" should be equal to "/concrete_dummies/1"
153+
And the header "Content-Location" should be equal to "/concrete_dummies/1.jsonld"
154154
And the header "Location" should be equal to "/concrete_dummies/1"
155155
And the JSON should be equal to:
156156
"""

features/main/crud_uri_variables.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Feature: Uri Variables
1313
Then the response status code should be 201
1414
And the response should be in JSON
1515
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
16-
And the header "Content-Location" should be equal to "/companies/1"
16+
And the header "Content-Location" should be equal to "/companies/1.jsonld"
1717
And the header "Location" should be equal to "/companies/1"
1818
And the JSON should be equal to:
1919
"""
@@ -51,7 +51,7 @@ Feature: Uri Variables
5151
Then the response status code should be 201
5252
And the response should be in JSON
5353
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
54-
And the header "Content-Location" should be equal to "/companies/1/employees/1"
54+
And the header "Content-Location" should be equal to "/companies/1/employees/1.jsonld"
5555
And the header "Location" should be equal to "/companies/1/employees/1"
5656
And the JSON should be equal to:
5757
"""

features/main/custom_normalized.feature

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Feature: Using custom normalized entity
1616
Then the response status code should be 201
1717
And the response should be in JSON
1818
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
19-
And the header "Content-Location" should be equal to "/custom_normalized_dummies/1"
19+
And the header "Content-Location" should be equal to "/custom_normalized_dummies/1.jsonld"
2020
And the header "Location" should be equal to "/custom_normalized_dummies/1"
2121
And the JSON should be equal to:
2222
"""
@@ -43,7 +43,7 @@ Feature: Using custom normalized entity
4343
Then the response status code should be 201
4444
And the response should be in JSON
4545
And the header "Content-Type" should be equal to "application/json; charset=utf-8"
46-
And the header "Content-Location" should be equal to "/related_normalized_dummies/1"
46+
And the header "Content-Location" should be equal to "/related_normalized_dummies/1.json"
4747
And the header "Location" should be equal to "/related_normalized_dummies/1"
4848
And the JSON should be equal to:
4949
"""
@@ -92,7 +92,7 @@ Feature: Using custom normalized entity
9292
Then the response status code should be 200
9393
And the response should be in JSON
9494
And the header "Content-Type" should be equal to "application/json; charset=utf-8"
95-
And the header "Content-Location" should be equal to "/related_normalized_dummies/1"
95+
And the header "Content-Location" should be equal to "/related_normalized_dummies/1.json"
9696
And the JSON should be equal to:
9797
"""
9898
{
@@ -158,7 +158,7 @@ Feature: Using custom normalized entity
158158
Then the response status code should be 200
159159
And the response should be in JSON
160160
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
161-
And the header "Content-Location" should be equal to "/custom_normalized_dummies/1"
161+
And the header "Content-Location" should be equal to "/custom_normalized_dummies/1.jsonld"
162162
And the JSON should be equal to:
163163
"""
164164
{
@@ -182,7 +182,7 @@ Feature: Using custom normalized entity
182182
Then the response status code should be 200
183183
And the response should be in JSON
184184
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
185-
And the header "Content-Location" should be equal to "/custom_normalized_dummies/1"
185+
And the header "Content-Location" should be equal to "/custom_normalized_dummies/1.jsonld"
186186
And the JSON should be equal to:
187187
"""
188188
{

features/main/custom_writable_identifier.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Feature: Using custom writable identifier on resource
1616
Then the response status code should be 201
1717
And the response should be in JSON
1818
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
19-
And the header "Content-Location" should be equal to "/custom_writable_identifier_dummies/my_slug"
19+
And the header "Content-Location" should be equal to "/custom_writable_identifier_dummies/my_slug.jsonld"
2020
And the header "Location" should be equal to "/custom_writable_identifier_dummies/my_slug"
2121
And the JSON should be equal to:
2222
"""
@@ -81,7 +81,7 @@ Feature: Using custom writable identifier on resource
8181
Then the response status code should be 200
8282
And the response should be in JSON
8383
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
84-
And the header "Content-Location" should be equal to "/custom_writable_identifier_dummies/slug_modified"
84+
And the header "Content-Location" should be equal to "/custom_writable_identifier_dummies/slug_modified.jsonld"
8585
And the JSON should be equal to:
8686
"""
8787
{

features/main/operation_resource.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Feature: Resource operations
5353
Then the response status code should be 200
5454
And the response should be in JSON
5555
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
56-
And the header "Content-Location" should be equal to "/operation_resources/1"
56+
And the header "Content-Location" should be equal to "/operation_resources/1.jsonld"
5757
And the JSON should be equal to:
5858
"""
5959
{

features/main/uuid.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Feature: Using uuid identifier on resource
1616
Then the response status code should be 201
1717
And the response should be in JSON
1818
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
19-
And the header "Content-Location" should be equal to "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78"
19+
And the header "Content-Location" should be equal to "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78.jsonld"
2020
And the header "Location" should be equal to "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78"
2121

2222
Scenario: Get a resource
@@ -69,7 +69,7 @@ Feature: Using uuid identifier on resource
6969
Then the response status code should be 200
7070
And the response should be in JSON
7171
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
72-
And the header "Content-Location" should be equal to "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78"
72+
And the header "Content-Location" should be equal to "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78.jsonld"
7373
And the JSON should be equal to:
7474
"""
7575
{
@@ -90,7 +90,7 @@ Feature: Using uuid identifier on resource
9090
Then the response status code should be 201
9191
And the response should be in JSON
9292
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
93-
And the header "Content-Location" should be equal to "/custom_generated_identifiers/foo"
93+
And the header "Content-Location" should be equal to "/custom_generated_identifiers/foo.jsonld"
9494
And the header "Location" should be equal to "/custom_generated_identifiers/foo"
9595
And the JSON should be equal to:
9696
"""

src/State/Processor/RespondProcessor.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
namespace ApiPlatform\State\Processor;
1515

1616
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
17+
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
18+
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
19+
use ApiPlatform\Metadata\Exception\RuntimeException;
1720
use ApiPlatform\Metadata\HttpOperation;
1821
use ApiPlatform\Metadata\IriConverterInterface;
1922
use ApiPlatform\Metadata\Operation;
@@ -92,31 +95,44 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
9295
$hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
9396
$hasData = !$hasOutput ? false : ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData)));
9497

95-
if ($hasData && $this->iriConverter) {
98+
if ($hasData) {
99+
$isAlternateResourceMetadata = $operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false;
100+
$canonicalUriTemplate = $operation->getExtraProperties()['canonical_uri_template'] ?? null;
101+
96102
if (
97103
!isset($headers['Location'])
98104
&& 300 <= $status && $status < 400
99-
&& (($operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false) || ($operation->getExtraProperties()['canonical_uri_template'] ?? null))
105+
&& ($isAlternateResourceMetadata || $canonicalUriTemplate)
100106
) {
101107
$canonicalOperation = $operation;
102-
if ($this->operationMetadataFactory && null !== ($operation->getExtraProperties()['canonical_uri_template'] ?? null)) {
103-
$canonicalOperation = $this->operationMetadataFactory->create($operation->getExtraProperties()['canonical_uri_template'], $context);
108+
if ($this->operationMetadataFactory && null !== ($canonicalUriTemplate)) {
109+
$canonicalOperation = $this->operationMetadataFactory->create($canonicalUriTemplate, $context);
104110
}
105111

106-
$headers['Location'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, $canonicalOperation);
112+
if ($this->iriConverter) {
113+
$headers['Location'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, $canonicalOperation);
114+
}
107115
} elseif ('PUT' === $method && !$request->attributes->get('previous_data') && null === $status && ($operation instanceof Put && ($operation->getAllowCreate() ?? false))) {
108116
$status = 201;
109117
}
110118
}
111119

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

114-
if ($hasData && $this->iriConverter && !isset($headers['Content-Location'])) {
115-
$iri = $this->iriConverter->getIriFromResource($originalData);
116-
$headers['Content-Location'] = $iri;
122+
if ($this->iriConverter && !isset($headers['Content-Location'])) {
123+
try {
124+
if ($hasData) {
125+
$iri = $this->iriConverter->getIriFromResource($originalData);
126+
} else {
127+
$iri = $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation);
128+
}
117129

118-
if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method && !isset($headers['Location'])) {
119-
$headers['Location'] = $iri;
130+
$headers['Content-Location'] = sprintf('%s.%s', $iri, $request->getRequestFormat());
131+
132+
if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method && !isset($headers['Location'])) {
133+
$headers['Location'] = $iri;
134+
}
135+
} catch (InvalidArgumentException|ItemNotFoundException|RuntimeException) {
120136
}
121137
}
122138

0 commit comments

Comments
 (0)