Skip to content

Commit afc3844

Browse files
authored
feat(openapi): document error outputs using json-schemas (#6923)
1 parent 0db0b20 commit afc3844

File tree

1 file changed

+61
-10
lines changed

1 file changed

+61
-10
lines changed

ApiResource/Error.php

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use Symfony\Component\WebLink\Link;
2626

2727
#[ErrorResource(
28-
openapi: false,
2928
uriVariables: ['status'],
3029
uriTemplate: '/errors/{status}',
3130
operations: [
@@ -37,15 +36,17 @@
3736
normalizationContext: [
3837
'groups' => ['jsonproblem'],
3938
'skip_null_values' => true,
39+
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
4040
],
4141
),
4242
new Operation(
4343
name: '_api_errors_hydra',
4444
routeName: 'api_errors',
45-
outputFormats: ['jsonld' => ['application/problem+json']],
45+
outputFormats: ['jsonld' => ['application/problem+json', 'application/ld+json']],
4646
normalizationContext: [
4747
'groups' => ['jsonld'],
4848
'skip_null_values' => true,
49+
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
4950
],
5051
links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
5152
),
@@ -55,32 +56,46 @@
5556
hideHydraOperation: true,
5657
outputFormats: ['jsonapi' => ['application/vnd.api+json']],
5758
normalizationContext: [
59+
'disable_json_schema_serializer_groups' => false,
5860
'groups' => ['jsonapi'],
5961
'skip_null_values' => true,
62+
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
6063
],
6164
),
6265
new Operation(
6366
name: '_api_errors',
6467
routeName: 'api_errors',
6568
hideHydraOperation: true,
69+
openapi: false
6670
),
6771
],
6872
provider: 'api_platform.state.error_provider',
69-
graphQlOperations: []
73+
graphQlOperations: [],
74+
description: 'A representation of common errors.'
7075
)]
7176
#[ApiProperty(property: 'traceAsString', hydra: false)]
7277
#[ApiProperty(property: 'string', hydra: false)]
7378
class Error extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface
7479
{
80+
private ?string $id = null;
81+
7582
public function __construct(
7683
private string $title,
7784
private string $detail,
78-
#[ApiProperty(identifier: true, writable: false, initializable: false)] private int $status,
85+
#[ApiProperty(
86+
description: 'The HTTP status code applicable to this problem.',
87+
identifier: true,
88+
writable: false,
89+
initializable: false,
90+
schema: ['type' => 'number', 'example' => 404, 'default' => 400]
91+
)] private int $status,
7992
?array $originalTrace = null,
8093
private ?string $instance = null,
8194
private string $type = 'about:blank',
8295
private array $headers = [],
8396
?\Throwable $previous = null,
97+
private ?array $meta = null,
98+
private ?array $source = null,
8499
) {
85100
parent::__construct($title, $status, $previous);
86101

@@ -98,7 +113,28 @@ public function __construct(
98113
#[Groups(['jsonapi'])]
99114
public function getId(): string
100115
{
101-
return (string) $this->status;
116+
return $this->id ?? ((string) $this->status);
117+
}
118+
119+
#[Groups(['jsonapi'])]
120+
#[ApiProperty(schema: ['type' => 'object'])]
121+
public function getMeta(): ?array
122+
{
123+
return $this->meta;
124+
}
125+
126+
#[Groups(['jsonapi'])]
127+
#[ApiProperty(schema: [
128+
'type' => 'object',
129+
'properties' => [
130+
'pointer' => ['type' => 'string'],
131+
'parameter' => ['type' => 'string'],
132+
'header' => ['type' => 'string'],
133+
],
134+
])]
135+
public function getSource(): ?array
136+
{
137+
return $this->source;
102138
}
103139

104140
#[SerializedName('trace')]
@@ -142,7 +178,7 @@ public function setHeaders(array $headers): void
142178
}
143179

144180
#[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
145-
#[ApiProperty(writable: false, initializable: false)]
181+
#[ApiProperty(writable: false, initializable: false, description: 'A URI reference that identifies the problem type')]
146182
public function getType(): string
147183
{
148184
return $this->type;
@@ -154,7 +190,7 @@ public function setType(string $type): void
154190
}
155191

156192
#[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
157-
#[ApiProperty(writable: false, initializable: false)]
193+
#[ApiProperty(writable: false, initializable: false, description: 'A short, human-readable summary of the problem.')]
158194
public function getTitle(): ?string
159195
{
160196
return $this->title;
@@ -177,7 +213,7 @@ public function setStatus(int $status): void
177213
}
178214

179215
#[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
180-
#[ApiProperty(writable: false, initializable: false)]
216+
#[ApiProperty(writable: false, initializable: false, description: 'A human-readable explanation specific to this occurrence of the problem.')]
181217
public function getDetail(): ?string
182218
{
183219
return $this->detail;
@@ -188,8 +224,8 @@ public function setDetail(?string $detail = null): void
188224
$this->detail = $detail;
189225
}
190226

191-
#[Groups(['jsonld', 'jsonproblem'])]
192-
#[ApiProperty(writable: false, initializable: false)]
227+
#[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
228+
#[ApiProperty(writable: false, initializable: false, description: 'A URI reference that identifies the specific occurrence of the problem. It may or may not yield further information if dereferenced.')]
193229
public function getInstance(): ?string
194230
{
195231
return $this->instance;
@@ -199,4 +235,19 @@ public function setInstance(?string $instance = null): void
199235
{
200236
$this->instance = $instance;
201237
}
238+
239+
public function setId(?string $id = null): void
240+
{
241+
$this->id = $id;
242+
}
243+
244+
public function setMeta(?array $meta = null): void
245+
{
246+
$this->meta = $meta;
247+
}
248+
249+
public function setSource(?array $source = null): void
250+
{
251+
$this->source = $source;
252+
}
202253
}

0 commit comments

Comments
 (0)