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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

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

29.41
/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\Metadata\Error as ErrorOperation;
17
use ApiPlatform\Metadata\ErrorResource;
18
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
19
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
20
use ApiPlatform\Validator\Exception\ValidationException as BaseValidationException;
21
use Symfony\Component\Serializer\Annotation\Groups;
22
use Symfony\Component\Serializer\Annotation\SerializedName;
23
use Symfony\Component\Validator\ConstraintViolationListInterface;
24

25
/**
26
 * Thrown when a validation error occurs.
27
 *
28
 * @author Kévin Dunglas <dunglas@gmail.com>
29
 */
30
#[ErrorResource(
31
    uriTemplate: '/validation_errors/{id}',
32
    status: 422,
33
    openapi: false,
34
    uriVariables: ['id'],
35
    shortName: 'ConstraintViolationList',
36
    operations: [
37
        new ErrorOperation(name: '_api_validation_errors_hydra', outputFormats: ['jsonld' => ['application/ld+json']], normalizationContext: ['groups' => ['jsonld'], 'skip_null_values' => true]),
38
        new ErrorOperation(name: '_api_validation_errors_problem', outputFormats: ['jsonproblem' => ['application/problem+json'], 'json' => ['application/problem+json']], normalizationContext: ['groups' => ['json'], 'skip_null_values' => true]),
39
        new ErrorOperation(name: '_api_validation_errors_jsonapi', outputFormats: ['jsonapi' => ['application/vnd.api+json']], normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true]),
40
    ]
41
)]
42
final class ValidationException extends BaseValidationException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface
43
{
44
    public function __construct(private readonly ConstraintViolationListInterface $constraintViolationList, string $message = '', int $code = 0, \Throwable $previous = null, string $errorTitle = null)
45
    {
46
        parent::__construct($message ?: $this->__toString(), $code, $previous, $errorTitle);
15✔
47
    }
48

49
    public function getConstraintViolationList(): ConstraintViolationListInterface
50
    {
51
        return $this->constraintViolationList;
9✔
52
    }
53

54
    public function getId(): string
55
    {
56
        $ids = [];
×
57
        foreach ($this->getConstraintViolationList() as $violation) {
×
58
            $ids[] = $violation->getCode();
×
59
        }
60

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

63
        if (!$id) {
×
64
            return spl_object_hash($this);
×
65
        }
66

67
        return $id;
×
68
    }
69

70
    public function __toString(): string
71
    {
72
        $message = '';
15✔
73
        foreach ($this->constraintViolationList as $violation) {
15✔
74
            if ('' !== $message) {
6✔
75
                $message .= "\n";
3✔
76
            }
77
            if ($propertyPath = $violation->getPropertyPath()) {
6✔
78
                $message .= "$propertyPath: ";
6✔
79
            }
80

81
            $message .= $violation->getMessage();
6✔
82
        }
83

84
        return $message;
15✔
85
    }
86

87
    #[SerializedName('hydra:title')]
88
    #[Groups(['jsonld', 'legacy_jsonld'])]
89
    public function getHydraTitle(): string
90
    {
91
        return $this->errorTitle ?? 'An error occurred';
×
92
    }
93

94
    #[SerializedName('hydra:description')]
95
    #[Groups(['jsonld', 'legacy_jsonld'])]
96
    public function getHydraDescription(): string
97
    {
98
        return $this->__toString();
×
99
    }
100

101
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
102
    public function getType(): string
103
    {
104
        return '/validation_errors/'.$this->getId();
×
105
    }
106

107
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
108
    public function getTitle(): ?string
109
    {
110
        return $this->errorTitle ?? 'An error occurred';
×
111
    }
112

113
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
114
    public function getDetail(): ?string
115
    {
116
        return $this->__toString();
×
117
    }
118

119
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
120
    public function getStatus(): ?int
121
    {
122
        return 422;
×
123
    }
124

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

131
    #[SerializedName('violations')]
132
    #[Groups(['json', 'jsonld', 'legacy_jsonld', 'legacy_jsonproblem', 'legacy_json'])]
133
    public function getViolations(): iterable
134
    {
135
        foreach ($this->getConstraintViolationList() as $violation) {
×
136
            $propertyPath = $violation->getPropertyPath();
×
137
            $violationData = [
×
138
                'propertyPath' => $propertyPath,
×
139
                'message' => $violation->getMessage(),
×
140
                'code' => $violation->getCode(),
×
141
            ];
×
142

143
            if ($hint = $violation->getParameters()['hint'] ?? false) {
×
144
                $violationData['hint'] = $hint;
×
145
            }
146

147
            yield $violationData;
×
148
        }
149
    }
150
}
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