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

api-platform / core / 6496109206

12 Oct 2023 01:09PM UTC coverage: 37.285% (-0.1%) from 37.405%
6496109206

push

github

soyuka
Merge 3.2

35 of 35 new or added lines in 3 files covered. (100.0%)

10240 of 27464 relevant lines covered (37.29%)

20.03 hits per line

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

38.46
/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\Serializer\Annotation\Groups;
23
use Symfony\Component\Serializer\Annotation\SerializedName;
24
use Symfony\Component\Validator\ConstraintViolationListInterface;
25
use Symfony\Component\WebLink\Link;
26

27
/**
28
 * Thrown when a validation error occurs.
29
 *
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 */
32
#[ErrorResource(
33
    uriTemplate: '/validation_errors/{id}',
34
    status: 422,
35
    openapi: false,
36
    uriVariables: ['id'],
37
    shortName: 'ConstraintViolationList',
38
    operations: [
39
        new ErrorOperation(name: '_api_validation_errors_problem', outputFormats: ['json' => ['application/problem+json']], normalizationContext: ['groups' => ['json'], 'skip_null_values' => true]),
40
        new ErrorOperation(
41
            name: '_api_validation_errors_hydra',
42
            outputFormats: ['jsonld' => ['application/problem+json']],
43
            links: [new Link(rel: ContextBuilderInterface::JSONLD_NS.'error', href: 'http://www.w3.org/ns/hydra/error')],
44
            normalizationContext: [
45
                'groups' => ['jsonld'],
46
                'skip_null_values' => true,
47
            ]
48
        ),
49
        new ErrorOperation(name: '_api_validation_errors_jsonapi', outputFormats: ['jsonapi' => ['application/vnd.api+json']], normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true]),
50
    ],
51
    graphQlOperations: []
52
)]
53
final class ValidationException extends BaseValidationException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface
54
{
55
    private int $status = 422;
56

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

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

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

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

75
        return $id;
×
76
    }
77

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

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

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

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

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

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

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

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

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

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

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

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

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

155
        return $message;
15✔
156
    }
157
}
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