EventListener/JmsSerializerSubscriber.php
public function onPreSerialize(PreSerializeEvent $event): void
    {
        $object = $event->getObject();

        if ($object instanceof Proxy && !$object->__isInitialized()) {
            $object->__load();
        }

        $objectUid = \spl_object_hash($object);
        if (\array_key_exists($objectUid, $this->serializedObjects)) {
            return;
        }

        $classAnnotation = $this->annotationReader->getClassAnnotation(
            new \ReflectionClass(ClassUtils::getClass($object)),
            VichSerializableClass::class
        );

        if ($classAnnotation instanceof VichSerializableClass) {
            $reflectionClass = ClassUtils::newReflectionClass(\get_class($object));
            $this->logger->debug(\sprintf(
                'Found @VichSerializableClass annotation for the class "%s"',
                $reflectionClass->getName()
            ));

            foreach ($reflectionClass->getProperties() as $property) {
                $vichSerializableAnnotation = $this->annotationReader->getPropertyAnnotation($property, VichSerializableField::class);

                if ($vichSerializableAnnotation instanceof VichSerializableField) {
                    $vichUploadableFileAnnotation = $this->annotationReader->getPropertyAnnotation($property, UploadableField::class);

                    if ($vichUploadableFileAnnotation instanceof UploadableField) {
                        throw new IncompatibleUploadableAndSerializableFieldAnnotationException(\sprintf(
                            'The field "%s" in the class "%s" cannot have @UploadableField and @VichSerializableField annotations at the same moment.',
                            $property->getName(),
                            $reflectionClass->getName()
                        ));
                    }
                    $this->logger->debug(\sprintf(
                        'Found @VichSerializableField annotation for the field "%s" in the class "%s"',
                        $property->getName(),
                        $reflectionClass->getName()
                    ));

                    $uri = null;
                    $property->setAccessible(true);

                    if ($property->getValue($event->getObject())) {
                        $uri = $this->storage->resolveUri($object, $vichSerializableAnnotation->getField());
                        if ($vichSerializableAnnotation->isIncludeHost() && false === \filter_var($uri, FILTER_VALIDATE_URL)) {
                            $uri = $this->getHostUrl().$uri;
                        }
                    }
                    $this->serializedObjects[$objectUid][$property->getName()] = $property->getValue($event->getObject());
                    $property->setValue($object, $uri);
                }
            }
        }
    }
public function onPreSerialize(PreSerializeEvent $event): void
    {
        $object = $event->getObject();

        if ($object instanceof Proxy && !$object->__isInitialized()) {
            $object->__load();
        }

        $objectUid = \spl_object_hash($object);
        if (\array_key_exists($objectUid, $this->serializedObjects)) {
            return;
        }

        $classAnnotation = $this->annotationReader->getClassAnnotation(
            new \ReflectionClass(ClassUtils::getClass($object)),
            VichSerializableClass::class
        );

        if ($classAnnotation instanceof VichSerializableClass) {
            $reflectionClass = ClassUtils::newReflectionClass(\get_class($object));
            $this->logger->debug(\sprintf(
                'Found @VichSerializableClass annotation for the class "%s"',
                $reflectionClass->getName()
            ));

            foreach ($reflectionClass->getProperties() as $property) {
                $vichSerializableAnnotation = $this->annotationReader->getPropertyAnnotation($property, VichSerializableField::class);

                if ($vichSerializableAnnotation instanceof VichSerializableField) {
                    $vichUploadableFileAnnotation = $this->annotationReader->getPropertyAnnotation($property, UploadableField::class);

                    if ($vichUploadableFileAnnotation instanceof UploadableField) {
                        throw new IncompatibleUploadableAndSerializableFieldAnnotationException(\sprintf(
                            'The field "%s" in the class "%s" cannot have @UploadableField and @VichSerializableField annotations at the same moment.',
                            $property->getName(),
                            $reflectionClass->getName()
                        ));
                    }
                    $this->logger->debug(\sprintf(
                        'Found @VichSerializableField annotation for the field "%s" in the class "%s"',
                        $property->getName(),
                        $reflectionClass->getName()
                    ));

                    $uri = null;
                    $property->setAccessible(true);

                    if ($property->getValue($event->getObject())) {
                        $uri = $this->storage->resolveUri($object, $vichSerializableAnnotation->getField());
                        if ($vichSerializableAnnotation->isIncludeHost() && false === \filter_var($uri, FILTER_VALIDATE_URL)) {
                            $uri = $this->getHostUrl().$uri;
                        }
                    }
                    $this->serializedObjects[$objectUid][$property->getName()] = $property->getValue($event->getObject());
                    $property->setValue($object, $uri);
                }
            }
        }
    }
@param PreSerializeEvent $event

@throws IncompatibleUploadableAndSerializableFieldAnnotationException