Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### v4.12.1 (2025-12-15)
* * *

### Bug Fixes:
* Fix empty json attributes arrays being sent incorrectly; now encoder encodes empty json attribute[] as {} using JSON_FORCE_OBJECT.(resolves #115)

### v4.12.0 (2025-11-26)
* * *

Expand Down
2 changes: 1 addition & 1 deletion src/ValueObjects/Encoders/JsonParamEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JsonParamEncoder implements ParamEncoderInterface

public static function encode(array $params, array $jsonKeys = []): string
{
return json_encode(self::formatJsonKeysAsSnakeCase($params));
return json_encode(self::formatJsonKeysAsSnakeCase($params), JSON_FORCE_OBJECT);
}

public static function formatJsonKeysAsSnakeCase($value, $maxDepth = 1000, $currentDepth = 0): array
Expand Down
2 changes: 1 addition & 1 deletion src/ValueObjects/Encoders/URLFormEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static function serialize($value, $prefix = null, $idx = null, $jsonKeys
$key = (!is_null($prefix) ? $prefix : '') .
(!is_null($prefix) ? '[' . $usK . ']' : $usK) .
(!is_null($idx) ? '[' . $idx . ']' : '');
$serialized[$key] = is_string($v)?$v:json_encode($v);
$serialized[$key] = is_string($v)?$v:json_encode($v, JSON_FORCE_OBJECT);
} else if (is_array($v) && !is_int($k)) {
$tempPrefix = (!is_null($prefix)) ? $prefix . '[' . Util::toUnderscoreFromCamelCase($k) . ']' : Util::toUnderscoreFromCamelCase($k);
$serialized = array_merge($serialized, self::serialize($v, $tempPrefix, null, $jsonKeys, $level + 1));
Expand Down
2 changes: 1 addition & 1 deletion src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

final class Version
{
const VERSION = '4.12.0';
const VERSION = '4.12.1';
}

?>
18 changes: 18 additions & 0 deletions tests/ValueObjects/Encoder/URLFormEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,22 @@ public function testEncodeParamsWithAttributeAsJsonStringAndCamelCaseAtSameLevel
$this->assertIsString($encoded);
$this->assertSame("first_name=John&last_name=Doe&billing_address=%7B%22city%22%3A%22Walnut%22%2C%22State%22%3A%22California%22%7D", $encoded);
}


/** Convert params to URL-form encoding. Do not transform empty arrays into associative arrays; convert them to an empty object {} instead. */
/** first_name=John&last_name=Doe&meta_data={}*/
public function testEncodeParamsWithAEmptyJsonAttribute(): void {
$params = [
"first_name" => "John",
"last_name" => "Doe",
"meta_data" => [
]
];
$json_keys = [
"metaData" => 0
];
$encoded = URLFormEncoder::encode($params, $json_keys);
$this->assertIsString($encoded);
$this->assertSame("first_name=John&last_name=Doe&meta_data=%7B%7D", $encoded);
}
}