-
-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mapper maps parameter names of inner objects #240
Comments
Hi @TimWolla thanks for the report, I think this issue should be fixed with 72cba32 which is a part of the version Feel free to re-open if the issue persists. 😉 |
This issue now exists again with 1.2.0. With two new tests for behavior which I understand to be the reason for the revert my expected behavior should now look like this: <?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\MapperBuilder;
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
final class ParameterNamesTest extends IntegrationTest
{
/**
* @dataProvider timezoneTargets
*/
public function test_wrong_parameter_names_throws_exception(string $class): void
{
$source = [
'timezone' => 'UTC',
];
$this->expectException(MappingError::class);
(new MapperBuilder())->mapper()->map($class, $source);
}
/**
* @dataProvider timezoneTargets
*/
public function test_right_parameter_names_work(string $class): void
{
$source = [
'tz1' => 'UTC',
];
self::assertSame('UTC', (new MapperBuilder())->mapper()->map($class, $source)->tz1->getName());
}
/**
* @dataProvider timezoneTargets
*/
public function test_right_parameter_names_with_additional_array_work(string $class): void
{
$source = [
'tz1' => [
'timezone' => 'UTC',
],
];
self::assertSame('UTC', (new MapperBuilder())->mapper()->map($class, $source)->tz1->getName());
}
/**
* @dataProvider timezoneTargets
*/
public function test_right_parameter_names_with_additional_array_with_incorrect_keys_throws_exception(string $class): void
{
$source = [
'tz1' => [
'tz' => 'UTC',
],
];
$this->expectException(MappingError::class);
(new MapperBuilder())->mapper()->map($class, $source)->tz1->getName();
}
public function timezoneTargets()
{
return [
[Target1::class],
[Target2::class],
];
}
}
final class Target1
{
public function __construct(
public readonly \DateTimeZone $tz1,
) {
}
}
final class Target2
{
public function __construct(
public readonly \DateTimeZone $tz1,
public readonly ?\DateTimeZone $tz2 = null,
) {
}
} |
Hi @TimWolla, what a fast post-release feedback. 🙂 I honestly forgot about this usecase, I'm sorry it troubles your application. I'll have to give it some thoughts, maybe a flag should be added in the mapper builder to switch the feature on/off. If you see any better way, please let me know. In the meantime, I re-open this issue. |
My understanding is that for single-parameter classes the following should work:
But what should not work:
So basically if my updated test passes, the behavior should be correct, it supports the use case in #269, but correctly rejects garbage. |
What might be reasonable as well is:
|
Oh, now that I'm reading through my comments, I believe I understand the problem: Because we're dealing with two nested single-parameter objects, it's not clear to which of the two a single-value-object applies. The |
Unfortunately I was not able to think of a better title. Please consider the following failing test case:
Now consider the following situation:
Target1
. This class expects a singletz1
parameter containing a value timezone string.timezone=UTC
(i.e. accidentally usestimezone
instead oftz1
for the parameter name).DateTimeZone::__construct()
is called$timezone
.$tz2
.The text was updated successfully, but these errors were encountered: