You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/SessionParameterValueResolver.php
+16-3Lines changed: 16 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,13 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
29
29
}
30
30
31
31
if ((!$type = $argument->getType()) || (!class_exists($type) && !interface_exists($type, false))) {
32
-
thrownew \LogicException(\sprintf('#[MapSessionParameter] cannot be used on controller argument "$%s": "%s" is not a class or interface name.', $argument->getName(), $type));
32
+
if ($type && (str_contains($type, '|') || str_contains($type, '&'))) {
33
+
if (!$argument->hasDefaultValue() && !$argument->isNullable()) {
34
+
thrownew \LogicException(\sprintf('#[MapSessionParameter] cannot be used on controller argument "$%s": "%s" is an union or intersection type, you need to make the parameter nullable or provide a default value..', $argument->getName(), $type));
35
+
}
36
+
} else {
37
+
thrownew \LogicException(\sprintf('#[MapSessionParameter] cannot be used on controller argument "$%s": "%s" is not a class or interface name.', $argument->getName(), $type));
38
+
}
33
39
}
34
40
35
41
if (interface_exists($type, false) && !$argument->hasDefaultValue() && !$argument->isNullable()) {
@@ -39,14 +45,21 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
39
45
$name = $attribute->name ?? $argument->getName();
40
46
if ($request->getSession()->has($name)) {
41
47
$value = $request->getSession()->get($name);
42
-
if (!$valueinstanceof$type) {
48
+
if (!$valueinstanceof$type && !str_contains($type, '|') && !str_contains($type, '&')) {
43
49
thrownew \LogicException(\sprintf('#[MapSessionParameter] cannot be used to map controller argument "$%s": the session contains a value of type "%s" which is not an instance of "%s".', $argument->getName(), get_debug_type($value), $type));
44
50
}
45
51
46
52
return [$value];
47
53
}
48
54
49
-
if (\is_object($value = $argument->hasDefaultValue() ? $argument->getDefaultValue() : new$type())) {
55
+
if ($argument->hasDefaultValue()) {
56
+
$value = $argument->getDefaultValue();
57
+
} else {
58
+
// handle the type SessionInterface|null $param, which doesn't have a default value and can't be instantiated.
'#[MapSessionParameter] cannot be used on controller argument "$MySessionObject": "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\SessionParameterInterface" is an interface, you need to make the parameter nullable or provide a default value.',
'#[MapSessionParameter] cannot be used on controller argument "$MySessionObject": "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\BasicSessionParameter|Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\EmptySessionParameter" is an union or intersection type, you need to make the parameter nullable or provide a default value.',
'#[MapSessionParameter] cannot be used on controller argument "$MySessionObject": "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\BasicSessionParameter&Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\EmptySessionParameter" is an union or intersection type, you need to make the parameter nullable or provide a default value.',
96
+
];
87
97
}
88
98
89
99
/**
@@ -131,13 +141,37 @@ public static function validDataProvider(): iterable
131
141
null,
132
142
];
133
143
yield'nullable interface without default value' => [
0 commit comments