-
Notifications
You must be signed in to change notification settings - Fork 357
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
\JsonSchema\Constraints\BaseConstraint::arrayToObjectRecursive() doesn't convert empty arrays to objects #407
Comments
Hum, this seems to only be on the 5.x.x branch. Adding cast to |
Strike that, it's on 6.0.0-dev, too. |
Thanks - fixed in #409. This will be backported to 5.x.x after merge. |
The issue was fixed for passing an empty array directly, but still persists for nested empty arrays. // Directly passing an empty array.
$array = [];
$object = \JsonSchema\Validator::arrayToObjectRecursive($array);
print_r($object);
// stdClass Object
// (
// ) // Passing a nested empty array.
$array = ['shouldBeAnObject' => []];
$object = \JsonSchema\Validator::arrayToObjectRecursive($array);
print_r($object);
// stdClass Object
// (
// [shouldBeAnObject] => Array
// (
// )
// ) It could be fixed by passing --- a/src/JsonSchema/Constraints/BaseConstraint.php
+++ b/src/JsonSchema/Constraints/BaseConstraint.php
@@ -142,7 +142,7 @@ class BaseConstraint
*/
public static function arrayToObjectRecursive($array)
{
- $json = json_encode($array);
+ $json = json_encode($array, \JSON_FORCE_OBJECT);
if (json_last_error() !== \JSON_ERROR_NONE) {
$message = 'Unable to encode schema array as JSON';
if (function_exists('json_last_error_msg')) { // Passing a nested empty array after adding JSON_FORCE_OBJECT flag.
$array = ['shouldBeAnObject' => []];
$object = \JsonSchema\Validator::arrayToObjectRecursive($array);
print_r($object);
// stdClass Object
// (
// [shouldBeAnObject] => stdClass Object
// (
// )
// ) |
Although, adding // Currently.
$array = ['key' => ['value1', 'value2']];
$object = \JsonSchema\Validator::arrayToObjectRecursive($array);
// stdClass Object
// (
// [key] => Array
// (
// [0] => value1
// [1] => value2
// )
// ) // After adding JSON_FORCE_OBJECT flag.
$array = ['key' => ['value1', 'value2']];
$object = \JsonSchema\Validator::arrayToObjectRecursive($array);
// stdClass Object
// (
// [key] => stdClass Object
// (
// [0] => value1
// [1] => value2
// )
// ) |
When an empty array is given to
\JsonSchema\Constraints\BaseConstraint::arrayToObjectRecursive()
, it is returned identical. This leads to issues later on when using aSchemaStorage
to contain this schema. Such an empty schema is only really used for tests, but this currently requires workarounds.The following example code
results in
The text was updated successfully, but these errors were encountered: