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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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(
UNCOV
37
    uriTemplate: '/validation_errors/{id}',
×
UNCOV
38
    status: 422,
×
UNCOV
39
    uriVariables: ['id'],
×
UNCOV
40
    openapi: false,
×
UNCOV
41
    outputFormats: ['jsonapi' => ['application/vnd.api+json'], 'jsonld' => ['application/ld+json'], 'json' => ['application/problem+json', 'application/json']],
×
UNCOV
42
    provider: 'api_platform.validator.state.error_provider',
×
UNCOV
43
    shortName: 'ConstraintViolation',
×
UNCOV
44
    description: 'Unprocessable entity',
×
UNCOV
45
    operations: [
×
UNCOV
46
        new ErrorOperation(
×
UNCOV
47
            name: '_api_validation_errors_problem',
×
UNCOV
48
            outputFormats: ['json' => ['application/problem+json']],
×
UNCOV
49
            normalizationContext: [
×
UNCOV
50
                SchemaFactory::OPENAPI_DEFINITION_NAME => '',
×
UNCOV
51
                'groups' => ['json'],
×
UNCOV
52
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
UNCOV
53
                'skip_null_values' => true,
×
UNCOV
54
            ]
×
UNCOV
55
        ),
×
UNCOV
56
        new ErrorOperation(
×
UNCOV
57
            name: '_api_validation_errors_hydra',
×
UNCOV
58
            outputFormats: ['jsonld' => ['application/problem+json', 'application/ld+json']],
×
UNCOV
59
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
×
UNCOV
60
            normalizationContext: [
×
UNCOV
61
                SchemaFactory::OPENAPI_DEFINITION_NAME => '',
×
UNCOV
62
                'groups' => ['jsonld'],
×
UNCOV
63
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
UNCOV
64
                'skip_null_values' => true,
×
UNCOV
65
            ]
×
UNCOV
66
        ),
×
UNCOV
67
        new ErrorOperation(
×
UNCOV
68
            name: '_api_validation_errors_jsonapi',
×
UNCOV
69
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
×
UNCOV
70
            normalizationContext: [
×
UNCOV
71
                SchemaFactory::OPENAPI_DEFINITION_NAME => '',
×
UNCOV
72
                'disable_json_schema_serializer_groups' => false,
×
UNCOV
73
                'groups' => ['jsonapi'],
×
UNCOV
74
                'skip_null_values' => true,
×
UNCOV
75
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
UNCOV
76
            ]
×
UNCOV
77
        ),
×
UNCOV
78
    ],
×
UNCOV
79
    graphQlOperations: []
×
UNCOV
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
    {
UNCOV
91
        $this->errorTitle = $errorTitle;
×
92

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

UNCOV
97
            return;
×
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
    /**
105
     * @deprecated
106
     */
107
    public function getErrorTitle(): ?string
108
    {
109
        return $this->errorTitle;
×
110
    }
111

112
    public function getId(): string
113
    {
UNCOV
114
        $ids = [];
×
UNCOV
115
        foreach ($this->getConstraintViolationList() as $violation) {
×
UNCOV
116
            $ids[] = $violation->getCode();
×
117
        }
118

UNCOV
119
        $id = 1 < \count($ids) ? CompositeIdentifierParser::stringify(identifiers: $ids) : ($ids[0] ?? null);
×
120

UNCOV
121
        if (!$id) {
×
122
            return spl_object_hash($this);
×
123
        }
124

UNCOV
125
        return $id;
×
126
    }
127

128
    #[Groups(['jsonld'])]
129
    #[ApiProperty(writable: false, initializable: false)]
130
    public function getDescription(): string
131
    {
UNCOV
132
        return $this->detail;
×
133
    }
134

135
    #[Groups(['jsonld', 'json', 'jsonapi'])]
136
    #[ApiProperty(writable: false, initializable: false)]
137
    public function getType(): string
138
    {
UNCOV
139
        return '/validation_errors/'.$this->getId();
×
140
    }
141

142
    #[Groups(['jsonld', 'json', 'jsonapi'])]
143
    #[ApiProperty(writable: false, initializable: false)]
144
    public function getTitle(): ?string
145
    {
UNCOV
146
        return $this->errorTitle ?? 'An error occurred';
×
147
    }
148

149
    #[Groups(['jsonld', 'json', 'jsonapi'])]
150
    #[ApiProperty(writable: false, initializable: false)]
151
    private string $detail;
152

153
    public function getDetail(): ?string
154
    {
UNCOV
155
        return $this->detail;
×
156
    }
157

158
    public function setDetail(string $detail): void
159
    {
UNCOV
160
        $this->detail = $detail;
×
161
    }
162

163
    #[Groups(['jsonld', 'json', 'jsonapi'])]
164
    public function getStatus(): ?int
165
    {
UNCOV
166
        return $this->status;
×
167
    }
168

169
    public function setStatus(int $status): void
170
    {
UNCOV
171
        $this->status = $status;
×
172
    }
173

174
    #[Groups(['jsonld', 'json', 'jsonapi'])]
175
    #[ApiProperty(writable: false, initializable: false)]
176
    public function getInstance(): ?string
177
    {
UNCOV
178
        return null;
×
179
    }
180

181
    #[SerializedName('violations')]
182
    #[Groups(['json', 'jsonld'])]
183
    #[ApiProperty(
UNCOV
184
        jsonldContext: ['@type' => 'ConstraintViolationList'],
×
UNCOV
185
        schema: [
×
UNCOV
186
            'type' => 'array',
×
UNCOV
187
            'items' => [
×
UNCOV
188
                'type' => 'object',
×
UNCOV
189
                'properties' => [
×
UNCOV
190
                    'propertyPath' => ['type' => 'string', 'description' => 'The property path of the violation'],
×
UNCOV
191
                    'message' => ['type' => 'string', 'description' => 'The message associated with the violation'],
×
UNCOV
192
                ],
×
UNCOV
193
            ],
×
UNCOV
194
        ]
×
UNCOV
195
    )]
×
196
    public function getConstraintViolationList(): ConstraintViolationListInterface
197
    {
UNCOV
198
        return $this->constraintViolationList;
×
199
    }
200

201
    public function __toString(): string
202
    {
UNCOV
203
        $message = '';
×
UNCOV
204
        foreach ($this->getConstraintViolationList() as $violation) {
×
UNCOV
205
            if ('' !== $message) {
×
UNCOV
206
                $message .= "\n";
×
207
            }
UNCOV
208
            if ($propertyPath = $violation->getPropertyPath()) {
×
UNCOV
209
                $message .= "$propertyPath: ";
×
210
            }
211

UNCOV
212
            $message .= $violation->getMessage();
×
213
        }
214

UNCOV
215
        return $message;
×
216
    }
217

218
    public function getStatusCode(): int
219
    {
UNCOV
220
        return $this->status;
×
221
    }
222

223
    public function getHeaders(): array
224
    {
UNCOV
225
        return [];
×
226
    }
227
}
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