-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
ObjectMapperCodeGenerator
doesn't recurse for serializeObject()
#75
Comments
Hi, I'm trying to reproduce your issue, but I'm failing to do so. I did uncover another issue, which is resolved in the PR. Could you see if this represents your case? And if not, can you provide a case to reproduce your issue? |
Sadly, it doesn't fix my issue. I'll try to create a minimum working example, because my current codebase is 760k LOC |
Here we go: <?php
declare(strict_types=1);
require_once './vendor/autoload.php';
use EventSauce\ObjectHydrator\ObjectMapperCodeGenerator;
class A
{
public function __construct(
public string $x,
) {
}
}
class B
{
public function __construct(
public ?A $a,
) {
}
}
$b = new B(a: new A(x: "here"));
$mapper = new ObjectMapperCodeGenerator();
$code = $mapper->dump([$b::class], 'Seri\\Tester');
$fileName = tempnam('/tmp/', 'seri_');
file_put_contents($fileName, $code);
require_once $fileName;
$serializer = new \Seri\Tester();
echo(json_encode($serializer->serializeObject($b)) . PHP_EOL); This throws the error |
Right, the issue seems to be in the class expanding mechanism, If you add |
I think I've got it. Can you checkout the PR I linked earlier? |
Checking out an unintended side-effect of the fix now. |
Your code should work in the PR branch now. |
Yes, but the original one doesn't, because it's generating duplications. Minimum working proof: <?php
declare(strict_types=1);
require_once './vendor/autoload.php';
use EventSauce\ObjectHydrator\ObjectMapperCodeGenerator;
class A
{
public function __construct(
public string $x,
) {
}
}
class B
{
public function __construct(
public A $a,
) {
}
}
class C
{
public function __construct(
public A $a,
public B $b,
) {
}
}
$c = new C(a: new A(x: "haha!"), b: new B(a: new A(x: "here")));
$mapper = new ObjectMapperCodeGenerator();
$code = $mapper->dump([$c::class], 'Seri\\Tester');
$fileName = tempnam('/tmp/', 'seri_');
file_put_contents($fileName, $code);
require_once $fileName;
$serializer = new \Seri\Tester();
echo(json_encode($serializer->serializeObject($b)) . PHP_EOL); This throws |
A quick |
Okay, now I'm getting other errors. Might take a break for today, but it seems it happens when I have this: <?php declare(strict_types=1);
namespace Nadybot\Modules\IMPLANT_MODULE;
class SymbiantSlot {
/**
* @param AbilityAmount[] $reqs
* @param AbilityAmount[] $mods
*
* @psalm-param list<AbilityAmount> $reqs
* @psalm-param list<AbilityAmount> $mods
*/
public function __construct(
public string $name,
public int $treatment,
public int $level,
public array $reqs,
public array $mods,
) {
}
} For this case, it reports |
The class expansion mechanism is trying to do its best to resolve everything, but the more things are defined in relation to custom tooling, like psalm or via docblocks, the more its guesswork and not exact science. My recommendation is always to use |
Well, I don't have much choice with array not being typed in PHP 🤷 and I thought that the algorithm for serialization and hydration was working the same way. |
You can also take a stab at trying to solve it yourself. I'm doing my best to help you, providing support for a free tool that I literally do not benefit from in any shape or capacity. |
Yes, I could, but it might take some time, before I'm familiar enough with the code to fix this kind of thing. Sadly, time doesn't grow on trees, and I was in the middle of preparing a new release when I noticed performance impacts when using the reflection version. So yes, I'm gonna try and fix it, but it will take a bit of time 😞 |
I have a (shortened) class like this:
When I use the
ObjectMapperCodeGenerator
, it recurses intoSlotConfig
and further down, but only for hydration.The generated code looks like this:
In comparison, the serialization code is this:
As you can see, it doesn't recurse, and to me, that is a bug. I'm using the latest
1.5.0
release. Am I mistaken?The text was updated successfully, but these errors were encountered: