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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

96.43
/src/Serializer/AbstractConstraintViolationListNormalizer.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\Serializer;
15

16
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
17
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
18
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Validator\ConstraintViolation;
21
use Symfony\Component\Validator\ConstraintViolationListInterface;
22

23
/**
24
 * Common features regarding Constraint Violation normalization.
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 *
28
 * @internal
29
 */
30
abstract class AbstractConstraintViolationListNormalizer implements NormalizerInterface
31
{
32
    public const FORMAT = null; // Must be overridden
33

34
    private readonly ?array $serializePayloadFields;
35

36
    public function __construct(?array $serializePayloadFields = null, private readonly ?NameConverterInterface $nameConverter = null)
37
    {
UNCOV
38
        $this->serializePayloadFields = null === $serializePayloadFields ? null : array_flip($serializePayloadFields);
954✔
39
    }
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
45
    {
UNCOV
46
        if (!($context['api_error_resource'] ?? false)) {
35✔
UNCOV
47
            return false;
1✔
48
        }
49

UNCOV
50
        return static::FORMAT === $format && $data instanceof ConstraintViolationListInterface;
35✔
51
    }
52

53
    public function getSupportedTypes($format): array
54
    {
UNCOV
55
        return $format === static::FORMAT ? [ConstraintViolationListInterface::class => true] : [];
891✔
56
    }
57

58
    protected function getMessagesAndViolations(ConstraintViolationListInterface $constraintViolationList): array
59
    {
UNCOV
60
        $violations = $messages = [];
37✔
61

UNCOV
62
        foreach ($constraintViolationList as $violation) {
37✔
UNCOV
63
            $class = \is_object($root = $violation->getRoot()) ? $root::class : null;
37✔
64

UNCOV
65
            if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
37✔
UNCOV
66
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT);
35✔
UNCOV
67
            } elseif ($this->nameConverter instanceof NameConverterInterface) {
2✔
UNCOV
68
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath());
1✔
69
            } else {
UNCOV
70
                $propertyPath = $violation->getPropertyPath();
1✔
71
            }
72

UNCOV
73
            $violationData = [
37✔
UNCOV
74
                'propertyPath' => $propertyPath,
37✔
UNCOV
75
                'message' => $violation->getMessage(),
37✔
UNCOV
76
                'code' => $violation->getCode(),
37✔
UNCOV
77
            ];
37✔
78

UNCOV
79
            if ($hint = $violation->getParameters()['hint'] ?? false) {
37✔
80
                $violationData['hint'] = $hint;
×
81
            }
82

UNCOV
83
            $constraint = $violation instanceof ConstraintViolation ? $violation->getConstraint() : null;
37✔
84
            if (
UNCOV
85
                [] !== $this->serializePayloadFields
37✔
86
                && $constraint
UNCOV
87
                && $constraint->payload
37✔
88
                // If some fields are whitelisted, only them are added
UNCOV
89
                && $payloadFields = null === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, $this->serializePayloadFields)
37✔
90
            ) {
UNCOV
91
                $violationData['payload'] = $payloadFields;
3✔
92
            }
93

UNCOV
94
            $violations[] = $violationData;
37✔
UNCOV
95
            $messages[] = ($violationData['propertyPath'] ? "{$violationData['propertyPath']}: " : '').$violationData['message'];
37✔
96
        }
97

UNCOV
98
        return [$messages, $violations];
37✔
99
    }
100
}
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