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

api-platform / core / 13175753759

06 Feb 2025 09:30AM UTC coverage: 0.0% (-7.7%) from 7.663%
13175753759

Pull #6947

github

web-flow
Merge 432a515ad into de2d298e3
Pull Request #6947: fix(metadata): remove temporary fix for ArrayCollection

0 of 1 new or added line in 1 file covered. (0.0%)

11655 existing lines in 385 files now uncovered.

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

28
/**
29
 * Thrown when a validation error occurs.
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
#[ErrorResource(
UNCOV
34
    uriTemplate: '/validation_errors/{id}',
×
UNCOV
35
    status: 422,
×
UNCOV
36
    openapi: false,
×
UNCOV
37
    uriVariables: ['id'],
×
UNCOV
38
    provider: 'api_platform.validator.state.error_provider',
×
UNCOV
39
    shortName: 'ConstraintViolationList',
×
UNCOV
40
    operations: [
×
UNCOV
41
        new ErrorOperation(
×
UNCOV
42
            name: '_api_validation_errors_problem',
×
UNCOV
43
            outputFormats: ['json' => ['application/problem+json']],
×
UNCOV
44
            normalizationContext: ['groups' => ['json'],
×
UNCOV
45
                'skip_null_values' => true,
×
UNCOV
46
            ]),
×
UNCOV
47
        new ErrorOperation(
×
UNCOV
48
            name: '_api_validation_errors_hydra',
×
UNCOV
49
            outputFormats: ['jsonld' => ['application/problem+json']],
×
UNCOV
50
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
×
UNCOV
51
            normalizationContext: [
×
UNCOV
52
                'groups' => ['jsonld'],
×
UNCOV
53
                'skip_null_values' => true,
×
UNCOV
54
            ]
×
UNCOV
55
        ),
×
UNCOV
56
        new ErrorOperation(
×
UNCOV
57
            name: '_api_validation_errors_jsonapi',
×
UNCOV
58
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
×
UNCOV
59
            normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true]
×
UNCOV
60
        ),
×
UNCOV
61
    ],
×
UNCOV
62
    graphQlOperations: []
×
UNCOV
63
)]
×
64
class ValidationException extends RuntimeException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface, HttpExceptionInterface, SymfonyHttpExceptionInterface
65
{
66
    private int $status = 422;
67
    protected ?string $errorTitle = null;
68
    private ConstraintViolationListInterface $constraintViolationList;
69

70
    public function __construct(string|ConstraintViolationListInterface $message = '', string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
71
    {
UNCOV
72
        $this->errorTitle = $errorTitle;
×
73

UNCOV
74
        if ($message instanceof ConstraintViolationListInterface) {
×
UNCOV
75
            $this->constraintViolationList = $message;
×
UNCOV
76
            parent::__construct($this->__toString(), $code ?? 0, $previous);
×
77

UNCOV
78
            return;
×
79
        }
80

81
        trigger_deprecation('api_platform/core', '3.3', \sprintf('The "%s" exception will have a "%s" first argument in 4.x.', self::class, ConstraintViolationListInterface::class));
×
82
        parent::__construct($message ?: $this->__toString(), $code ?? 0, $previous);
×
83
    }
84

85
    /**
86
     * @deprecated
87
     */
88
    public function getErrorTitle(): ?string
89
    {
90
        return $this->errorTitle;
×
91
    }
92

93
    public function getId(): string
94
    {
UNCOV
95
        $ids = [];
×
UNCOV
96
        foreach ($this->getConstraintViolationList() as $violation) {
×
UNCOV
97
            $ids[] = $violation->getCode();
×
98
        }
99

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

UNCOV
102
        if (!$id) {
×
103
            return spl_object_hash($this);
×
104
        }
105

UNCOV
106
        return $id;
×
107
    }
108

109
    #[Groups(['jsonld'])]
110
    public function getDescription(): string
111
    {
UNCOV
112
        return $this->detail;
×
113
    }
114

115
    #[Groups(['jsonld', 'json', 'jsonapi'])]
116
    public function getType(): string
117
    {
UNCOV
118
        return '/validation_errors/'.$this->getId();
×
119
    }
120

121
    #[Groups(['jsonld', 'json', 'jsonapi'])]
122
    public function getTitle(): ?string
123
    {
UNCOV
124
        return $this->errorTitle ?? 'An error occurred';
×
125
    }
126

127
    #[Groups(['jsonld', 'json', 'jsonapi'])]
128
    private string $detail;
129

130
    public function getDetail(): ?string
131
    {
UNCOV
132
        return $this->detail;
×
133
    }
134

135
    public function setDetail(string $detail): void
136
    {
UNCOV
137
        $this->detail = $detail;
×
138
    }
139

140
    #[Groups(['jsonld', 'json', 'jsonapi'])]
141
    public function getStatus(): ?int
142
    {
UNCOV
143
        return $this->status;
×
144
    }
145

146
    public function setStatus(int $status): void
147
    {
UNCOV
148
        $this->status = $status;
×
149
    }
150

151
    #[Groups(['jsonld', 'json', 'jsonapi'])]
152
    public function getInstance(): ?string
153
    {
UNCOV
154
        return null;
×
155
    }
156

157
    #[SerializedName('violations')]
158
    #[Groups(['json', 'jsonld'])]
159
    public function getConstraintViolationList(): ConstraintViolationListInterface
160
    {
UNCOV
161
        return $this->constraintViolationList;
×
162
    }
163

164
    public function __toString(): string
165
    {
UNCOV
166
        $message = '';
×
UNCOV
167
        foreach ($this->getConstraintViolationList() as $violation) {
×
UNCOV
168
            if ('' !== $message) {
×
UNCOV
169
                $message .= "\n";
×
170
            }
UNCOV
171
            if ($propertyPath = $violation->getPropertyPath()) {
×
UNCOV
172
                $message .= "$propertyPath: ";
×
173
            }
174

UNCOV
175
            $message .= $violation->getMessage();
×
176
        }
177

UNCOV
178
        return $message;
×
179
    }
180

181
    public function getStatusCode(): int
182
    {
UNCOV
183
        return $this->status;
×
184
    }
185

186
    public function getHeaders(): array
187
    {
UNCOV
188
        return [];
×
189
    }
190
}
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