Skip to content

ObjectMapperCodeGenerator doesn't recurse for serializeObject() #75

@Nadyita

Description

@Nadyita

I have a (shortened) class like this:

class ImplantConfig {
	/** psalm-param int<1,300> $ql */
	public function __construct(
		public int $ql=300,
		public ?SlotConfig $head=null,
	) {
	}
}

When I use the ObjectMapperCodeGenerator, it recurses into SlotConfig and further down, but only for hydration.
The generated code looks like this:

    /**
     * @template T of object
     * @param class-string<T> $className
     * @return T
     */
    public function hydrateObject(string $className, array $payload): object
    {
        return match($className) {
            'Nadybot\Modules\IMPLANT_MODULE\ImplantConfig' => $this->hydrateNadybot⚡️Modules⚡️IMPLANT_MODULE⚡️ImplantConfig($payload),
                'Nadybot\Modules\IMPLANT_MODULE\SlotConfig' => $this->hydrateNadybot⚡️Modules⚡️IMPLANT_MODULE⚡️SlotConfig($payload),
                'Nadybot\Modules\IMPLANT_MODULE\SymbiantSlot' => $this->hydrateNadybot⚡️Modules⚡️IMPLANT_MODULE⚡️SymbiantSlot($payload),
                'Nadybot\Modules\IMPLANT_MODULE\AbilityAmount' => $this->hydrateNadybot⚡️Modules⚡️IMPLANT_MODULE⚡️AbilityAmount($payload),
            default => throw UnableToHydrateObject::noHydrationDefined($className, $this->hydrationStack),
        };
    }

In comparison, the serialization code is this:

    /**
     * @template T
     *
     * @param T               $object
     * @param class-string<T> $className
     */
    public function serializeObjectOfType(object $object, string $className): mixed
    {
        try {
            return match($className) {
                'array' => $this->serializeValuearray($object),
            'Ramsey\Uuid\UuidInterface' => $this->serializeValueRamsey⚡️Uuid⚡️UuidInterface($object),
            'DateTime' => $this->serializeValueDateTime($object),
            'DateTimeImmutable' => $this->serializeValueDateTimeImmutable($object),
            'DateTimeInterface' => $this->serializeValueDateTimeInterface($object),
            'Nadybot\Modules\IMPLANT_MODULE\ImplantConfig' => $this->serializeObjectNadybot⚡️Modules⚡️IMPLANT_MODULE⚡️ImplantConfig($object),
                default => throw new \LogicException("No serialization defined for $className"),
            };
        } catch (\Throwable $exception) {
            throw UnableToSerializeObject::dueToError($className, $exception);
        }
    }

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions