From 4f5f5675629fe52ea415a6bd91f3625eedea9c87 Mon Sep 17 00:00:00 2001 From: Vincent <407859+vincentchalamon@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:55:59 +0200 Subject: [PATCH] fix: remove hydra prefix on errors (#6624) Co-authored-by: soyuka --- docs/guides/validate-incoming-data.php | 6 +++--- features/hydra/error.feature | 16 ++++++---------- features/jsonld/input_output.feature | 2 +- features/main/attribute_resource.feature | 2 +- features/main/not_exposed.feature | 15 ++++++++++++--- features/main/relation.feature | 12 ++++++------ features/main/union_intersect_types.feature | 2 +- features/main/validation.feature | 2 +- features/serializer/vo_relations.feature | 10 +++++----- src/JsonLd/ContextBuilder.php | 3 ++- src/State/ApiResource/Error.php | 15 --------------- src/Validator/Exception/ValidationException.php | 10 +--------- 12 files changed, 39 insertions(+), 56 deletions(-) diff --git a/docs/guides/validate-incoming-data.php b/docs/guides/validate-incoming-data.php index 2b8bbed03b2..afefd71189a 100644 --- a/docs/guides/validate-incoming-data.php +++ b/docs/guides/validate-incoming-data.php @@ -139,8 +139,8 @@ public function testValidation(): void // { // "@context": "/contexts/ConstraintViolationList", // "@type": "ConstraintViolationList", - // "hydra:title": "An error occurred", - // "hydra:description": "properties: The product must have the minimal properties required (\"description\", \"price\")", + // "title": "An error occurred", + // "description": "properties: The product must have the minimal properties required (\"description\", \"price\")", // "violations": [ // { // "propertyPath": "properties", @@ -151,7 +151,7 @@ public function testValidation(): void // ``` $this->assertResponseStatusCodeSame(422); $this->assertJsonContains([ - 'hydra:description' => 'properties: The product must have the minimal properties required ("description", "price")', + 'description' => 'properties: The product must have the minimal properties required ("description", "price")', 'title' => 'An error occurred', 'violations' => [ ['propertyPath' => 'properties', 'message' => 'The product must have the minimal properties required ("description", "price")'], diff --git a/features/hydra/error.feature b/features/hydra/error.feature index 85e574e43f4..6b473c7b5b1 100644 --- a/features/hydra/error.feature +++ b/features/hydra/error.feature @@ -17,9 +17,8 @@ Feature: Error handling And the header "Link" should contain '; rel="http://www.w3.org/ns/json-ld#error"' And the JSON node "type" should exist And the JSON node "title" should be equal to "An error occurred" - And the JSON node "hydra:title" should be equal to "An error occurred" And the JSON node "detail" should exist - And the JSON node "hydra:description" should exist + And the JSON node "description" should exist And the JSON node "trace" should exist And the JSON node "status" should exist And the JSON node "@context" should not exist @@ -47,10 +46,9 @@ Feature: Error handling } ], "detail": "name: This value should not be blank.", - "hydra:title": "An error occurred", - "hydra:description": "name: This value should not be blank.", - "type": "/validation_errors/c1051bb4-d103-4f74-8988-acbcafc7fdc3", - "title": "An error occurred" + "title": "An error occurred", + "description": "name: This value should not be blank.", + "type": "/validation_errors/c1051bb4-d103-4f74-8988-acbcafc7fdc3" } """ @@ -82,9 +80,8 @@ Feature: Error handling And the JSON node "@context" should not exist And the JSON node "type" should exist And the JSON node "title" should be equal to "An error occurred" - And the JSON node "hydra:title" should be equal to "An error occurred" And the JSON node "detail" should exist - And the JSON node "hydra:description" should exist + And the JSON node "description" should exist Scenario: Get an rfc 7807 bad method error When I add "Content-Type" header equal to "application/ld+json" @@ -100,9 +97,8 @@ Feature: Error handling And the JSON node "@context" should not exist And the JSON node "type" should exist And the JSON node "title" should be equal to "An error occurred" - And the JSON node "hydra:title" should be equal to "An error occurred" And the JSON node "detail" should exist - And the JSON node "hydra:description" should exist + And the JSON node "description" should exist Scenario: Get an rfc 7807 validation error When I add "Content-Type" header equal to "application/ld+json" diff --git a/features/jsonld/input_output.feature b/features/jsonld/input_output.feature index 73b1ba6e34c..6f65fa71c7f 100644 --- a/features/jsonld/input_output.feature +++ b/features/jsonld/input_output.feature @@ -309,7 +309,7 @@ Feature: JSON-LD DTO input and output """ Then the response status code should be 400 And the response should be in JSON - And the JSON node "hydra:description" should be equal to "The input data is misformatted." + And the JSON node "description" should be equal to "The input data is misformatted." @!mongodb Scenario: Reset password through an input DTO without DataTransformer diff --git a/features/main/attribute_resource.feature b/features/main/attribute_resource.feature index 90a8bff1663..016e3e99973 100644 --- a/features/main/attribute_resource.feature +++ b/features/main/attribute_resource.feature @@ -100,7 +100,7 @@ Feature: Resource attributes And the response should be in JSON And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8" And the header "Link" should contain '; rel="http://www.w3.org/ns/json-ld#error"' - 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"' + 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"' Scenario: Uri variables with Post operation When I add "Content-Type" header equal to "application/ld+json" diff --git a/features/main/not_exposed.feature b/features/main/not_exposed.feature index aa6cb252798..09f4d8691d9 100644 --- a/features/main/not_exposed.feature +++ b/features/main/not_exposed.feature @@ -171,13 +171,22 @@ Feature: Expose only a collection of objects When I send a "GET" request to "" Then the response status code should be 404 And the response should be in JSON - And the JSON node "hydra:description" should be equal to "" + And the JSON node "hydra:description" should be equal to "" Examples: - | uri | hydra:description | - | /.well-known/genid/12345 | This route is not exposed on purpose. It generates an IRI for a collection resource without identifier nor item operation. | + | uri | description | | /tables/12345 | This route does not aim to be called. | | /forks/12345 | This route does not aim to be called. | + Scenario Outline: Get a not exposed route returns a 404 with an explanation + When I send a "GET" request to "" + Then the response status code should be 404 + And the response should be in JSON + And the JSON node "description" should be equal to "" + Examples: + | uri | description | + | /.well-known/genid/12345 | This route is not exposed on purpose. It generates an IRI for a collection resource without identifier nor item operation. | + + Scenario: Get a single item still works When I send a "GET" request to "/cuillers/12345" Then the response status code should be 200 diff --git a/features/main/relation.feature b/features/main/relation.feature index 07d5050cda6..206a4789ffe 100644 --- a/features/main/relation.feature +++ b/features/main/relation.feature @@ -472,7 +472,7 @@ Feature: Relations support Then the response status code should be 400 And the response should be in JSON And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8" - And the JSON node "hydra:description" should contain 'Invalid IRI "certainly not an IRI".' + And the JSON node "description" should contain 'Invalid IRI "certainly not an IRI".' Scenario: Passing an invalid type to a relation When I add "Content-Type" header equal to "application/ld+json" @@ -493,20 +493,20 @@ Feature: Relations support "properties": { "@type": { "type": "string", - "pattern": "^hydra:Error$" + "pattern": "^Error$" }, - "hydra:title": { + "title": { "type": "string", "pattern": "^An error occurred$" }, - "hydra:description": { + "description": { "pattern": "^The type of the \"ApiPlatform\\\\Tests\\\\Fixtures\\\\TestBundle\\\\(Document|Entity)\\\\RelatedDummy\" resource must be \"array\" \\(nested document\\) or \"string\" \\(IRI\\), \"integer\" given.$" } }, "required": [ "@type", - "hydra:title", - "hydra:description" + "title", + "description" ] } """ diff --git a/features/main/union_intersect_types.feature b/features/main/union_intersect_types.feature index 97e415f60ea..d11edd42002 100644 --- a/features/main/union_intersect_types.feature +++ b/features/main/union_intersect_types.feature @@ -118,4 +118,4 @@ Feature: Union/Intersect types Then the response status code should be 400 And the response should be in JSON And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8" - 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.' + 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.' diff --git a/features/main/validation.feature b/features/main/validation.feature index b8ab1feb8a6..8c6db85dcb1 100644 --- a/features/main/validation.feature +++ b/features/main/validation.feature @@ -87,7 +87,7 @@ Feature: Using validations groups And the JSON node "violations[0].message" should be equal to "This value should not be null." And the JSON node "violations[0].propertyPath" should be equal to "test" And the JSON node "detail" should be equal to "test: This value should not be null." - And the JSON node "hydra:description" should be equal to "test: This value should not be null." + And the JSON node "description" should be equal to "test: This value should not be null." And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8" @!mongodb diff --git a/features/serializer/vo_relations.feature b/features/serializer/vo_relations.feature index 0a3ba73c37a..ccf49439814 100644 --- a/features/serializer/vo_relations.feature +++ b/features/serializer/vo_relations.feature @@ -148,20 +148,20 @@ Feature: Value object as ApiResource "properties": { "@type": { "type": "string", - "pattern": "^hydra:Error$" + "pattern": "^Error$" }, - "hydra:title": { + "title": { "type": "string", "pattern": "^An error occurred$" }, - "hydra:description": { + "description": { "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\".$" } }, "required": [ "@type", - "hydra:title", - "hydra:description" + "title", + "description" ] } """ diff --git a/src/JsonLd/ContextBuilder.php b/src/JsonLd/ContextBuilder.php index 5cb591d0fab..0fe1063636b 100644 --- a/src/JsonLd/ContextBuilder.php +++ b/src/JsonLd/ContextBuilder.php @@ -14,6 +14,7 @@ namespace ApiPlatform\JsonLd; use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait; +use ApiPlatform\Metadata\Error; use ApiPlatform\Metadata\Get; use ApiPlatform\Metadata\HttpOperation; use ApiPlatform\Metadata\IriConverterInterface; @@ -184,7 +185,7 @@ private function getResourceContextWithShortname(string $resourceClass, int $ref } } - if (false === ($this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true)) { + if (false === ($this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true) || $operation instanceof Error) { return ['http://www.w3.org/ns/hydra/context.jsonld', $context]; } diff --git a/src/State/ApiResource/Error.php b/src/State/ApiResource/Error.php index 43f65a25ae9..2ad7ebfa653 100644 --- a/src/State/ApiResource/Error.php +++ b/src/State/ApiResource/Error.php @@ -25,7 +25,6 @@ use Symfony\Component\WebLink\Link; #[ErrorResource( - types: ['hydra:Error'], openapi: false, uriVariables: ['status'], uriTemplate: '/errors/{status}', @@ -98,21 +97,7 @@ public function __construct( #[Groups(['trace'])] public ?array $originalTrace = null; - #[SerializedName('hydra:title')] #[Groups(['jsonld'])] - public function getHydraTitle(): ?string - { - return $this->title; - } - - #[SerializedName('hydra:description')] - #[Groups(['jsonld'])] - public function getHydraDescription(): ?string - { - return $this->detail; - } - - #[SerializedName('description')] public function getDescription(): ?string { return $this->detail; diff --git a/src/Validator/Exception/ValidationException.php b/src/Validator/Exception/ValidationException.php index 26f4c2f1fb4..98117463ff1 100644 --- a/src/Validator/Exception/ValidationException.php +++ b/src/Validator/Exception/ValidationException.php @@ -108,16 +108,8 @@ public function getId(): string return $id; } - #[SerializedName('hydra:title')] #[Groups(['jsonld'])] - public function getHydraTitle(): string - { - return $this->errorTitle ?? 'An error occurred'; - } - - #[Groups(['jsonld'])] - #[SerializedName('hydra:description')] - public function getHydraDescription(): string + public function getDescription(): string { return $this->detail; }