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

api-platform / core / 6774876659

06 Nov 2023 06:18PM UTC coverage: 37.409%. Remained the same
6774876659

push

github

soyuka
Merge 3.2

10 of 15 new or added lines in 7 files covered. (66.67%)

1 existing line in 1 file now uncovered.

10322 of 27592 relevant lines covered (37.41%)

20.6 hits per line

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

35.71
/src/Symfony/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\Symfony\Validator\Exception;
15

16
use ApiPlatform\JsonLd\ContextBuilderInterface;
17
use ApiPlatform\Metadata\Error as ErrorOperation;
18
use ApiPlatform\Metadata\ErrorResource;
19
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
20
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
21
use ApiPlatform\Validator\Exception\ValidationException as BaseValidationException;
22
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
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(
34
    uriTemplate: '/validation_errors/{id}',
35
    status: 422,
36
    openapi: false,
37
    uriVariables: ['id'],
38
    shortName: 'ConstraintViolationList',
39
    operations: [
40
        new ErrorOperation(name: '_api_validation_errors_problem', outputFormats: ['json' => ['application/problem+json']], normalizationContext: ['groups' => ['json'], 'skip_null_values' => true]),
41
        new ErrorOperation(
42
            name: '_api_validation_errors_hydra',
43
            outputFormats: ['jsonld' => ['application/problem+json']],
44
            links: [new Link(rel: ContextBuilderInterface::JSONLD_NS.'error', href: 'http://www.w3.org/ns/hydra/error')],
45
            normalizationContext: [
46
                'groups' => ['jsonld'],
47
                'skip_null_values' => true,
48
            ]
49
        ),
50
        new ErrorOperation(name: '_api_validation_errors_jsonapi', outputFormats: ['jsonapi' => ['application/vnd.api+json']], normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true]),
51
    ],
52
    graphQlOperations: []
53
)]
54
final class ValidationException extends BaseValidationException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface, HttpExceptionInterface
55
{
56
    private int $status = 422;
57

58
    public function __construct(private readonly ConstraintViolationListInterface $constraintViolationList, string $message = '', int $code = 0, \Throwable $previous = null, string $errorTitle = null)
59
    {
60
        parent::__construct($message ?: $this->__toString(), $code, $previous, $errorTitle);
15✔
61
    }
62

63
    public function getId(): string
64
    {
65
        $ids = [];
×
66
        foreach ($this->getConstraintViolationList() as $violation) {
×
67
            $ids[] = $violation->getCode();
×
68
        }
69

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

72
        if (!$id) {
×
73
            return spl_object_hash($this);
×
74
        }
75

76
        return $id;
×
77
    }
78

79
    #[SerializedName('hydra:title')]
80
    #[Groups(['jsonld', 'legacy_jsonld'])]
81
    public function getHydraTitle(): string
82
    {
83
        return $this->errorTitle ?? 'An error occurred';
×
84
    }
85

86
    #[Groups(['jsonld', 'legacy_jsonld'])]
87
    #[SerializedName('hydra:description')]
88
    public function getHydraDescription(): string
89
    {
90
        return $this->detail;
×
91
    }
92

93
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
94
    public function getType(): string
95
    {
96
        return '/validation_errors/'.$this->getId();
×
97
    }
98

99
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
100
    public function getTitle(): ?string
101
    {
102
        return $this->errorTitle ?? 'An error occurred';
×
103
    }
104

105
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
106
    private string $detail;
107

108
    public function getDetail(): ?string
109
    {
110
        return $this->detail;
×
111
    }
112

113
    public function setDetail(string $detail): void
114
    {
115
        $this->detail = $detail;
×
116
    }
117

118
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
119
    public function getStatus(): ?int
120
    {
121
        return $this->status;
×
122
    }
123

124
    public function setStatus(int $status): void
125
    {
126
        $this->status = $status;
×
127
    }
128

129
    #[Groups(['jsonld', 'json'])]
130
    public function getInstance(): ?string
131
    {
132
        return null;
×
133
    }
134

135
    #[SerializedName('violations')]
136
    #[Groups(['json', 'jsonld', 'legacy_jsonld', 'legacy_jsonproblem', 'legacy_json'])]
137
    public function getConstraintViolationList(): ConstraintViolationListInterface
138
    {
139
        return $this->constraintViolationList;
9✔
140
    }
141

142
    public function __toString(): string
143
    {
144
        $message = '';
15✔
145
        foreach ($this->constraintViolationList as $violation) {
15✔
146
            if ('' !== $message) {
6✔
147
                $message .= "\n";
3✔
148
            }
149
            if ($propertyPath = $violation->getPropertyPath()) {
6✔
150
                $message .= "$propertyPath: ";
6✔
151
            }
152

153
            $message .= $violation->getMessage();
6✔
154
        }
155

156
        return $message;
15✔
157
    }
158

159
    public function getStatusCode(): int
160
    {
NEW
161
        return $this->status;
×
162
    }
163

164
    public function getHeaders(): array
165
    {
NEW
166
        return [];
×
167
    }
168
}
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

© 2026 Coveralls, Inc