• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 16369805213

18 Jul 2025 11:49AM UTC coverage: 22.609% (+0.01%) from 22.598%
16369805213

push

github

soyuka
docs: changelog 4.1.19

11145 of 49294 relevant lines covered (22.61%)

23.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.94
/src/Validator/Exception/ValidationException.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Validator\Exception;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\Error as ErrorOperation;
18
use ApiPlatform\Metadata\ErrorResource;
19
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
20
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
21
use ApiPlatform\Metadata\Exception\RuntimeException;
22
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
23
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
24
use Symfony\Component\Serializer\Annotation\Groups;
25
use Symfony\Component\Serializer\Annotation\SerializedName;
26
use Symfony\Component\Validator\ConstraintViolationList;
27
use Symfony\Component\Validator\ConstraintViolationListInterface;
28
use Symfony\Component\WebLink\Link;
29

30
/**
31
 * Thrown when a validation error occurs.
32
 *
33
 * @author Kévin Dunglas <dunglas@gmail.com>
34
 */
35
#[ErrorResource(
36
    uriTemplate: '/validation_errors/{id}',
37
    status: 422,
38
    uriVariables: ['id'],
39
    openapi: false,
40
    outputFormats: [
41
        'jsonapi' => ['application/vnd.api+json'],
42
        'jsonld' => ['application/ld+json'],
43
        'json' => ['application/problem+json', 'application/json'],
44
        'xml' => ['application/xml', 'text/xml'],
45
    ],
46
    provider: 'api_platform.validator.state.error_provider',
47
    shortName: 'ConstraintViolation',
48
    description: 'Unprocessable entity',
49
    operations: [
50
        new ErrorOperation(
51
            name: '_api_validation_errors_problem',
52
            outputFormats: [
53
                'json' => ['application/problem+json'],
54
            ],
55
            normalizationContext: [
56
                'groups' => ['json'],
57
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
58
                'skip_null_values' => true,
59
            ]
60
        ),
61
        new ErrorOperation(
62
            name: '_api_validation_errors_hydra',
63
            outputFormats: ['jsonld' => ['application/problem+json', 'application/ld+json']],
64
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
65
            normalizationContext: [
66
                'groups' => ['jsonld'],
67
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
68
                'skip_null_values' => true,
69
            ]
70
        ),
71
        new ErrorOperation(
72
            name: '_api_validation_errors_jsonapi',
73
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
74
            normalizationContext: [
75
                'disable_json_schema_serializer_groups' => false,
76
                'groups' => ['jsonapi'],
77
                'skip_null_values' => true,
78
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
79
            ]
80
        ),
81
        new ErrorOperation(
82
            name: '_api_validation_errors_xml',
83
            outputFormats: [
84
                'xml' => ['application/xml', 'text/xml'],
85
            ],
86
            normalizationContext: [
87
                'groups' => ['json'],
88
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
89
                'skip_null_values' => true,
90
            ]
91
        ),
92
    ],
93
    graphQlOperations: []
94
)]
95
#[ApiProperty(property: 'traceAsString', hydra: false)]
96
#[ApiProperty(property: 'string', hydra: false)]
97
class ValidationException extends RuntimeException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface, HttpExceptionInterface, SymfonyHttpExceptionInterface
98
{
99
    private int $status = 422;
100
    protected ?string $errorTitle = null;
101
    private array|ConstraintViolationListInterface $constraintViolationList = [];
102

103
    public function __construct(string|ConstraintViolationListInterface $message = new ConstraintViolationList(), string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
104
    {
105
        $this->errorTitle = $errorTitle;
86✔
106

107
        if ($message instanceof ConstraintViolationListInterface) {
86✔
108
            $this->constraintViolationList = $message;
86✔
109
            parent::__construct($this->__toString(), $code ?? 0, $previous);
86✔
110

111
            return;
86✔
112
        }
113

114
        trigger_deprecation('api_platform/core', '5.0', \sprintf('The "%s" exception will have a "%s" first argument in 5.x.', self::class, ConstraintViolationListInterface::class));
×
115
        parent::__construct($message ?: $this->__toString(), $code ?? 0, $previous);
×
116
    }
117

118
    public function getId(): string
119
    {
120
        $ids = [];
64✔
121
        foreach ($this->getConstraintViolationList() as $violation) {
64✔
122
            $ids[] = $violation->getCode();
64✔
123
        }
124

125
        $id = 1 < \count($ids) ? CompositeIdentifierParser::stringify(identifiers: $ids) : ($ids[0] ?? null);
64✔
126

127
        if (!$id) {
64✔
128
            return spl_object_hash($this);
2✔
129
        }
130

131
        return $id;
62✔
132
    }
133

134
    #[Groups(['jsonld'])]
135
    #[ApiProperty(writable: false, initializable: false)]
136
    public function getDescription(): string
137
    {
138
        return $this->detail;
64✔
139
    }
140

141
    #[Groups(['jsonld', 'json', 'jsonapi'])]
142
    #[ApiProperty(writable: false, initializable: false)]
143
    public function getType(): string
144
    {
145
        return '/validation_errors/'.$this->getId();
64✔
146
    }
147

148
    #[Groups(['jsonld', 'json', 'jsonapi'])]
149
    #[ApiProperty(writable: false, initializable: false)]
150
    public function getTitle(): ?string
151
    {
152
        return $this->errorTitle ?? 'An error occurred';
84✔
153
    }
154

155
    #[Groups(['jsonld', 'json', 'jsonapi'])]
156
    #[ApiProperty(writable: false, initializable: false)]
157
    private string $detail;
158

159
    public function getDetail(): ?string
160
    {
161
        return $this->detail;
64✔
162
    }
163

164
    public function setDetail(string $detail): void
165
    {
166
        $this->detail = $detail;
64✔
167
    }
168

169
    #[Groups(['jsonld', 'json', 'jsonapi'])]
170
    public function getStatus(): ?int
171
    {
172
        return $this->status;
84✔
173
    }
174

175
    public function setStatus(int $status): void
176
    {
177
        $this->status = $status;
64✔
178
    }
179

180
    #[Groups(['jsonld', 'json', 'jsonapi'])]
181
    #[ApiProperty(writable: false, initializable: false)]
182
    public function getInstance(): ?string
183
    {
184
        return null;
64✔
185
    }
186

187
    #[SerializedName('violations')]
188
    #[Groups(['json', 'jsonld'])]
189
    #[ApiProperty(
190
        jsonldContext: ['@type' => 'ConstraintViolationList'],
191
        schema: [
192
            'type' => 'array',
193
            'items' => [
194
                'type' => 'object',
195
                'properties' => [
196
                    'propertyPath' => ['type' => 'string', 'description' => 'The property path of the violation'],
197
                    'message' => ['type' => 'string', 'description' => 'The message associated with the violation'],
198
                ],
199
            ],
200
        ]
201
    )]
202
    public function getConstraintViolationList(): ConstraintViolationListInterface
203
    {
204
        return $this->constraintViolationList;
86✔
205
    }
206

207
    public function __toString(): string
208
    {
209
        $message = '';
86✔
210
        foreach ($this->getConstraintViolationList() as $violation) {
86✔
211
            if ('' !== $message) {
66✔
212
                $message .= "\n";
14✔
213
            }
214
            if ($propertyPath = $violation->getPropertyPath()) {
66✔
215
                $message .= "$propertyPath: ";
66✔
216
            }
217

218
            $message .= $violation->getMessage();
66✔
219
        }
220

221
        return $message;
86✔
222
    }
223

224
    public function getStatusCode(): int
225
    {
226
        return $this->status;
64✔
227
    }
228

229
    public function getHeaders(): array
230
    {
231
        return [];
64✔
232
    }
233
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc