• 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

94.64
/src/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.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\Metadata\Property;
15

16
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
17
use ApiPlatform\Metadata\ApiProperty;
18
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface;
20
use Symfony\Component\Validator\Constraint;
21
use Symfony\Component\Validator\Constraints\Bic;
22
use Symfony\Component\Validator\Constraints\CardScheme;
23
use Symfony\Component\Validator\Constraints\Compound;
24
use Symfony\Component\Validator\Constraints\Currency;
25
use Symfony\Component\Validator\Constraints\Date;
26
use Symfony\Component\Validator\Constraints\DateTime;
27
use Symfony\Component\Validator\Constraints\Email;
28
use Symfony\Component\Validator\Constraints\File;
29
use Symfony\Component\Validator\Constraints\Iban;
30
use Symfony\Component\Validator\Constraints\Image;
31
use Symfony\Component\Validator\Constraints\Isbn;
32
use Symfony\Component\Validator\Constraints\Issn;
33
use Symfony\Component\Validator\Constraints\NotBlank;
34
use Symfony\Component\Validator\Constraints\NotNull;
35
use Symfony\Component\Validator\Constraints\Sequentially;
36
use Symfony\Component\Validator\Constraints\Time;
37
use Symfony\Component\Validator\Constraints\Url;
38
use Symfony\Component\Validator\Constraints\Uuid;
39
use Symfony\Component\Validator\Mapping\ClassMetadataInterface as ValidatorClassMetadataInterface;
40
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface as ValidatorMetadataFactoryInterface;
41
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface as ValidatorPropertyMetadataInterface;
42

43
/**
44
 * Decorates a metadata loader using the validator.
45
 *
46
 * @author Kévin Dunglas <dunglas@gmail.com>
47
 */
48
final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryInterface
49
{
50
    /**
51
     * @var string[] A list of constraint classes making the entity required
52
     */
53
    public const REQUIRED_CONSTRAINTS = [NotBlank::class, NotNull::class];
54

55
    public const SCHEMA_MAPPED_CONSTRAINTS = [
56
        Url::class => 'https://schema.org/url',
57
        Email::class => 'https://schema.org/email',
58
        Uuid::class => 'https://schema.org/identifier',
59
        CardScheme::class => 'https://schema.org/identifier',
60
        Bic::class => 'https://schema.org/identifier',
61
        Iban::class => 'https://schema.org/identifier',
62
        Date::class => 'https://schema.org/Date',
63
        DateTime::class => 'https://schema.org/DateTime',
64
        Time::class => 'https://schema.org/Time',
65
        Image::class => 'https://schema.org/image',
66
        File::class => 'https://schema.org/MediaObject',
67
        Currency::class => 'https://schema.org/priceCurrency',
68
        Isbn::class => 'https://schema.org/isbn',
69
        Issn::class => 'https://schema.org/issn',
70
    ];
71

72
    /**
73
     * @param PropertySchemaRestrictionMetadataInterface[] $restrictionsMetadata
74
     */
75
    public function __construct(private readonly ValidatorMetadataFactoryInterface $validatorMetadataFactory, private readonly PropertyMetadataFactoryInterface $decorated, private readonly iterable $restrictionsMetadata = [])
76
    {
UNCOV
77
    }
1,026✔
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
83
    {
UNCOV
84
        $propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
287✔
85

UNCOV
86
        $extraProperties = $propertyMetadata->getExtraProperties() ?? [];
287✔
87
        // see AttributePropertyMetadataFactory
UNCOV
88
        if (true === ($extraProperties[SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED] ?? false)) {
287✔
89
            // schema seems to have been declared by the user: do not override nor complete user value
UNCOV
90
            return $propertyMetadata;
15✔
91
        }
92

UNCOV
93
        $required = $propertyMetadata->isRequired();
287✔
UNCOV
94
        $types = $propertyMetadata->getTypes();
287✔
UNCOV
95
        $schema = $propertyMetadata->getSchema();
287✔
96

UNCOV
97
        if (null !== $required && $types && $schema) {
287✔
98
            return $propertyMetadata;
×
99
        }
100

UNCOV
101
        $validatorClassMetadata = $this->validatorMetadataFactory->getMetadataFor($resourceClass);
287✔
102

UNCOV
103
        if (!$validatorClassMetadata instanceof ValidatorClassMetadataInterface) {
287✔
104
            throw new \UnexpectedValueException(\sprintf('Validator class metadata expected to be of type "%s".', ValidatorClassMetadataInterface::class));
×
105
        }
106

UNCOV
107
        $validationGroups = $this->getValidationGroups($validatorClassMetadata, $options);
287✔
UNCOV
108
        $restrictions = [];
287✔
UNCOV
109
        $types ??= [];
287✔
110

UNCOV
111
        foreach ($validatorClassMetadata->getPropertyMetadata($property) as $validatorPropertyMetadata) {
287✔
UNCOV
112
            foreach ($this->getPropertyConstraints($validatorPropertyMetadata, $validationGroups) as $constraint) {
90✔
UNCOV
113
                if (null === $required && $this->isRequired($constraint)) {
87✔
UNCOV
114
                    $required = true;
38✔
115
                }
116

UNCOV
117
                $type = self::SCHEMA_MAPPED_CONSTRAINTS[$constraint::class] ?? null;
87✔
118

UNCOV
119
                if ($type && !\in_array($type, $types, true)) {
87✔
UNCOV
120
                    $types[] = $type;
23✔
121
                }
122

UNCOV
123
                foreach ($this->restrictionsMetadata as $restrictionMetadata) {
87✔
UNCOV
124
                    if ($restrictionMetadata->supports($constraint, $propertyMetadata)) {
82✔
UNCOV
125
                        $restrictions[] = $restrictionMetadata->create($constraint, $propertyMetadata);
43✔
126
                    }
127
                }
128
            }
129
        }
130

UNCOV
131
        if ($types) {
287✔
UNCOV
132
            $propertyMetadata = $propertyMetadata->withTypes($types);
35✔
133
        }
134

UNCOV
135
        $propertyMetadata = $propertyMetadata->withRequired($required ?? false);
287✔
136

UNCOV
137
        if (!empty($restrictions)) {
287✔
UNCOV
138
            if (null === $schema) {
43✔
UNCOV
139
                $schema = [];
43✔
140
            }
141

UNCOV
142
            $schema += array_merge(...$restrictions);
43✔
UNCOV
143
            $propertyMetadata = $propertyMetadata->withSchema($schema);
43✔
144
        }
145

UNCOV
146
        return $propertyMetadata;
287✔
147
    }
148

149
    /**
150
     * Returns the list of validation groups.
151
     */
152
    private function getValidationGroups(ValidatorClassMetadataInterface $classMetadata, array $options): array
153
    {
154
        if (
UNCOV
155
            isset($options['validation_groups'])
287✔
UNCOV
156
            && !\is_callable($options['validation_groups'])
287✔
157
        ) {
UNCOV
158
            return $options['validation_groups'];
3✔
159
        }
160

UNCOV
161
        if (!method_exists($classMetadata, 'getDefaultGroup')) {
284✔
162
            throw new \UnexpectedValueException(\sprintf('Validator class metadata expected to have method "%s".', 'getDefaultGroup'));
×
163
        }
164

UNCOV
165
        return [$classMetadata->getDefaultGroup()];
284✔
166
    }
167

168
    /**
169
     * Tests if the property is required because of its validation groups.
170
     */
171
    private function getPropertyConstraints(
172
        ValidatorPropertyMetadataInterface $validatorPropertyMetadata,
173
        array $groups,
174
    ): array {
UNCOV
175
        $constraints = [];
90✔
176

UNCOV
177
        foreach ($groups as $validationGroup) {
90✔
UNCOV
178
            if (!\is_string($validationGroup)) {
90✔
UNCOV
179
                continue;
1✔
180
            }
181

UNCOV
182
            foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $propertyConstraint) {
89✔
UNCOV
183
                if ($propertyConstraint instanceof Sequentially || $propertyConstraint instanceof Compound) {
87✔
UNCOV
184
                    $constraints[] = $propertyConstraint->getNestedConstraints();
2✔
185
                } else {
UNCOV
186
                    $constraints[] = [$propertyConstraint];
85✔
187
                }
188
            }
189
        }
190

UNCOV
191
        return array_merge([], ...$constraints);
90✔
192
    }
193

194
    /**
195
     * Is this constraint making the related property required?
196
     */
197
    private function isRequired(Constraint $constraint): bool
198
    {
UNCOV
199
        if ($constraint instanceof NotBlank && $constraint->allowNull) {
86✔
UNCOV
200
            return false;
2✔
201
        }
202

UNCOV
203
        foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) {
86✔
UNCOV
204
            if ($constraint instanceof $requiredConstraint) {
86✔
UNCOV
205
                return true;
38✔
206
            }
207
        }
208

UNCOV
209
        return false;
58✔
210
    }
211
}
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