Skip to content

Commit d1765ef

Browse files
authored
fix ValidatedMapperCompiler running before UndefinedAwareMapperCompiler (#91)
1 parent ae16e9c commit d1765ef

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/Compiler/MapperFactory/DefaultMapperCompilerFactory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,20 +486,20 @@ protected function addValidator(
486486
$validatorInputType = $validatorCompiler->getInputType();
487487
$mapperOutputType = $mapperCompiler->getOutputType();
488488

489-
if (PhpDocTypeUtils::isSubTypeOf($mapperOutputType, $validatorInputType)) {
490-
return new ValidatedMapperCompiler($mapperCompiler, [$validatorCompiler]);
491-
}
492-
493489
if ($mapperCompiler instanceof MapDefaultValue) {
494490
return new MapDefaultValue($this->addValidator($mapperCompiler->mapperCompiler, $validatorCompiler), $mapperCompiler->defaultValue);
495491
}
496492

493+
if ($mapperCompiler instanceof MapOptional) {
494+
return new MapOptional($this->addValidator($mapperCompiler->mapperCompiler, $validatorCompiler));
495+
}
496+
497497
if ($mapperCompiler instanceof MapNullable) {
498498
return new MapNullable($this->addValidator($mapperCompiler->innerMapperCompiler, $validatorCompiler));
499499
}
500500

501-
if ($mapperCompiler instanceof MapOptional) {
502-
return new MapOptional($this->addValidator($mapperCompiler->mapperCompiler, $validatorCompiler));
501+
if (PhpDocTypeUtils::isSubTypeOf($mapperOutputType, $validatorInputType)) {
502+
return new ValidatedMapperCompiler($mapperCompiler, [$validatorCompiler]);
503503
}
504504

505505
throw CannotCreateMapperCompilerException::withIncompatibleValidator($validatorCompiler, $mapperCompiler);

tests/Compiler/MapperFactory/Data/BrandInputWithDefaultValues.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace ShipMonkTests\InputMapper\Compiler\MapperFactory\Data;
44

55
use ShipMonk\InputMapper\Compiler\Mapper\Optional;
6+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertInt32;
7+
use ShipMonk\InputMapper\Compiler\Validator\String\AssertStringLength;
68

79
class BrandInputWithDefaultValues
810
{
@@ -12,9 +14,11 @@ class BrandInputWithDefaultValues
1214
*/
1315
public function __construct(
1416
#[Optional(default: 'ShipMonk')]
17+
#[AssertStringLength(min: 5)]
1518
public readonly string $name,
1619

1720
#[Optional]
21+
#[AssertInt32]
1822
public readonly ?int $foundedIn,
1923

2024
#[Optional(default: ['Jan Bednář'])]

tests/Compiler/MapperFactory/DefaultMapperCompilerFactoryTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use ShipMonk\InputMapper\Compiler\MapperFactory\DefaultMapperCompilerFactory;
3737
use ShipMonk\InputMapper\Compiler\Type\GenericTypeParameter;
3838
use ShipMonk\InputMapper\Compiler\Validator\Array\AssertListLength;
39+
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertInt32;
3940
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertIntRange;
4041
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertNegativeInt;
4142
use ShipMonk\InputMapper\Compiler\Validator\Int\AssertNonNegativeInt;
@@ -151,11 +152,17 @@ public static function provideCreateOkData(): iterable
151152
BrandInputWithDefaultValues::class,
152153
[
153154
'name' => new MapDefaultValue(
154-
new MapString(),
155+
new ValidatedMapperCompiler(
156+
new MapString(),
157+
[new AssertStringLength(min: 5)],
158+
),
155159
'ShipMonk',
156160
),
157161
'foundedIn' => new MapDefaultValue(
158-
new MapNullable(new MapInt()),
162+
new MapNullable(new ValidatedMapperCompiler(
163+
new MapInt(),
164+
[new AssertInt32()],
165+
)),
159166
null,
160167
),
161168
'founders' => new MapDefaultValue(

0 commit comments

Comments
 (0)