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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 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: ['jsonapi' => ['application/vnd.api+json'], 'jsonld' => ['application/ld+json'], 'json' => ['application/problem+json', 'application/json']],
42
    provider: 'api_platform.validator.state.error_provider',
43
    shortName: 'ConstraintViolation',
44
    description: 'Unprocessable entity',
45
    operations: [
46
        new ErrorOperation(
47
            name: '_api_validation_errors_problem',
48
            outputFormats: ['json' => ['application/problem+json']],
49
            normalizationContext: [
50
                SchemaFactory::OPENAPI_DEFINITION_NAME => '',
51
                'groups' => ['json'],
52
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
53
                'skip_null_values' => true,
54
            ]
55
        ),
56
        new ErrorOperation(
57
            name: '_api_validation_errors_hydra',
58
            outputFormats: ['jsonld' => ['application/problem+json', 'application/ld+json']],
59
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
60
            normalizationContext: [
61
                SchemaFactory::OPENAPI_DEFINITION_NAME => '',
62
                'groups' => ['jsonld'],
63
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
64
                'skip_null_values' => true,
65
            ]
66
        ),
67
        new ErrorOperation(
68
            name: '_api_validation_errors_jsonapi',
69
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
70
            normalizationContext: [
71
                SchemaFactory::OPENAPI_DEFINITION_NAME => '',
72
                'disable_json_schema_serializer_groups' => false,
73
                'groups' => ['jsonapi'],
74
                'skip_null_values' => true,
75
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
76
            ]
77
        ),
78
    ],
79
    graphQlOperations: []
80
)]
81
#[ApiProperty(property: 'traceAsString', hydra: false)]
82
#[ApiProperty(property: 'string', hydra: false)]
83
class ValidationException extends RuntimeException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface, HttpExceptionInterface, SymfonyHttpExceptionInterface
84
{
85
    private int $status = 422;
86
    protected ?string $errorTitle = null;
87
    private array|ConstraintViolationListInterface $constraintViolationList = [];
88

89
    public function __construct(string|ConstraintViolationListInterface $message = new ConstraintViolationList(), string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
90
    {
91
        $this->errorTitle = $errorTitle;
66✔
92

93
        if ($message instanceof ConstraintViolationListInterface) {
66✔
94
            $this->constraintViolationList = $message;
66✔
95
            parent::__construct($this->__toString(), $code ?? 0, $previous);
66✔
96

97
            return;
66✔
98
        }
99

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

104
    public function getId(): string
105
    {
106
        $ids = [];
42✔
107
        foreach ($this->getConstraintViolationList() as $violation) {
42✔
108
            $ids[] = $violation->getCode();
42✔
109
        }
110

111
        $id = 1 < \count($ids) ? CompositeIdentifierParser::stringify(identifiers: $ids) : ($ids[0] ?? null);
42✔
112

113
        if (!$id) {
42✔
114
            return spl_object_hash($this);
2✔
115
        }
116

117
        return $id;
40✔
118
    }
119

120
    #[Groups(['jsonld'])]
121
    #[ApiProperty(writable: false, initializable: false)]
122
    public function getDescription(): string
123
    {
124
        return $this->detail;
42✔
125
    }
126

127
    #[Groups(['jsonld', 'json', 'jsonapi'])]
128
    #[ApiProperty(writable: false, initializable: false)]
129
    public function getType(): string
130
    {
131
        return '/validation_errors/'.$this->getId();
42✔
132
    }
133

134
    #[Groups(['jsonld', 'json', 'jsonapi'])]
135
    #[ApiProperty(writable: false, initializable: false)]
136
    public function getTitle(): ?string
137
    {
138
        return $this->errorTitle ?? 'An error occurred';
64✔
139
    }
140

141
    #[Groups(['jsonld', 'json', 'jsonapi'])]
142
    #[ApiProperty(writable: false, initializable: false)]
143
    private string $detail;
144

145
    public function getDetail(): ?string
146
    {
147
        return $this->detail;
42✔
148
    }
149

150
    public function setDetail(string $detail): void
151
    {
152
        $this->detail = $detail;
42✔
153
    }
154

155
    #[Groups(['jsonld', 'json', 'jsonapi'])]
156
    public function getStatus(): ?int
157
    {
158
        return $this->status;
64✔
159
    }
160

161
    public function setStatus(int $status): void
162
    {
163
        $this->status = $status;
42✔
164
    }
165

166
    #[Groups(['jsonld', 'json', 'jsonapi'])]
167
    #[ApiProperty(writable: false, initializable: false)]
168
    public function getInstance(): ?string
169
    {
170
        return null;
42✔
171
    }
172

173
    #[SerializedName('violations')]
174
    #[Groups(['json', 'jsonld'])]
175
    #[ApiProperty(
176
        jsonldContext: ['@type' => 'ConstraintViolationList'],
177
        schema: [
178
            'type' => 'array',
179
            'items' => [
180
                'type' => 'object',
181
                'properties' => [
182
                    'propertyPath' => ['type' => 'string', 'description' => 'The property path of the violation'],
183
                    'message' => ['type' => 'string', 'description' => 'The message associated with the violation'],
184
                ],
185
            ],
186
        ]
187
    )]
188
    public function getConstraintViolationList(): ConstraintViolationListInterface
189
    {
190
        return $this->constraintViolationList;
66✔
191
    }
192

193
    public function __toString(): string
194
    {
195
        $message = '';
66✔
196
        foreach ($this->getConstraintViolationList() as $violation) {
66✔
197
            if ('' !== $message) {
44✔
198
                $message .= "\n";
8✔
199
            }
200
            if ($propertyPath = $violation->getPropertyPath()) {
44✔
201
                $message .= "$propertyPath: ";
44✔
202
            }
203

204
            $message .= $violation->getMessage();
44✔
205
        }
206

207
        return $message;
66✔
208
    }
209

210
    public function getStatusCode(): int
211
    {
212
        return $this->status;
42✔
213
    }
214

215
    public function getHeaders(): array
216
    {
217
        return [];
42✔
218
    }
219
}
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