Skip to content

Commit 7ebff27

Browse files
authored
fix: errors without compatibility flag (#5841)
1 parent 4ac62b0 commit 7ebff27

File tree

20 files changed

+142
-56
lines changed

20 files changed

+142
-56
lines changed

features/hydra/error.feature

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ Feature: Error handling
4242
"""
4343
Then the response status code should be 400
4444
And the response should be in JSON
45-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
45+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
4646
And the JSON node "@context" should be equal to "/contexts/Error"
4747
And the JSON node "@type" should be equal to "hydra:Error"
4848
And the JSON node "hydra:title" should be equal to "An error occurred"
4949
And the JSON node "hydra:description" should be equal to 'Nested documents for attribute "relatedDummy" are not allowed. Use IRIs instead.'
5050
And the JSON node "trace" should exist
51-
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
5251

5352
Scenario: Get an error during deserialization of collection
5453
When I add "Content-Type" header equal to "application/ld+json"
@@ -63,7 +62,7 @@ Feature: Error handling
6362
"""
6463
Then the response status code should be 400
6564
And the response should be in JSON
66-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
65+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
6766
And the JSON node "@context" should be equal to "/contexts/Error"
6867
And the JSON node "@type" should be equal to "hydra:Error"
6968
And the JSON node "hydra:title" should be equal to "An error occurred"
@@ -80,7 +79,7 @@ Feature: Error handling
8079
"""
8180
Then the response status code should be 400
8281
And the response should be in JSON
83-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
82+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
8483
And the JSON node "@context" should be equal to "/contexts/Error"
8584
And the JSON node "@type" should be equal to "hydra:Error"
8685
And the JSON node "hydra:title" should be equal to "An error occurred"
@@ -98,7 +97,7 @@ Feature: Error handling
9897
"""
9998
Then the response status code should be 400
10099
And the response should be in JSON
101-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
100+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
102101
And the JSON node "@context" should be equal to "/contexts/Error"
103102
And the JSON node "@type" should be equal to "hydra:Error"
104103
And the JSON node "hydra:title" should be equal to "An error occurred"
@@ -152,3 +151,39 @@ Feature: Error handling
152151
Then the response status code should be 201
153152
And the response should be in JSON
154153
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
154+
155+
Scenario: Get an rfc 7807 validation error
156+
When I add "Content-Type" header equal to "application/ld+json"
157+
And I send a "POST" request to "/validation_exception_problems" with body:
158+
"""
159+
{}
160+
"""
161+
Then the response status code should be 422
162+
And the response should be in JSON
163+
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
164+
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
165+
And the JSON node "@context" should not exist
166+
167+
Scenario: Get an rfc 7807 error
168+
When I add "Content-Type" header equal to "application/ld+json"
169+
And I send a "POST" request to "/exception_problems" with body:
170+
"""
171+
{}
172+
"""
173+
Then the response status code should be 400
174+
And the response should be in JSON
175+
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
176+
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
177+
And the JSON node "@context" should not exist
178+
179+
Scenario: Get an rfc 7807 error with backward compatibility
180+
When I add "Content-Type" header equal to "application/ld+json"
181+
And I send a "POST" request to "/exception_problems_with_compatibility" with body:
182+
"""
183+
{}
184+
"""
185+
Then the response status code should be 400
186+
And the response should be in JSON
187+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
188+
And the header "Link" should not contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
189+
And the JSON node "@context" should exist

features/main/not_exposed.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Feature: Expose only a collection of objects
171171
When I send a "GET" request to "<uri>"
172172
Then the response status code should be 404
173173
And the response should be in JSON
174-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
174+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
175175
And the JSON node "hydra:description" should be equal to "<hydra:description>"
176176
Examples:
177177
| uri | hydra:description |

features/main/relation.feature

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -484,15 +484,12 @@ Feature: Relations support
484484
Then the response status code should be 400
485485
And the response should be in JSON
486486
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
487+
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
487488
And the JSON should be valid according to this schema:
488489
"""
489490
{
490491
"type": "object",
491492
"properties": {
492-
"@context": {
493-
"type": "string",
494-
"pattern": "^/contexts/Error$"
495-
},
496493
"@type": {
497494
"type": "string",
498495
"pattern": "^hydra:Error$"
@@ -506,7 +503,6 @@ Feature: Relations support
506503
}
507504
},
508505
"required": [
509-
"@context",
510506
"@type",
511507
"hydra:title",
512508
"hydra:description"

features/mongodb/filters.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Feature: Filters on collections
1010
When I send a "GET" request to "/dummies?relatedDummy.thirdLevel.badFourthLevel.level=4"
1111
Then the response status code should be 500
1212
And the response should be in JSON
13-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
13+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
1414
And the JSON node "@context" should be equal to "/contexts/Error"
1515
And the JSON node "@type" should be equal to "hydra:Error"
1616
And the JSON node "hydra:title" should be equal to "An error occurred"
@@ -21,7 +21,7 @@ Feature: Filters on collections
2121
When I send a "GET" request to "/dummies?relatedDummy.thirdLevel.fourthLevel.badThirdLevel.level=3"
2222
Then the response status code should be 500
2323
And the response should be in JSON
24-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
24+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
2525
And the JSON node "@context" should be equal to "/contexts/Error"
2626
And the JSON node "@type" should be equal to "hydra:Error"
2727
And the JSON node "hydra:title" should be equal to "An error occurred"

features/security/send_security_headers.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Feature: Send security header
1717
{"name": 1}
1818
"""
1919
Then the response status code should be 400
20-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
2120
And the header "X-Content-Type-Options" should be equal to "nosniff"
2221
And the header "X-Frame-Options" should be equal to "deny"
2322

features/security/strong_typing.feature

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Feature: Handle properly invalid data submitted to the API
5252
"""
5353
Then the response status code should be 400
5454
And the response should be in JSON
55-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
55+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
5656
And the JSON node "@context" should be equal to "/contexts/Error"
5757
And the JSON node "@type" should be equal to "hydra:Error"
5858
And the JSON node "hydra:title" should be equal to "An error occurred"
@@ -69,7 +69,7 @@ Feature: Handle properly invalid data submitted to the API
6969
"""
7070
Then the response status code should be 400
7171
And the response should be in JSON
72-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
72+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
7373
And the JSON node "@context" should be equal to "/contexts/Error"
7474
And the JSON node "@type" should be equal to "hydra:Error"
7575
And the JSON node "hydra:title" should be equal to "An error occurred"
@@ -87,7 +87,7 @@ Feature: Handle properly invalid data submitted to the API
8787
"""
8888
Then the response status code should be 400
8989
And the response should be in JSON
90-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
90+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
9191

9292
Scenario: Send non-array data when an array is expected
9393
When I add "Content-Type" header equal to "application/ld+json"
@@ -100,7 +100,7 @@ Feature: Handle properly invalid data submitted to the API
100100
"""
101101
Then the response status code should be 400
102102
And the response should be in JSON
103-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
103+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
104104
And the JSON node "@context" should be equal to "/contexts/Error"
105105
And the JSON node "@type" should be equal to "hydra:Error"
106106
And the JSON node "hydra:title" should be equal to "An error occurred"
@@ -118,7 +118,7 @@ Feature: Handle properly invalid data submitted to the API
118118
"""
119119
Then the response status code should be 400
120120
And the response should be in JSON
121-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
121+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
122122
And the JSON node "@context" should be equal to "/contexts/Error"
123123
And the JSON node "@type" should be equal to "hydra:Error"
124124
And the JSON node "hydra:title" should be equal to "An error occurred"
@@ -134,7 +134,7 @@ Feature: Handle properly invalid data submitted to the API
134134
"""
135135
Then the response status code should be 400
136136
And the response should be in JSON
137-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
137+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
138138
And the JSON node "@context" should be equal to "/contexts/Error"
139139
And the JSON node "@type" should be equal to "hydra:Error"
140140
And the JSON node "hydra:title" should be equal to "An error occurred"

features/security/validate_incoming_content-types.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ Feature: Validate incoming content type
1313
something
1414
"""
1515
Then the response status code should be 415
16-
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
16+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
1717
And the JSON node "hydra:description" should be equal to 'The content-type "text/plain" is not supported. Supported MIME types are "application/ld+json", "application/hal+json", "application/vnd.api+json", "application/xml", "text/xml", "application/json", "text/html", "application/graphql", "multipart/form-data".'

features/serializer/vo_relations.feature

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,12 @@ Feature: Value object as ApiResource
140140
"""
141141
Then the response status code should be 400
142142
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
143+
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
143144
And the JSON should be valid according to this schema:
144145
"""
145146
{
146147
"type": "object",
147148
"properties": {
148-
"@context": {
149-
"type": "string",
150-
"pattern": "^/contexts/Error$"
151-
},
152149
"@type": {
153150
"type": "string",
154151
"pattern": "^hydra:Error$"
@@ -162,7 +159,6 @@ Feature: Value object as ApiResource
162159
}
163160
},
164161
"required": [
165-
"@context",
166162
"@type",
167163
"hydra:title",
168164
"hydra:description"

src/Hydra/EventListener/AddLinkHeaderListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function onKernelResponse(ResponseEvent $event): void
5151

5252
$apiDocUrl = $this->urlGenerator->generate('api_doc', ['_format' => 'jsonld'], UrlGeneratorInterface::ABS_URL);
5353
$apiDocLink = new Link(ContextBuilder::HYDRA_NS.'apiDocumentation', $apiDocUrl);
54-
$linkProvider = $request->attributes->get('_links', new GenericLinkProvider());
54+
$linkProvider = $request->attributes->get('_api_platform_links', new GenericLinkProvider());
5555

5656
if (!$linkProvider instanceof EvolvableLinkProviderInterface) {
5757
return;
@@ -63,6 +63,6 @@ public function onKernelResponse(ResponseEvent $event): void
6363
}
6464
}
6565

66-
$request->attributes->set('_links', $linkProvider->withLink($apiDocLink));
66+
$request->attributes->set('_api_platform_links', $linkProvider->withLink($apiDocLink));
6767
}
6868
}

src/Hydra/State/HydraLinkProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
4040
}
4141

4242
$apiDocUrl = $this->urlGenerator->generate('api_doc', ['_format' => 'jsonld'], UrlGeneratorInterface::ABS_URL);
43-
$linkProvider = $request->attributes->get('_links') ?? new GenericLinkProvider();
43+
$linkProvider = $request->attributes->get('_api_platform_links') ?? new GenericLinkProvider();
4444

4545
foreach ($operation->getLinks() ?? [] as $link) {
4646
$linkProvider = $linkProvider->withLink($link);
4747
}
4848

4949
$link = new Link(ContextBuilder::HYDRA_NS.'apiDocumentation', $apiDocUrl);
50-
$request->attributes->set('_links', $linkProvider->withLink($link));
50+
$request->attributes->set('_api_platform_links', $linkProvider->withLink($link));
5151

5252
return $this->decorated->process($data, $operation, $uriVariables, $context);
5353
}

0 commit comments

Comments
 (0)