Copilot commented on code in PR #3787:
URL: https://github.com/apache/thrift/pull/3787#discussion_r3999760343
##########
lib/php/lib/Base/TBase.php:
##########
@@ -349,8 +349,24 @@ private function writeList(array $var, array $spec,
TProtocol $output, bool $set
} else {
$xfer += $output->writeListBegin($etype, count($var));
}
+ $setUsesValues = false;
+ if ($set && array_is_list($var)) {
+ // Preserve the legacy `element => true` marker form when every
+ // value is `true`. This keeps ambiguous `set<bool>` inputs such
+ // as `[true]` on the backward-compatible path.
+
+ foreach ($var as $candidate) {
+ if ($candidate !== true) {
+ $setUsesValues = true;
+ break;
+ }
+ }
+ }
foreach ($var as $key => $val) {
- $elem = $set ? $key : $val;
+ $elem = $set && !$setUsesValues ? $key : $val;
+ if ($set && !$setUsesValues && $etype === TType::BOOL) {
+ $elem = (bool) $elem;
Review Comment:
This only recasts legacy keys for `bool`. Under `declare(strict_types=1)`,
PHP coerces numeric-string array keys to `int`, so a legacy `set<string>` or
`set<uuid>` such as `['123' => true]` reaches `writeString()`/`writeUuid()` as
an integer and throws `TypeError`. The generated serializer already recasts
every scalar key; mirror those type-specific casts here so the shared helper
also preserves legacy keyed sets.
##########
lib/php/lib/Exception/TException.php:
##########
@@ -348,8 +348,24 @@ private function writeList(array $var, array $spec,
TProtocol $output, bool $set
} else {
$xfer += $output->writeListBegin($etype, count($var));
}
+ $setUsesValues = false;
+ if ($set && array_is_list($var)) {
+ // Preserve the legacy `element => true` marker form when every
+ // value is `true`. This keeps ambiguous `set<bool>` inputs such
+ // as `[true]` on the backward-compatible path.
+
+ foreach ($var as $candidate) {
+ if ($candidate !== true) {
+ $setUsesValues = true;
+ break;
+ }
+ }
+ }
foreach ($var as $key => $val) {
- $elem = $set ? $key : $val;
+ $elem = $set && !$setUsesValues ? $key : $val;
+ if ($set && !$setUsesValues && $etype === TType::BOOL) {
+ $elem = (bool) $elem;
+ }
Review Comment:
This only recasts legacy keys for `bool`. Under `declare(strict_types=1)`,
PHP coerces numeric-string array keys to `int`, so a legacy `set<string>` or
`set<uuid>` such as `['123' => true]` reaches `writeString()`/`writeUuid()` as
an integer and throws `TypeError`. The generated serializer already recasts
every scalar key; mirror those type-specific casts here so the shared helper
also preserves legacy keyed sets.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]