Skip to content
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

fix(php): JSON serialize empty arrays as empty object #5986

Merged
merged 5 commits into from
Feb 13, 2025

Conversation

Swimburger
Copy link
Member

Description

JSON serialize empty arrays as empty object

Changes Made

  • If incoming array is sequential, return as array and let PHP JSON serializer handle as is
  • Else, continue as usual, but if result is an empty array, cast to object to force PHP JSON serializer to serialize it as an empty object.

Testing

  • Unit tests added/updated
  • Manual testing completed
  • Seed

Copy link
Member

@amckinney amckinney left a comment

Choose a reason for hiding this comment

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

Only nits, but lgtm!

* @param array $arr
* @return bool
*/
private static function isSequential(array $arr): bool { // @phpstan-ignore-line
Copy link
Member

Choose a reason for hiding this comment

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

What are we ignoring here?

Copy link
Member Author

Choose a reason for hiding this comment

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

phpstan wants array to have more information, but we don't have more info

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, phpstan doesn't mind mixed[]

$overrideProperties = $options['bodyProperties'] ?? [];
if(is_array($body) && self::isSequential($body)){
Copy link
Member

Choose a reason for hiding this comment

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

nit: Let's tidy up the spacing on these:

Suggested change
if(is_array($body) && self::isSequential($body)){
if (is_array($body) && self::isSequential($body)) {

Comment on lines 195 to 201
if(is_array($result)){
$result = array_merge($result, $overrideProperties);
if(empty($result)){
// force to be serialized as {} instead of []
return (object)($result);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if(is_array($result)){
$result = array_merge($result, $overrideProperties);
if(empty($result)){
// force to be serialized as {} instead of []
return (object)($result);
}
}
if (is_array($result)){
$result = array_merge($result, $overrideProperties);
if (empty($result)){
// force to be serialized as {} instead of []
return (object)($result);
}
}

@Swimburger Swimburger merged commit 64f11e4 into main Feb 13, 2025
49 of 51 checks passed
@Swimburger Swimburger deleted the niels/php/json--serialize-empty-arrays-as-object branch February 13, 2025 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants