-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Config] Fix array shape generation for backed enums #62563
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
Conversation
07f5138 to
0879077
Compare
src/Symfony/Component/Config/Definition/ArrayShapeGenerator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Config/Tests/Fixtures/StringBackedTestEnum.php
Outdated
Show resolved
Hide resolved
116a0ef to
12fa494
Compare
The reason is that EnumNode exists for quite some time now and it was used before enum were introduced to only accept a set of scalar values (but no native enum support). Providing public function getPermissibleValues(string $separator, bool $trim = true): string
{
if (is_subclass_of($this->enumFqcn, \BackedEnum::class)) {
return implode($separator, array_column($this->enumFqcn::cases(), 'value'));
}
// ...
}I think it is better because when dealing with configuration, you don't necessarily want to expose the whole underlying enum FQCN and deal with long class names. Hope this helps! |
|
When using |
|
So my fix is correct, when I interpret your comment @alexandre-daubois, right? While @stof your comment is an improvement I can implement if wanted |
Like this @stof ? |
alexandre-daubois
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! 🙂
nicolas-grekas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor suggestion. Also: tests are failing.
--- a/src/Symfony/Component/Config/Definition/EnumNode.php
+++ b/src/Symfony/Component/Config/Definition/EnumNode.php
@@ -85,14 +85,12 @@ class EnumNode extends ScalarNode
$values = array_column($this->enumFqcn::cases(), 'value');
- return implode($separator, array_map(static function ($value) {
- return json_encode($value, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
- }, $values));
+ return implode($separator, array_map(static fn ($value) => json_encode($value, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE), $values));
}
return implode($separator, array_unique(array_map(static function ($value) use ($trim) {
if (!$value instanceof \UnitEnum) {
- return json_encode($value);
+ return json_encode($value, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | \JSON_PRESERVE_ZERO_FRACTION);
}
return $trim ? ltrim(var_export($value, true), '\\') : var_export($value, true);|
Failures unrelated |
b7ee321 to
62422c4
Compare
|
Thank you @OskarStark. |
…karStark) This PR was merged into the main branch. Discussion ---------- Use Symfony 7.4 for demo, examples and ai.symfony.com | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | -- | License | MIT ### Needs - [x] symfony/symfony#62563 Commits ------- ab840cc minor: Require Symfony 7.4 as minimum version
Spotted in symfony/ai#1013 ➡️ https://github.com/symfony/ai/actions/runs/19780810683/job/56680686767?pr=1013
It works for
redis, but not forpostgres, becauseredisis using->values(Distance::cases())andpostgresis using->enumFqcn(PostgresDistance::class):Redis
Enum
Config
RESULT
* redis?: array<string, array{ // Default: [] * connection_parameters?: mixed, // see https://github.com/phpredis/phpredis?tab=readme-ov-file#example-1 * client?: string, // a service id of a Redis client * index_name: string, * key_prefix?: string, // Default: "vector:" * distance?: \Symfony\AI\Store\Bridge\Redis\Distance::Cosine|\Symfony\AI\Store\Bridge\Redis\Distance::L2|\Symfony\AI\Store\Bridge\Redis\Distance::Ip, // Distance metric to use for vector similarity search // Default: "COSINE" * }>,Postgres
Enum
Config
RESULT
* postgres?: array<string, array{ // Default: [] * dsn?: string, * username?: string, * password?: string, * table_name: string, * vector_field?: string, * distance?: cosine|inner_product|l1|l2, // Distance metric to use for vector similarity search // Default: "l2" * dbal_connection?: string, * }>,you can see, that the result is different:
FQCN:
distance?: \Symfony\AI\Store\Bridge\Redis\Distance::Cosine|\Symfony\AI\Store\Bridge\Redis\Distance::L2|\Symfony\AI\Store\Bridge\Redis\Distance::Ip,vs. strings:
distance?: cosine|inner_product|l1|l2,Why?