Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: Make serializer configurable via YAML configuration #1390

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lenchester
Copy link

@lenchester lenchester commented May 20, 2025

This change enables users to specify custom serializers with configuration
options in their Enqueue bundle YAML configuration, improving flexibility
when working with different message formats.

  • Add support for configuring serializers through YAML in RdKafkaContext
  • Allow serializer specification as a class name, array with options, or instance

- Add support for configuring serializers through YAML in RdKafkaContext
- Allow serializer specification as a class name, array with options, or instance
Comment on lines 72 to 86
if (is_string($config['serializer'])) {
$this->setSerializer(new $config['serializer']());
} elseif (is_array($config['serializer']) && isset($config['serializer']['class'])) {
$serializerClass = $config['serializer']['class'];
$serializerOptions = $config['serializer']['options'] ?? [];
if (!empty($serializerOptions)) {
$this->setSerializer(new $serializerClass($serializerOptions));
} else {
$this->setSerializer(new $serializerClass());
}
} elseif ($config['serializer'] instanceof Serializer) {
$this->setSerializer($config['serializer']);
} else {
throw new InvalidArgumentException('Invalid serializer configuration');
}
Copy link
Contributor

@Steveb-p Steveb-p May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd prefer something like this, with more verbosity provided for invalid configuration values.

$serializerClass "normalization" could be extracted to a separate private method. Similarly, exception message/instance creation.

Also, added check that the instance we are creating is actually of the right class. Usually object creation is a domain of an app, but in rare circumstance that somehow user could control what ends up in $config['serializer'], a malicious user could for example create PHP subprocesses that would allow backdoor access to a server. Unlikely, but it doesn't hurt to check.

Suggested change
if (is_string($config['serializer'])) {
$this->setSerializer(new $config['serializer']());
} elseif (is_array($config['serializer']) && isset($config['serializer']['class'])) {
$serializerClass = $config['serializer']['class'];
$serializerOptions = $config['serializer']['options'] ?? [];
if (!empty($serializerOptions)) {
$this->setSerializer(new $serializerClass($serializerOptions));
} else {
$this->setSerializer(new $serializerClass());
}
} elseif ($config['serializer'] instanceof Serializer) {
$this->setSerializer($config['serializer']);
} else {
throw new InvalidArgumentException('Invalid serializer configuration');
}
if ($config['serializer'] instanceof Serializer) {
$this->setSerializer($config['serializer']);
return;
}
if (is_string($config['serializer']) {
$serializerClass = $config['serializer'];
} elseif (is_array($config['serializer'] && isset($config['serializer']['class']) {
$serializerClass = $config['serializer']['class'];
} else {
throw new InvalidArgumentException(sprintf(
'Invalid serializer configuration. Expected "%s" option to be string, array with "class" property, or %s instance. Received %s instead.',
'serializer',
Serializer::class,
get_debug_type($config['serializer']),
);
}
if (!class_exists($serializerClass) || !is_a($serializerClass, Serializer, true)) {
throw new InvalidArgumentException(sprintf(
'Invalid serializer configuration. Expected "%s" option to be string, array with "class" property, or %s instance. Received %s instead.',
'serializer',
Serializer::class,
$serializerClass,
);
}
$serializerOptions = $config['serializer']['options'] ?? [];
$this->setSerializer(new $serializerClass($serializerOptions));

- Ensures robustness by providing a default serializer
- Refactoring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants