Skip to content

Commit 329acf2

Browse files
authored
fix(metadata): infer parameter string type from schema (api-platform#7161)
1 parent 5459ba3 commit 329acf2

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ private function getDefaultParameters(Operation $operation, string $resourceClas
154154
// this forces the type to be only a list
155155
if ('array' === ($parameter->getSchema()['type'] ?? null)) {
156156
$parameter = $parameter->withNativeType(Type::list(Type::string()));
157+
} elseif ('string' === ($parameter->getSchema()['type'] ?? null)) {
158+
$parameter = $parameter->withNativeType(Type::string());
157159
} else {
158160
$parameter = $parameter->withNativeType(Type::union(Type::string(), Type::list(Type::string())));
159161
}

tests/Fixtures/TestBundle/ApiResource/WithParameter.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
2929
use Symfony\Component\Serializer\Attribute\Groups;
3030
use Symfony\Component\TypeInfo\Type\BuiltinType;
31+
use Symfony\Component\TypeInfo\Type\CollectionType;
32+
use Symfony\Component\TypeInfo\Type\GenericType;
33+
use Symfony\Component\TypeInfo\Type\UnionType;
3134
use Symfony\Component\TypeInfo\TypeIdentifier;
3235
use Symfony\Component\Validator\Constraints\All;
3336
use Symfony\Component\Validator\Constraints as Assert;
@@ -61,7 +64,21 @@
6164
#[GetCollection(
6265
uriTemplate: 'with_parameters_country{._format}',
6366
parameters: [
64-
'country' => new QueryParameter(schema: ['type' => 'string'], constraints: [new Country()]),
67+
'country' => new QueryParameter(
68+
schema: ['type' => 'string'],
69+
constraints: [new Country()],
70+
nativeType: new UnionType(
71+
new BuiltinType(TypeIdentifier::STRING),
72+
new CollectionType(
73+
new GenericType( // @phpstan-ignore-line
74+
new BuiltinType(TypeIdentifier::ARRAY), // @phpstan-ignore-line
75+
new BuiltinType(TypeIdentifier::INT),
76+
new BuiltinType(TypeIdentifier::STRING),
77+
),
78+
true,
79+
),
80+
)
81+
),
6582
],
6683
provider: [self::class, 'collectionProvider']
6784
)]
@@ -104,8 +121,7 @@
104121
nativeType: new BuiltinType(TypeIdentifier::STRING),
105122
),
106123
'pattern' => new QueryParameter(
107-
schema: ['pattern' => '\d'],
108-
nativeType: new BuiltinType(TypeIdentifier::STRING),
124+
schema: ['pattern' => '\d', 'type' => 'string'],
109125
),
110126
],
111127
provider: [self::class, 'collectionProvider']

0 commit comments

Comments
 (0)