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

api-platform / core / 14925420478

09 May 2025 09:03AM UTC coverage: 7.739% (+0.6%) from 7.179%
14925420478

Pull #7134

github

web-flow
Merge a0984c209 into 4ddce1f53
Pull Request #7134: refactor(metadata): type parameters to list<string>|string

48 of 134 new or added lines in 15 files covered. (35.82%)

2 existing lines in 2 files now uncovered.

12267 of 158510 relevant lines covered (7.74%)

15.54 hits per line

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

0.0
/tests/Fixtures/TestBundle/ApiResource/WithParameter.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\Tests\Fixtures\TestBundle\ApiResource;
15

16
use ApiPlatform\Metadata\Get;
17
use ApiPlatform\Metadata\GetCollection;
18
use ApiPlatform\Metadata\HeaderParameter;
19
use ApiPlatform\Metadata\Link;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\Metadata\Parameter;
22
use ApiPlatform\Metadata\Parameters;
23
use ApiPlatform\Metadata\QueryParameter;
24
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
25
use ApiPlatform\Serializer\Filter\GroupFilter;
26
use ApiPlatform\Tests\Fixtures\TestBundle\Parameter\CustomGroupParameterProvider;
27
use Symfony\Component\HttpFoundation\JsonResponse;
28
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
29
use Symfony\Component\Serializer\Attribute\Groups;
30
use Symfony\Component\TypeInfo\Type\BuiltinType;
31
use Symfony\Component\TypeInfo\TypeIdentifier;
32
use Symfony\Component\Validator\Constraints as Assert;
33

34
#[Get(
35
    uriTemplate: 'with_parameters/{id}{._format}',
×
36
    uriVariables: [
×
37
        'id' => new Link(schema: ['type' => 'string', 'format' => 'uuid'], property: 'id'),
×
38
    ],
×
39
    parameters: [
×
40
        'groups' => new QueryParameter(filter: new GroupFilter(parameterName: 'groups', overrideDefaultGroups: false)),
×
41
        'group' => new QueryParameter(provider: [self::class, 'provideGroup']),
×
42
        'properties' => new QueryParameter(filter: 'my_dummy.property'),
×
43
        'service' => new QueryParameter(provider: CustomGroupParameterProvider::class),
×
44
        'object' => new QueryParameter(provider: new CustomGroupParameterProvider()),
×
45
        'auth' => new HeaderParameter(provider: [self::class, 'restrictAccess']),
×
46
        'priority' => new QueryParameter(provider: [self::class, 'assertSecond'], priority: 10),
×
47
        'priorityb' => new QueryParameter(provider: [self::class, 'assertFirst'], priority: 20),
×
48
        'array' => new QueryParameter(provider: [self::class, 'assertArray'], openApi: false),
×
49
    ],
×
50
    provider: [self::class, 'provide']
×
51
)]
×
52
#[GetCollection(
53
    uriTemplate: 'with_parameters_collection{._format}',
×
54
    parameters: [
×
55
        'hydra' => new QueryParameter(property: 'a', required: true),
×
56
    ],
×
57
    provider: [self::class, 'collectionProvider']
×
58
)]
×
59
#[GetCollection(
60
    uriTemplate: 'validate_parameters{._format}',
×
61
    parameters: [
×
62
        'enum' => new QueryParameter(schema: ['enum' => ['a', 'b'], 'uniqueItems' => true]),
×
NEW
63
        'num' => new QueryParameter(
×
NEW
64
            schema: ['minimum' => 1, 'maximum' => 3],
×
NEW
65
            nativeType: new BuiltinType(TypeIdentifier::STRING),
×
NEW
66
        ),
×
NEW
67
        'exclusiveNum' => new QueryParameter(
×
NEW
68
            schema: ['exclusiveMinimum' => 1, 'exclusiveMaximum' => 3],
×
NEW
69
            nativeType: new BuiltinType(TypeIdentifier::STRING),
×
NEW
70
        ),
×
NEW
71
        'blank' => new QueryParameter(
×
NEW
72
            openApi: new OpenApiParameter(name: 'blank', in: 'query', allowEmptyValue: false),
×
NEW
73
            nativeType: new BuiltinType(TypeIdentifier::STRING),
×
NEW
74
        ),
×
NEW
75
        'length' => new QueryParameter(
×
NEW
76
            schema: ['maxLength' => 1, 'minLength' => 3],
×
NEW
77
            nativeType: new BuiltinType(TypeIdentifier::STRING),
×
NEW
78
        ),
×
79
        'array' => new QueryParameter(schema: ['minItems' => 2, 'maxItems' => 3]),
×
NEW
80
        'multipleOf' => new QueryParameter(
×
NEW
81
            schema: ['multipleOf' => 2],
×
NEW
82
            nativeType: new BuiltinType(TypeIdentifier::STRING),
×
NEW
83
        ),
×
NEW
84
        'int' => new QueryParameter(
×
NEW
85
            property: 'a',
×
NEW
86
            constraints: [new Assert\Type('integer')],
×
NEW
87
            provider: [self::class, 'toInt'],
×
NEW
88
            nativeType: new BuiltinType(TypeIdentifier::STRING),
×
NEW
89
        ),
×
NEW
90
        'pattern' => new QueryParameter(
×
NEW
91
            schema: ['pattern' => '\d'],
×
NEW
92
            nativeType: new BuiltinType(TypeIdentifier::STRING),
×
NEW
93
        ),
×
94
    ],
×
95
    provider: [self::class, 'collectionProvider']
×
96
)]
×
97
#[GetCollection(
98
    uriTemplate: 'with_disabled_parameter_validation{._format}',
×
99
    parameters: new Parameters([new QueryParameter(key: 'bla', required: true)]),
×
100
    queryParameterValidationEnabled: false,
×
101
    provider: [self::class, 'collectionProvider']
×
102
)]
×
103
#[GetCollection(
104
    uriTemplate: 'with_parameters_header_and_query{._format}',
×
NEW
105
    parameters: new Parameters([
×
NEW
106
        new QueryParameter(
×
NEW
107
            key: 'q',
×
NEW
108
            nativeType: new BuiltinType(TypeIdentifier::STRING),
×
NEW
109
        ),
×
NEW
110
        new HeaderParameter(
×
NEW
111
            key: 'q',
×
NEW
112
            nativeType: new BuiltinType(TypeIdentifier::STRING),
×
NEW
113
        ),
×
NEW
114
    ]),
×
115
    provider: [self::class, 'headerAndQueryProvider']
×
116
)]
×
117
#[GetCollection(
118
    uriTemplate: 'header_required',
×
119
    parameters: [
×
120
        'Req' => new HeaderParameter(required: true, schema: ['type' => 'string']),
×
121
    ],
×
122
    provider: [self::class, 'headerProvider']
×
123
)]
×
124
#[GetCollection(
NEW
125
    uriTemplate: 'header_integer',
×
NEW
126
    parameters: [
×
NEW
127
        'Foo' => new HeaderParameter(
×
NEW
128
            schema: [
×
NEW
129
                'type' => 'integer',
×
NEW
130
                'example' => 3,
×
NEW
131
                'minimum' => 1,
×
NEW
132
                'maximum' => 5,
×
NEW
133
            ],
×
NEW
134
            required: true,
×
NEW
135
        ),
×
NEW
136
    ],
×
NEW
137
    provider: [self::class, 'noopProvider']
×
NEW
138
)]
×
139
#[QueryParameter(key: 'everywhere')]
140
class WithParameter
141
{
142
    protected static int $counter = 1;
143
    public int $id = 1;
144

145
    #[Groups(['a'])]
146
    public $a = 'foo';
147
    #[Groups(['b', 'custom'])]
148
    public $b = 'bar';
149

150
    public static function collectionProvider()
151
    {
152
        return [new self()];
×
153
    }
154

155
    public static function provide()
156
    {
157
        return new self();
×
158
    }
159

160
    public static function assertArray(): void
161
    {
162
    }
×
163

164
    public static function assertFirst(): void
165
    {
166
        \assert(1 === static::$counter);
×
167
        ++static::$counter;
×
168
    }
169

170
    public static function assertSecond(): void
171
    {
172
        \assert(2 === static::$counter);
×
173
    }
174

175
    public static function provideGroup(Parameter $parameter, array $parameters = [], array $context = [])
176
    {
177
        $operation = $context['operation'];
×
178

179
        return $operation->withNormalizationContext(['groups' => $parameters['group']]);
×
180
    }
181

182
    public static function restrictAccess(): void
183
    {
184
        throw new AccessDeniedHttpException();
×
185
    }
186

187
    public static function headerAndQueryProvider(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
188
    {
189
        $parameters = $operation->getParameters();
×
190
        $values = [$parameters->get('q', HeaderParameter::class)->getValue(), $parameters->get('q', QueryParameter::class)->getValue()];
×
191

192
        return new JsonResponse($values);
×
193
    }
194

195
    public static function toInt(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
196
    {
197
        if (null === ($operation = $context['operation'] ?? null)) {
×
198
            return null;
×
199
        }
200

201
        $value = $parameter->getValue();
×
202

203
        if (is_numeric($value)) {
×
204
            $value = (int) $value;
×
205
        }
206

207
        $parameters = $operation->getParameters();
×
208
        $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties(
×
209
            $parameter->getExtraProperties() + ['_api_values' => $value]
×
210
        ));
×
211

212
        return $operation->withParameters($parameters);
×
213
    }
214

215
    public static function headerProvider(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
216
    {
217
        $parameters = $operation->getParameters();
×
218
        $values = [$parameters->get('Req', HeaderParameter::class)->getValue()];
×
219

220
        return new JsonResponse($values);
×
221
    }
222

223
    public static function noopProvider(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
224
    {
NEW
225
        return new JsonResponse([]);
×
226
    }
227
}
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