Skip to content

Commit 0f7fc94

Browse files
authored
fix(http): null for enums values in request bodies (#1498)
1 parent a280f56 commit 0f7fc94

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

packages/validation/src/Rules/IsEnum.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ private function isDesirable($value): bool
8282

8383
private function retrieveEnumValue(mixed $value): mixed
8484
{
85+
if ($value === null) {
86+
return null;
87+
}
88+
8589
if (method_exists($this->enum, 'tryFrom')) {
8690
return $this->enum::tryFrom($value);
8791
}

tests/Integration/Route/RequestToObjectMapperTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,15 @@ public function test_reserved_properties_cannot_be_mapped(): void
132132
},
133133
);
134134
}
135+
136+
public function test_missing_enum_value(): void
137+
{
138+
$request = new GenericRequest(method: Method::POST, uri: '/', body: []);
139+
140+
try {
141+
map($request)->to(RequestWithEnum::class);
142+
} catch (ValidationFailed $validationFailed) {
143+
$this->assertInstanceOf(NotNull::class, $validationFailed->failingRules['enumParam'][0]);
144+
}
145+
}
135146
}

0 commit comments

Comments
 (0)