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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 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\JsonSchema\SchemaFactory;
17
use ApiPlatform\Metadata\ApiProperty;
18
use ApiPlatform\Metadata\Error as ErrorOperation;
19
use ApiPlatform\Metadata\ErrorResource;
20
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
21
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
22
use ApiPlatform\Metadata\Exception\RuntimeException;
23
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
24
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
25
use Symfony\Component\Serializer\Annotation\Groups;
26
use Symfony\Component\Serializer\Annotation\SerializedName;
27
use Symfony\Component\Validator\ConstraintViolationList;
28
use Symfony\Component\Validator\ConstraintViolationListInterface;
29
use Symfony\Component\WebLink\Link;
30

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

107
    public function __construct(string|ConstraintViolationListInterface $message = new ConstraintViolationList(), string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
108
    {
109
        $this->errorTitle = $errorTitle;
90✔
110

111
        if ($message instanceof ConstraintViolationListInterface) {
90✔
112
            $this->constraintViolationList = $message;
90✔
113
            parent::__construct($this->__toString(), $code ?? 0, $previous);
90✔
114

115
            return;
90✔
116
        }
117

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

122
    public function getId(): string
123
    {
124
        $ids = [];
66✔
125
        foreach ($this->getConstraintViolationList() as $violation) {
66✔
126
            $ids[] = $violation->getCode();
66✔
127
        }
128

129
        $id = 1 < \count($ids) ? CompositeIdentifierParser::stringify(identifiers: $ids) : ($ids[0] ?? null);
66✔
130

131
        if (!$id) {
66✔
132
            return spl_object_hash($this);
2✔
133
        }
134

135
        return $id;
64✔
136
    }
137

138
    #[Groups(['jsonld'])]
139
    #[ApiProperty(writable: false, initializable: false)]
140
    public function getDescription(): string
141
    {
142
        return $this->detail;
66✔
143
    }
144

145
    #[Groups(['jsonld', 'json', 'jsonapi'])]
146
    #[ApiProperty(writable: false, initializable: false)]
147
    public function getType(): string
148
    {
149
        return '/validation_errors/'.$this->getId();
66✔
150
    }
151

152
    #[Groups(['jsonld', 'json', 'jsonapi'])]
153
    #[ApiProperty(writable: false, initializable: false)]
154
    public function getTitle(): ?string
155
    {
156
        return $this->errorTitle ?? 'An error occurred';
88✔
157
    }
158

159
    #[Groups(['jsonld', 'json', 'jsonapi'])]
160
    #[ApiProperty(writable: false, initializable: false)]
161
    private string $detail;
162

163
    public function getDetail(): ?string
164
    {
165
        return $this->detail;
66✔
166
    }
167

168
    public function setDetail(string $detail): void
169
    {
170
        $this->detail = $detail;
66✔
171
    }
172

173
    #[Groups(['jsonld', 'json', 'jsonapi'])]
174
    public function getStatus(): ?int
175
    {
176
        return $this->status;
88✔
177
    }
178

179
    public function setStatus(int $status): void
180
    {
181
        $this->status = $status;
66✔
182
    }
183

184
    #[Groups(['jsonld', 'json', 'jsonapi'])]
185
    #[ApiProperty(writable: false, initializable: false)]
186
    public function getInstance(): ?string
187
    {
188
        return null;
66✔
189
    }
190

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

211
    public function __toString(): string
212
    {
213
        $message = '';
90✔
214
        foreach ($this->getConstraintViolationList() as $violation) {
90✔
215
            if ('' !== $message) {
68✔
216
                $message .= "\n";
16✔
217
            }
218
            if ($propertyPath = $violation->getPropertyPath()) {
68✔
219
                $message .= "$propertyPath: ";
68✔
220
            }
221

222
            $message .= $violation->getMessage();
68✔
223
        }
224

225
        return $message;
90✔
226
    }
227

228
    public function getStatusCode(): int
229
    {
230
        return $this->status;
66✔
231
    }
232

233
    public function getHeaders(): array
234
    {
235
        return [];
66✔
236
    }
237
}
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