Skip to content

Commit 6d6e115

Browse files
committed
Add failing test case to report error when using DiscriminatorMap with missing 'typePropery'
1 parent 5a0b2c1 commit 6d6e115

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This is not god! It should raise a proper exception, telling the key
2+
// `linkType` is not present is the source
3+
Error {
4+
+class: "Error"
5+
+message: "Cannot instantiate abstract class AutoMapper\Tests\AutoMapperTest\DiscriminatorMapBadConfiguration\LinkType"
6+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace AutoMapper\Tests\AutoMapperTest\DiscriminatorMapBadConfiguration;
4+
5+
use AutoMapper\Tests\AutoMapperBuilder;
6+
use Symfony\Component\Serializer\Attribute\DiscriminatorMap;
7+
8+
#[DiscriminatorMap('linkType', [
9+
'default' => DefaultLinkType::class,
10+
])]
11+
abstract class LinkType
12+
{
13+
}
14+
15+
class DefaultLinkType extends LinkType
16+
{
17+
public function __construct(
18+
public string $link,
19+
) {
20+
}
21+
}
22+
23+
$source = [
24+
// There is a typo on the key `type`.
25+
// It should be `linkType` instead of `type`.
26+
'type' => 'default',
27+
'link' => 'https://example.com',
28+
];
29+
30+
try {
31+
return AutoMapperBuilder::buildAutoMapper()->map($source, LinkType::class);
32+
} catch (\Throwable $th) {
33+
return $th;
34+
}

0 commit comments

Comments
 (0)