yiisoft / yiisoft/yii2-queue

src/serializers/JsonSerializer.php doesn't work if a class has parameters in the constructor

Open
#455 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
1.1k
Forks
285
Avg merge
5d 3h
Merged PRs (30d)
2

Description

What steps will reproduce the problem?

Try to serialize an active record which has a datetime column with its default value

What's expected?

Serialize the object and then successfully unserializing it.

What do you get instead?

When you try to unserialize, you get an error because yii\db\Expression is missing required parameters in the constructor

Additional info

I have an idea to use reflection to get around the issue. Of course it will only work if the parameters are public properties. I will post here if it works.

<?php
class JsonSerializer extends \yii\queue\serializers\JsonSerializer
{
	public $constructorParamsKey = 'constructorParams';

	/**
	 * @param mixed $data
	 * @return array|mixed
	 * @throws InvalidConfigException
	 */
	protected function toArray($data)
	{
		if (is_object($data)) {
			$result = [$this->classKey => get_class($data)];
			foreach (get_object_vars($data) as $property => $value) {
				if ($property === $this->classKey) {
					throw new InvalidConfigException("Object cannot contain $this->classKey property.");
				}
				if ($property === $this->constructorParamsKey) {
					throw new InvalidConfigException("Object cannot contain $this->constructorParamsKey property.");
				}

				$result[$property] = $this->toArray($value);
			}

			$constructorParams = $this->getConstructorParams($data);

			if(!empty($constructorParams)){
				$result['constructorParams'] = $constructorParams;
			}

			return $result;
		}

		if (is_array($data)) {
			$result = [];
			foreach ($data as $key => $value) {
				if ($key === $this->classKey) {
					throw new InvalidConfigException("Array cannot contain $this->classKey key.");
				}
				$result[$key] = $this->toArray($value);
			}

			return $result;
		}

		return $data;
	}

	/**
	 * @param array $data
	 * @return mixed
	 */
	protected function fromArray($data)
	{
		if (!is_array($data)) {
			return $data;
		}

		if (!isset($data[$this->classKey])) {
			$result = [];
			foreach ($data as $key => $value) {
				$result[$key] = $this->fromArray($value);
			}

			return $result;
		}

		$config = ['class' => $data[$this->classKey]];
		unset($data[$this->classKey]);

		$constructorParams = [];
		if(isset($data['constructorParams'])){
			$constructorParams = $data['constructorParams'];
			unset($data['constructorParams']);
		}

		foreach ($data as $property => $value) {
			$config[$property] = $this->fromArray($value);
		}

		return Yii::createObject($config, $constructorParams);
	}

	/**
	 * Returns the constructor params and their values
	 * @param $object
	 * @return array
	 */
	protected function getConstructorParams($object)
	{
		$ref = new ReflectionClass($object);
		if (!$ref->isInstantiable()) {
			return [];
		}

		$parameters = [];
		$constructor = $ref->getConstructor();
		$params = $constructor->getParameters();

		foreach ($params as $param) {
			$name = $param->getName();
			if(property_exists($object, $name)) {
				$parameters[] = $object->{$name};
			}
		}

		return $parameters;
	}
}
Q A
Yii version 2.0.15.1
PHP version 7.2.34
Operating system Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/serializers/JsonSerializer.php and reproduce the failure by serializing and unserializing an active record with a datetime column whose default is a yii\db\Expression. Trace how constructor arguments are handled during deserialization, then verify that the object round trip succeeds without the missing-parameter error.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.