Skip to content

Commit fcbd804

Browse files
authored
fix(jsonld): duplicate error fields when prefix is enabled (#7021)
1 parent 1298536 commit fcbd804

17 files changed

+45
-56
lines changed

features/hydra/collection.feature

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ Feature: Collections support
479479

480480
When I send a "GET" request to "/dummies?itemsPerPage=0&page=2"
481481
Then the response status code should be 400
482-
And the JSON node "description" should be equal to "Page should not be greater than 1 if limit is equal to 0"
482+
And the JSON node "detail" should be equal to "Page should not be greater than 1 if limit is equal to 0"
483483

484484
Scenario: Cursor-based pagination with an empty collection
485485
When I send a "GET" request to "/so_manies"

features/hydra/error.feature

+10-12
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ Feature: Error handling
1616
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
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
19-
And the JSON node "title" should be equal to "An error occurred"
19+
And the JSON node "title" should not exists
2020
And the JSON node "hydra:title" should be equal to "An error occurred"
2121
And the JSON node "detail" should exist
22-
And the JSON node "description" should exist
22+
And the JSON node "description" should not exist
2323
And the JSON node "hydra:description" should exist
2424
And the JSON node "trace" should exist
2525
And the JSON node "status" should exist
@@ -49,10 +49,8 @@ Feature: Error handling
4949
}
5050
],
5151
"detail": "name: This value should not be blank.",
52-
"title": "An error occurred",
5352
"hydra:title": "An error occurred",
5453
"hydra:description": "name: This value should not be blank.",
55-
"description": "name: This value should not be blank.",
5654
"type": "/validation_errors/c1051bb4-d103-4f74-8988-acbcafc7fdc3"
5755
}
5856
"""
@@ -69,7 +67,7 @@ Feature: Error handling
6967
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
7068
And the JSON node "@context" should exist
7169
And the JSON node "type" should exist
72-
And the JSON node "title" should be equal to "An error occurred"
70+
And the JSON node "hydra:title" should be equal to "An error occurred"
7371
And the JSON node "detail" should exist
7472

7573
Scenario: Get an rfc 7807 not found error
@@ -84,9 +82,9 @@ Feature: Error handling
8482
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
8583
And the JSON node "@context" should exist
8684
And the JSON node "type" should exist
87-
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"
8886
And the JSON node "detail" should exist
89-
And the JSON node "description" should exist
87+
And the JSON node "description" should not exist
9088

9189
Scenario: Get an rfc 7807 bad method error
9290
When I add "Content-Type" header equal to "application/ld+json"
@@ -101,9 +99,9 @@ Feature: Error handling
10199
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
102100
And the JSON node "@context" should exist
103101
And the JSON node "type" should exist
104-
And the JSON node "title" should be equal to "An error occurred"
102+
And the JSON node "hydra:title" should be equal to "An error occurred"
105103
And the JSON node "detail" should exist
106-
And the JSON node "description" should exist
104+
And the JSON node "description" should not exist
107105

108106
Scenario: Get an rfc 7807 validation error
109107
When I add "Content-Type" header equal to "application/ld+json"
@@ -118,7 +116,7 @@ Feature: Error handling
118116
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
119117
And the JSON node "@context" should exist
120118
And the JSON node "type" should exist
121-
And the JSON node "title" should be equal to "An error occurred"
119+
And the JSON node "hydra:title" should be equal to "An error occurred"
122120
And the JSON node "detail" should exist
123121
And the JSON node "violations" should exist
124122

@@ -133,8 +131,8 @@ Feature: Error handling
133131
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
134132
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
135133
And the JSON node "type" should exist
136-
And the JSON node "title" should be equal to "An error occurred"
134+
And the JSON node "hydra:title" should be equal to "An error occurred"
137135
And the JSON node "detail" should exist
138-
And the JSON node "description" should exist
136+
And the JSON node "description" should not exist
139137
And the JSON node "trace" should exist
140138
And the JSON node "status" should exist

features/jsonld/input_output.feature

+1-1
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 "description" should be equal to "The input data is misformatted."
312+
And the JSON node "detail" 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

+1-1
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 "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 "detail" 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/crud.feature

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Feature: Create-Retrieve-Update-Delete
9696
When I add "Content-Type" header equal to "application/ld+json"
9797
And I send a "POST" request to "/dummies"
9898
Then the response status code should be 400
99-
And the JSON node "description" should be equal to "Syntax error"
99+
And the JSON node "detail" should be equal to "Syntax error"
100100

101101
Scenario: Get a not found exception
102102
When I send a "GET" request to "/dummies/42"
@@ -554,7 +554,7 @@ Feature: Create-Retrieve-Update-Delete
554554
When I add "Content-Type" header equal to "application/ld+json"
555555
And I send a "PUT" request to "/dummies/1"
556556
Then the response status code should be 400
557-
And the JSON node "description" should be equal to "Syntax error"
557+
And the JSON node "detail" should be equal to "Syntax error"
558558

559559
Scenario: Delete a resource
560560
When I send a "DELETE" request to "/dummies/1"

features/main/not_exposed.feature

+2-2
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 JSON node "description" should be equal to "<description>"
174+
And the JSON node "detail" should be equal to "<description>"
175175
Examples:
176176
| uri | description |
177177
| /tables/12345 | This route does not aim to be called. |
@@ -181,7 +181,7 @@ Feature: Expose only a collection of objects
181181
When I send a "GET" request to "<uri>"
182182
Then the response status code should be 404
183183
And the response should be in JSON
184-
And the JSON node "description" should be equal to "<description>"
184+
And the JSON node "detail" should be equal to "<description>"
185185
Examples:
186186
| uri | description |
187187
| /.well-known/genid/12345 | This route is not exposed on purpose. It generates an IRI for a collection resource without identifier nor item operation. |

features/main/relation.feature

+5-5
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 "description" should contain 'Invalid IRI "certainly not an IRI".'
475+
And the JSON node "detail" 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"
@@ -495,18 +495,18 @@ Feature: Relations support
495495
"type": "string",
496496
"pattern": "^hydra:Error$"
497497
},
498-
"title": {
498+
"hydra:title": {
499499
"type": "string",
500500
"pattern": "^An error occurred$"
501501
},
502-
"description": {
502+
"detail": {
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-
"title",
509-
"description"
508+
"hydra:title",
509+
"detail"
510510
]
511511
}
512512
"""

features/main/union_intersect_types.feature

+1-1
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 "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 "detail" 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

+2-5
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ Feature: Using validations groups
3131
{
3232
"@context": "/contexts/ConstraintViolation",
3333
"@type": "ConstraintViolation",
34-
"title": "An error occurred",
35-
"description": "name: This value should not be null.",
34+
"detail": "name: This value should not be null.",
3635
"violations": [
3736
{
3837
"propertyPath": "name",
@@ -60,8 +59,7 @@ Feature: Using validations groups
6059
{
6160
"@context": "/contexts/ConstraintViolation",
6261
"@type": "ConstraintViolation",
63-
"title": "An error occurred",
64-
"description": "title: This value should not be null.",
62+
"detail": "title: This value should not be null.",
6563
"violations": [
6664
{
6765
"propertyPath": "title",
@@ -87,7 +85,6 @@ Feature: Using validations groups
8785
And the JSON node "violations[0].message" should be equal to "This value should not be null."
8886
And the JSON node "violations[0].propertyPath" should be equal to "test"
8987
And the JSON node "detail" 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."
9188
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
9289

9390
@!mongodb

features/mongodb/filters.feature

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ Feature: Filters on collections
1313
And the header "Content-Type" should be equal to "application/problem+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"
16-
And the JSON node "title" should be equal to "An error occurred"
17-
And the JSON node "description" should be equal to "Cannot use reference 'badFourthLevel' in class 'ThirdLevel' for lookup or graphLookup: dbRef references are not supported."
16+
And the JSON node "detail" should be equal to "Cannot use reference 'badFourthLevel' in class 'ThirdLevel' for lookup or graphLookup: dbRef references are not supported."
1817
And the JSON node "trace" should exist
1918

2019
Scenario: Error when getting collection with nested properties if references are not correctly stored (not owning side)
@@ -24,6 +23,5 @@ Feature: Filters on collections
2423
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
2524
And the JSON node "@context" should be equal to "/contexts/Error"
2625
And the JSON node "@type" should be equal to "hydra:Error"
27-
And the JSON node "title" should be equal to "An error occurred"
28-
And the JSON node "description" should be equal to "Cannot use reference 'badThirdLevel' in class 'FourthLevel' for lookup or graphLookup: dbRef references are not supported."
26+
And the JSON node "detail" should be equal to "Cannot use reference 'badThirdLevel' in class 'FourthLevel' for lookup or graphLookup: dbRef references are not supported."
2927
And the JSON node "trace" should exist

features/security/strong_typing.feature

+5-10
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ Feature: Handle properly invalid data submitted to the API
5555
And the header "Content-Type" should be equal to "application/problem+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"
58-
And the JSON node "title" should be equal to "An error occurred"
59-
And the JSON node "description" should be equal to 'The type of the "name" attribute must be "string", "NULL" given.'
58+
And the JSON node "detail" should be equal to 'The type of the "name" attribute must be "string", "NULL" given.'
6059

6160
Scenario: Create a resource with wrong value type for relation
6261
When I add "Content-Type" header equal to "application/ld+json"
@@ -72,8 +71,7 @@ Feature: Handle properly invalid data submitted to the API
7271
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
7372
And the JSON node "@context" should be equal to "/contexts/Error"
7473
And the JSON node "@type" should be equal to "hydra:Error"
75-
And the JSON node "title" should be equal to "An error occurred"
76-
And the JSON node "description" should be equal to 'Invalid IRI "1".'
74+
And the JSON node "detail" should be equal to 'Invalid IRI "1".'
7775
And the JSON node "trace" should exist
7876

7977
Scenario: Ignore invalid dates
@@ -116,8 +114,7 @@ Feature: Handle properly invalid data submitted to the API
116114
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
117115
And the JSON node "@context" should be equal to "/contexts/Error"
118116
And the JSON node "@type" should be equal to "hydra:Error"
119-
And the JSON node "title" should be equal to "An error occurred"
120-
And the JSON node "description" should be equal to 'The type of the "relatedDummies" attribute must be "array", "string" given.'
117+
And the JSON node "detail" should be equal to 'The type of the "relatedDummies" attribute must be "array", "string" given.'
121118
And the JSON node "trace" should exist
122119

123120
Scenario: Send an object where an array is expected
@@ -134,8 +131,7 @@ Feature: Handle properly invalid data submitted to the API
134131
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
135132
And the JSON node "@context" should be equal to "/contexts/Error"
136133
And the JSON node "@type" should be equal to "hydra:Error"
137-
And the JSON node "title" should be equal to "An error occurred"
138-
And the JSON node "description" should be equal to 'The type of the key "a" must be "int", "string" given.'
134+
And the JSON node "detail" should be equal to 'The type of the key "a" must be "int", "string" given.'
139135

140136
Scenario: Send a scalar having the bad type
141137
When I add "Content-Type" header equal to "application/ld+json"
@@ -150,8 +146,7 @@ Feature: Handle properly invalid data submitted to the API
150146
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
151147
And the JSON node "@context" should be equal to "/contexts/Error"
152148
And the JSON node "@type" should be equal to "hydra:Error"
153-
And the JSON node "title" should be equal to "An error occurred"
154-
And the JSON node "description" should be equal to 'The type of the "name" attribute must be "string", "integer" given.'
149+
And the JSON node "detail" should be equal to 'The type of the "name" attribute must be "string", "integer" given.'
155150

156151
Scenario: According to the JSON spec, allow numbers without explicit floating point for JSON formats
157152
When I add "Content-Type" header equal to "application/ld+json"

features/security/validate_incoming_content-types.feature

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ Feature: Validate incoming content type
1414
"""
1515
Then the response status code should be 415
1616
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
17-
And the JSON node "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".'
17+
And the JSON node "detail" 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

+2-7
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,13 @@ Feature: Value object as ApiResource
150150
"type": "string",
151151
"pattern": "^hydra:Error$"
152152
},
153-
"title": {
154-
"type": "string",
155-
"pattern": "^An error occurred$"
156-
},
157-
"description": {
153+
"detail": {
158154
"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\".$"
159155
}
160156
},
161157
"required": [
162158
"@type",
163-
"title",
164-
"description"
159+
"detail"
165160
]
166161
}
167162
"""

src/JsonLd/Serializer/ErrorNormalizer.php

+4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ public function normalize(mixed $object, ?string $format = null, array $context
4040

4141
if (isset($normalized['description'])) {
4242
$normalized['hydra:description'] = $normalized['description'];
43+
unset($normalized['description']);
4344
}
4445

4546
if (isset($normalized['title'])) {
4647
$normalized['hydra:title'] = $normalized['title'];
48+
// this is confusing as the field is also available in the Problem Detail Json representation
49+
// but we don't want to repeat the title in the response, tldr: use hydra without prefix
50+
unset($normalized['title']);
4751
}
4852

4953
return $normalized;

tests/Functional/ErrorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function formatsProvider(): array
6767
'401',
6868
[
6969
'@type' => 'hydra:Error',
70-
'description' => 'Unauthorized',
70+
'hydra:description' => 'Unauthorized',
7171
],
7272
],
7373
[

tests/Functional/ItemUriTemplateTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public function testIssue6718(): void
3737
'headers' => ['accept' => 'application/ld+json'],
3838
]);
3939
$this->assertResponseStatusCodeSame(404);
40-
$this->assertJsonContains(['description' => 'Not Found']);
40+
$this->assertJsonContains(['detail' => 'Not Found']);
4141
}
4242
}

tests/JsonLd/Serializer/ErrorNormalizerTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ public function testAddHydraPrefix(): void
2828
$res = $errorNormalizer->normalize(new \stdClass());
2929
$this->assertEquals('hydra:Error', $res['@type']);
3030
$this->assertArrayHasKey('hydra:description', $res);
31-
$this->assertEquals($res['hydra:description'], $res['description']);
31+
$this->assertEquals($res['hydra:description'], 'bar');
3232
$this->assertArrayHasKey('hydra:title', $res);
33-
$this->assertEquals($res['hydra:title'], $res['title']);
33+
$this->assertEquals($res['hydra:title'], 'foo');
34+
$this->assertArrayNotHasKey('title', $res);
35+
$this->assertArrayNotHasKey('description', $res);
3436
}
3537
}

0 commit comments

Comments
 (0)