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

api-platform / core / 13724522058

07 Mar 2025 04:04PM UTC coverage: 8.175% (-0.3%) from 8.518%
13724522058

Pull #7005

github

web-flow
Merge 322407532 into 1e0bc9dc8
Pull Request #7005: fix(validation): deprecate string message for ValidationException con…

4 of 6 new or added lines in 1 file covered. (66.67%)

159 existing lines in 24 files now uncovered.

12839 of 157045 relevant lines covered (8.18%)

13.55 hits per line

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

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

85
    public function __construct(string|ConstraintViolationListInterface $message = new ConstraintViolationList(), string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
86
    {
87
        $this->errorTitle = $errorTitle;
75✔
88

89
        if ($message instanceof ConstraintViolationListInterface) {
75✔
90
            $this->constraintViolationList = $message;
75✔
91
            parent::__construct($this->__toString(), $code ?? 0, $previous);
75✔
92

93
            return;
75✔
94
        }
95

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

100
    /**
101
     * @deprecated
102
     */
103
    public function getErrorTitle(): ?string
104
    {
105
        return $this->errorTitle;
×
106
    }
107

108
    public function getId(): string
109
    {
110
        $ids = [];
40✔
111
        foreach ($this->getConstraintViolationList() as $violation) {
40✔
112
            $ids[] = $violation->getCode();
40✔
113
        }
114

115
        $id = 1 < \count($ids) ? CompositeIdentifierParser::stringify(identifiers: $ids) : ($ids[0] ?? null);
40✔
116

117
        if (!$id) {
40✔
UNCOV
118
            return spl_object_hash($this);
×
119
        }
120

121
        return $id;
40✔
122
    }
123

124
    #[Groups(['jsonld'])]
125
    #[ApiProperty(writable: false, initializable: false)]
126
    public function getDescription(): string
127
    {
128
        return $this->detail;
28✔
129
    }
130

131
    #[Groups(['jsonld', 'json', 'jsonapi'])]
132
    #[ApiProperty(writable: false, initializable: false)]
133
    public function getType(): string
134
    {
135
        return '/validation_errors/'.$this->getId();
40✔
136
    }
137

138
    #[Groups(['jsonld', 'json', 'jsonapi'])]
139
    #[ApiProperty(writable: false, initializable: false)]
140
    public function getTitle(): ?string
141
    {
142
        return $this->errorTitle ?? 'An error occurred';
72✔
143
    }
144

145
    #[Groups(['jsonld', 'json', 'jsonapi'])]
146
    #[ApiProperty(writable: false, initializable: false)]
147
    private string $detail;
148

149
    public function getDetail(): ?string
150
    {
151
        return $this->detail;
40✔
152
    }
153

154
    public function setDetail(string $detail): void
155
    {
156
        $this->detail = $detail;
40✔
157
    }
158

159
    #[Groups(['jsonld', 'json', 'jsonapi'])]
160
    public function getStatus(): ?int
161
    {
162
        return $this->status;
72✔
163
    }
164

165
    public function setStatus(int $status): void
166
    {
167
        $this->status = $status;
40✔
168
    }
169

170
    #[Groups(['jsonld', 'json', 'jsonapi'])]
171
    #[ApiProperty(writable: false, initializable: false)]
172
    public function getInstance(): ?string
173
    {
174
        return null;
40✔
175
    }
176

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

197
    public function __toString(): string
198
    {
199
        $message = '';
75✔
200
        foreach ($this->getConstraintViolationList() as $violation) {
75✔
201
            if ('' !== $message) {
43✔
202
                $message .= "\n";
8✔
203
            }
204
            if ($propertyPath = $violation->getPropertyPath()) {
43✔
205
                $message .= "$propertyPath: ";
43✔
206
            }
207

208
            $message .= $violation->getMessage();
43✔
209
        }
210

211
        return $message;
75✔
212
    }
213

214
    public function getStatusCode(): int
215
    {
216
        return $this->status;
40✔
217
    }
218

219
    public function getHeaders(): array
220
    {
221
        return [];
40✔
222
    }
223
}
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