Skip to content

Commit 4f5f567

Browse files
fix: remove hydra prefix on errors (#6624)
Co-authored-by: soyuka <[email protected]>
1 parent 05f7d79 commit 4f5f567

File tree

12 files changed

+39
-56
lines changed

12 files changed

+39
-56
lines changed

docs/guides/validate-incoming-data.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public function testValidation(): void
139139
// {
140140
// "@context": "/contexts/ConstraintViolationList",
141141
// "@type": "ConstraintViolationList",
142-
// "hydra:title": "An error occurred",
143-
// "hydra:description": "properties: The product must have the minimal properties required (\"description\", \"price\")",
142+
// "title": "An error occurred",
143+
// "description": "properties: The product must have the minimal properties required (\"description\", \"price\")",
144144
// "violations": [
145145
// {
146146
// "propertyPath": "properties",
@@ -151,7 +151,7 @@ public function testValidation(): void
151151
// ```
152152
$this->assertResponseStatusCodeSame(422);
153153
$this->assertJsonContains([
154-
'hydra:description' => 'properties: The product must have the minimal properties required ("description", "price")',
154+
'description' => 'properties: The product must have the minimal properties required ("description", "price")',
155155
'title' => 'An error occurred',
156156
'violations' => [
157157
['propertyPath' => 'properties', 'message' => 'The product must have the minimal properties required ("description", "price")'],

features/hydra/error.feature

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ Feature: Error handling
1717
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
1818
And the JSON node "type" should exist
1919
And the JSON node "title" should be equal to "An error occurred"
20-
And the JSON node "hydra:title" should be equal to "An error occurred"
2120
And the JSON node "detail" should exist
22-
And the JSON node "hydra:description" should exist
21+
And the JSON node "description" should exist
2322
And the JSON node "trace" should exist
2423
And the JSON node "status" should exist
2524
And the JSON node "@context" should not exist
@@ -47,10 +46,9 @@ Feature: Error handling
4746
}
4847
],
4948
"detail": "name: This value should not be blank.",
50-
"hydra:title": "An error occurred",
51-
"hydra:description": "name: This value should not be blank.",
52-
"type": "/validation_errors/c1051bb4-d103-4f74-8988-acbcafc7fdc3",
53-
"title": "An error occurred"
49+
"title": "An error occurred",
50+
"description": "name: This value should not be blank.",
51+
"type": "/validation_errors/c1051bb4-d103-4f74-8988-acbcafc7fdc3"
5452
}
5553
"""
5654

@@ -82,9 +80,8 @@ Feature: Error handling
8280
And the JSON node "@context" should not exist
8381
And the JSON node "type" should exist
8482
And the JSON node "title" should be equal to "An error occurred"
85-
And the JSON node "hydra:title" should be equal to "An error occurred"
8683
And the JSON node "detail" should exist
87-
And the JSON node "hydra:description" should exist
84+
And the JSON node "description" should exist
8885

8986
Scenario: Get an rfc 7807 bad method error
9087
When I add "Content-Type" header equal to "application/ld+json"
@@ -100,9 +97,8 @@ Feature: Error handling
10097
And the JSON node "@context" should not exist
10198
And the JSON node "type" should exist
10299
And the JSON node "title" should be equal to "An error occurred"
103-
And the JSON node "hydra:title" should be equal to "An error occurred"
104100
And the JSON node "detail" should exist
105-
And the JSON node "hydra:description" should exist
101+
And the JSON node "description" should exist
106102

107103
Scenario: Get an rfc 7807 validation error
108104
When I add "Content-Type" header equal to "application/ld+json"

features/jsonld/input_output.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Feature: JSON-LD DTO input and output
309309
"""
310310
Then the response status code should be 400
311311
And the response should be in JSON
312-
And the JSON node "hydra:description" should be equal to "The input data is misformatted."
312+
And the JSON node "description" should be equal to "The input data is misformatted."
313313

314314
@!mongodb
315315
Scenario: Reset password through an input DTO without DataTransformer

features/main/attribute_resource.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Feature: Resource attributes
100100
And the response should be in JSON
101101
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
102102
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
103-
And the JSON node "hydra:description" should be equal to 'Unable to generate an IRI for the item of type "ApiPlatform\Tests\Fixtures\TestBundle\Entity\IncompleteUriVariableConfigured"'
103+
And the JSON node "description" should be equal to 'Unable to generate an IRI for the item of type "ApiPlatform\Tests\Fixtures\TestBundle\Entity\IncompleteUriVariableConfigured"'
104104

105105
Scenario: Uri variables with Post operation
106106
When I add "Content-Type" header equal to "application/ld+json"

features/main/not_exposed.feature

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,22 @@ 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 JSON node "hydra:description" should be equal to "<hydra:description>"
174+
And the JSON node "hydra:description" should be equal to "<description>"
175175
Examples:
176-
| uri | hydra:description |
177-
| /.well-known/genid/12345 | This route is not exposed on purpose. It generates an IRI for a collection resource without identifier nor item operation. |
176+
| uri | description |
178177
| /tables/12345 | This route does not aim to be called. |
179178
| /forks/12345 | This route does not aim to be called. |
180179

180+
Scenario Outline: Get a not exposed route returns a 404 with an explanation
181+
When I send a "GET" request to "<uri>"
182+
Then the response status code should be 404
183+
And the response should be in JSON
184+
And the JSON node "description" should be equal to "<description>"
185+
Examples:
186+
| uri | description |
187+
| /.well-known/genid/12345 | This route is not exposed on purpose. It generates an IRI for a collection resource without identifier nor item operation. |
188+
189+
181190
Scenario: Get a single item still works
182191
When I send a "GET" request to "/cuillers/12345"
183192
Then the response status code should be 200

features/main/relation.feature

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ Feature: Relations support
472472
Then the response status code should be 400
473473
And the response should be in JSON
474474
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
475-
And the JSON node "hydra:description" should contain 'Invalid IRI "certainly not an IRI".'
475+
And the JSON node "description" should contain 'Invalid IRI "certainly not an IRI".'
476476

477477
Scenario: Passing an invalid type to a relation
478478
When I add "Content-Type" header equal to "application/ld+json"
@@ -493,20 +493,20 @@ Feature: Relations support
493493
"properties": {
494494
"@type": {
495495
"type": "string",
496-
"pattern": "^hydra:Error$"
496+
"pattern": "^Error$"
497497
},
498-
"hydra:title": {
498+
"title": {
499499
"type": "string",
500500
"pattern": "^An error occurred$"
501501
},
502-
"hydra:description": {
502+
"description": {
503503
"pattern": "^The type of the \"ApiPlatform\\\\Tests\\\\Fixtures\\\\TestBundle\\\\(Document|Entity)\\\\RelatedDummy\" resource must be \"array\" \\(nested document\\) or \"string\" \\(IRI\\), \"integer\" given.$"
504504
}
505505
},
506506
"required": [
507507
"@type",
508-
"hydra:title",
509-
"hydra:description"
508+
"title",
509+
"description"
510510
]
511511
}
512512
"""

features/main/union_intersect_types.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ Feature: Union/Intersect types
118118
Then the response status code should be 400
119119
And the response should be in JSON
120120
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
121-
And the JSON node "hydra:description" should be equal to 'Could not denormalize object of type "ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5452\ActivableInterface", no supporting normalizer found.'
121+
And the JSON node "description" should be equal to 'Could not denormalize object of type "ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5452\ActivableInterface", no supporting normalizer found.'

features/main/validation.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Feature: Using validations groups
8787
And the JSON node "violations[0].message" should be equal to "This value should not be null."
8888
And the JSON node "violations[0].propertyPath" should be equal to "test"
8989
And the JSON node "detail" should be equal to "test: This value should not be null."
90-
And the JSON node "hydra:description" should be equal to "test: This value should not be null."
90+
And the JSON node "description" should be equal to "test: This value should not be null."
9191
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
9292

9393
@!mongodb

features/serializer/vo_relations.feature

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,20 @@ Feature: Value object as ApiResource
148148
"properties": {
149149
"@type": {
150150
"type": "string",
151-
"pattern": "^hydra:Error$"
151+
"pattern": "^Error$"
152152
},
153-
"hydra:title": {
153+
"title": {
154154
"type": "string",
155155
"pattern": "^An error occurred$"
156156
},
157-
"hydra:description": {
157+
"description": {
158158
"pattern": "^Cannot create an instance of \"ApiPlatform\\\\Tests\\\\Fixtures\\\\TestBundle\\\\(Document|Entity)\\\\VoDummyCar\" from serialized data because its constructor requires the following parameters to be present : \"\\$drivers\".$"
159159
}
160160
},
161161
"required": [
162162
"@type",
163-
"hydra:title",
164-
"hydra:description"
163+
"title",
164+
"description"
165165
]
166166
}
167167
"""

src/JsonLd/ContextBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\JsonLd;
1515

1616
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
17+
use ApiPlatform\Metadata\Error;
1718
use ApiPlatform\Metadata\Get;
1819
use ApiPlatform\Metadata\HttpOperation;
1920
use ApiPlatform\Metadata\IriConverterInterface;
@@ -184,7 +185,7 @@ private function getResourceContextWithShortname(string $resourceClass, int $ref
184185
}
185186
}
186187

187-
if (false === ($this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true)) {
188+
if (false === ($this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true) || $operation instanceof Error) {
188189
return ['http://www.w3.org/ns/hydra/context.jsonld', $context];
189190
}
190191

0 commit comments

Comments
 (0)