Skip to content

Commit 8884bd9

Browse files
authored
Merge pull request #6600 from soyuka/merge-34
Merge 3.4
2 parents a50e2d0 + 3a0f9e5 commit 8884bd9

File tree

21 files changed

+549
-169
lines changed

21 files changed

+549
-169
lines changed

features/hydra/error.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,20 @@ Feature: Error handling
121121
And the JSON node "title" should be equal to "An error occurred"
122122
And the JSON node "detail" should exist
123123
And the JSON node "violations" should exist
124+
125+
Scenario: Get an rfc 7807 error
126+
When I add "Content-Type" header equal to "application/ld+json"
127+
And I send a "POST" request to "/exception_problems_without_prefix" with body:
128+
"""
129+
{}
130+
"""
131+
Then the response status code should be 400
132+
And the response should be in JSON
133+
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
134+
And the header "Link" should contain '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error"'
135+
And the JSON node "type" should exist
136+
And the JSON node "title" should be equal to "An error occurred"
137+
And the JSON node "detail" should exist
138+
And the JSON node "description" should exist
139+
And the JSON node "trace" should exist
140+
And the JSON node "status" should exist
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Hydra\Serializer;
15+
16+
use ApiPlatform\JsonLd\ContextBuilder;
17+
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
18+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
19+
20+
final class HydraPrefixNameConverter implements NameConverterInterface, AdvancedNameConverterInterface
21+
{
22+
public function __construct(private readonly NameConverterInterface $nameConverter)
23+
{
24+
}
25+
26+
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
27+
{
28+
$name = $this->nameConverter->normalize($propertyName, $class, $format, $context);
29+
30+
if (true === ($context[ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX] ?? true)) {
31+
return $name;
32+
}
33+
34+
return str_starts_with($name, ContextBuilder::HYDRA_PREFIX) ? str_replace(ContextBuilder::HYDRA_PREFIX, '', $name) : $name;
35+
}
36+
37+
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
38+
{
39+
return $this->nameConverter->denormalize($propertyName, $class, $format, $context);
40+
}
41+
}

src/Metadata/Delete.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
?array $exceptionToStatus = null,
4949
?bool $queryParameterValidationEnabled = null,
5050
?array $links = null,
51+
?array $errors = null,
5152

5253
?string $shortName = null,
5354
?string $class = null,
@@ -128,6 +129,7 @@ public function __construct(
128129
exceptionToStatus: $exceptionToStatus,
129130
queryParameterValidationEnabled: $queryParameterValidationEnabled,
130131
links: $links,
132+
errors: $errors,
131133
shortName: $shortName,
132134
class: $class,
133135
paginationEnabled: $paginationEnabled,

src/Metadata/Error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
?array $exceptionToStatus = null,
4949
?bool $queryParameterValidationEnabled = null,
5050
?array $links = null,
51+
?array $errors = null,
5152

5253
?string $shortName = null,
5354
?string $class = null,
@@ -123,6 +124,7 @@ public function __construct(
123124
exceptionToStatus: $exceptionToStatus,
124125
queryParameterValidationEnabled: $queryParameterValidationEnabled,
125126
links: $links,
127+
errors: $errors,
126128
shortName: $shortName,
127129
class: $class,
128130
paginationEnabled: $paginationEnabled,

src/Metadata/Get.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
?array $exceptionToStatus = null,
4949
?bool $queryParameterValidationEnabled = null,
5050
?array $links = null,
51+
?array $errors = null,
5152

5253
?string $shortName = null,
5354
?string $class = null,
@@ -127,6 +128,7 @@ public function __construct(
127128
exceptionToStatus: $exceptionToStatus,
128129
queryParameterValidationEnabled: $queryParameterValidationEnabled,
129130
links: $links,
131+
errors: $errors,
130132
shortName: $shortName,
131133
class: $class,
132134
paginationEnabled: $paginationEnabled,

src/Metadata/GetCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
?array $exceptionToStatus = null,
4949
?bool $queryParameterValidationEnabled = null,
5050
?array $links = null,
51+
?array $errors = null,
5152

5253
?string $shortName = null,
5354
?string $class = null,
@@ -128,6 +129,7 @@ public function __construct(
128129
exceptionToStatus: $exceptionToStatus,
129130
queryParameterValidationEnabled: $queryParameterValidationEnabled,
130131
links: $links,
132+
errors: $errors,
131133
shortName: $shortName,
132134
class: $class,
133135
paginationEnabled: $paginationEnabled,

src/Metadata/HttpOperation.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Metadata;
1515

16+
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
1617
use ApiPlatform\OpenApi\Attributes\Webhook;
1718
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
1819
use ApiPlatform\State\OptionsInterface;
@@ -72,11 +73,12 @@ class HttpOperation extends Operation
7273
* class?: string|null,
7374
* name?: string,
7475
* }|string|false|null $output {@see https://api-platform.com/docs/core/dto/#specifying-an-input-or-an-output-data-representation}
75-
* @param string|array|bool|null $mercure {@see https://api-platform.com/docs/core/mercure}
76-
* @param string|bool|null $messenger {@see https://api-platform.com/docs/core/messenger/#dispatching-a-resource-through-the-message-bus}
77-
* @param string|callable|null $provider {@see https://api-platform.com/docs/core/state-providers/#state-providers}
78-
* @param string|callable|null $processor {@see https://api-platform.com/docs/core/state-processors/#state-processors}
79-
* @param WebLink[]|null $links
76+
* @param string|array|bool|null $mercure {@see https://api-platform.com/docs/core/mercure}
77+
* @param string|bool|null $messenger {@see https://api-platform.com/docs/core/messenger/#dispatching-a-resource-through-the-message-bus}
78+
* @param string|callable|null $provider {@see https://api-platform.com/docs/core/state-providers/#state-providers}
79+
* @param string|callable|null $processor {@see https://api-platform.com/docs/core/state-processors/#state-processors}
80+
* @param WebLink[]|null $links
81+
* @param array<class-string<ProblemExceptionInterface>>|null $errors
8082
*/
8183
public function __construct(
8284
protected string $method = 'GET',
@@ -152,6 +154,7 @@ public function __construct(
152154
protected bool|OpenApiOperation|Webhook|null $openapi = null,
153155
protected ?array $exceptionToStatus = null,
154156
protected ?array $links = null,
157+
protected ?array $errors = null,
155158

156159
?string $shortName = null,
157160
?string $class = null,
@@ -614,4 +617,20 @@ public function withLinks(array $links): self
614617

615618
return $self;
616619
}
620+
621+
public function getErrors(): ?array
622+
{
623+
return $this->errors;
624+
}
625+
626+
/**
627+
* @param class-string<ProblemExceptionInterface>[] $errors
628+
*/
629+
public function withErrors(array $errors): self
630+
{
631+
$self = clone $this;
632+
$self->errors = $errors;
633+
634+
return $self;
635+
}
617636
}

src/Metadata/NotExposed.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function __construct(
6161

6262
?bool $queryParameterValidationEnabled = null,
6363
?array $links = null,
64+
?array $errors = null,
6465

6566
?string $shortName = null,
6667
?string $class = null,
@@ -137,6 +138,7 @@ public function __construct(
137138
exceptionToStatus: $exceptionToStatus,
138139
queryParameterValidationEnabled: $queryParameterValidationEnabled,
139140
links: $links,
141+
errors: $errors,
140142
shortName: $shortName,
141143
class: $class,
142144
paginationEnabled: $paginationEnabled,

src/Metadata/Patch.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
?array $exceptionToStatus = null,
4949
?bool $queryParameterValidationEnabled = null,
5050
?array $links = null,
51+
?array $errors = null,
5152

5253
?string $shortName = null,
5354
?string $class = null,
@@ -128,6 +129,7 @@ public function __construct(
128129
exceptionToStatus: $exceptionToStatus,
129130
queryParameterValidationEnabled: $queryParameterValidationEnabled,
130131
links: $links,
132+
errors: $errors,
131133
shortName: $shortName,
132134
class: $class,
133135
paginationEnabled: $paginationEnabled,

src/Metadata/Post.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
?array $exceptionToStatus = null,
4949
?bool $queryParameterValidationEnabled = null,
5050
?array $links = null,
51+
?array $errors = null,
5152

5253
?string $shortName = null,
5354
?string $class = null,
@@ -129,6 +130,7 @@ public function __construct(
129130
exceptionToStatus: $exceptionToStatus,
130131
queryParameterValidationEnabled: $queryParameterValidationEnabled,
131132
links: $links,
133+
errors: $errors,
132134
shortName: $shortName,
133135
class: $class,
134136
paginationEnabled: $paginationEnabled,

0 commit comments

Comments
 (0)