Skip to content

Commit 549b4af

Browse files
eraydbighappyface
authored andcommitted
[BUGFIX] Cast empty schema arrays to object (#409)
* Cast root to object * Use function_exists to allow polyfill compatibility * Move array->object conversion to SchemaConstraint & SchemaStorage Fixes issue #408
1 parent d7fcdcb commit 549b4af

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

src/JsonSchema/Constraints/BaseConstraint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ public static function arrayToObjectRecursive($array)
146146
$json = json_encode($array);
147147
if (json_last_error() !== \JSON_ERROR_NONE) {
148148
$message = 'Unable to encode schema array as JSON';
149-
if (version_compare(phpversion(), '5.5.0', '>=')) {
149+
if (function_exists('json_last_error_msg')) {
150150
$message .= ': ' . json_last_error_msg();
151151
}
152152
throw new InvalidArgumentException($message);
153153
}
154154

155-
return json_decode($json);
155+
return (object) json_decode($json);
156156
}
157157
}

src/JsonSchema/Constraints/SchemaConstraint.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
3636
// passed schema
3737
$validationSchema = $schema;
3838
} elseif ($this->getTypeCheck()->propertyExists($element, $this->inlineSchemaProperty)) {
39-
$inlineSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty);
40-
if (is_array($inlineSchema)) {
41-
$inlineSchema = json_decode(json_encode($inlineSchema));
42-
}
4339
// inline schema
44-
$validationSchema = $inlineSchema;
40+
$validationSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty);
4541
} else {
4642
throw new InvalidArgumentException('no schema found to verify against');
4743
}
4844

45+
// cast array schemas to object
46+
if (is_array($validationSchema)) {
47+
$validationSchema = BaseConstraint::arrayToObjectRecursive($validationSchema);
48+
}
49+
4950
// validate schema against whatever is defined in $validationSchema->$schema. If no
5051
// schema is defined, assume self::DEFAULT_SCHEMA_SPEC (currently draft-04).
5152
if ($this->factory->getConfig(self::CHECK_MODE_VALIDATE_SCHEMA)) {

src/JsonSchema/SchemaStorage.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace JsonSchema;
44

5+
use JsonSchema\Constraints\BaseConstraint;
56
use JsonSchema\Entity\JsonPointer;
67
use JsonSchema\Exception\UnresolvableJsonPointerException;
78
use JsonSchema\Iterator\ObjectIterator;
@@ -51,6 +52,12 @@ public function addSchema($id, $schema = null)
5152
// schemas do not have an associated URI when passed via Validator::validate().
5253
$schema = $this->uriRetriever->retrieve($id);
5354
}
55+
56+
// cast array schemas to object
57+
if (is_array($schema)) {
58+
$schema = BaseConstraint::arrayToObjectRecursive($schema);
59+
}
60+
5461
$objectIterator = new ObjectIterator($schema);
5562
foreach ($objectIterator as $toResolveSchema) {
5663
if (property_exists($toResolveSchema, '$ref') && is_string($toResolveSchema->{'$ref'})) {

src/JsonSchema/Validator.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public function validate(&$value, $schema = null, $checkMode = null)
4545
// reset errors prior to validation
4646
$this->reset();
4747

48-
// make sure $schema is an object
49-
if (is_array($schema)) {
50-
$schema = self::arrayToObjectRecursive($schema);
51-
}
52-
5348
// set checkMode
5449
$initialCheckMode = $this->factory->getConfig();
5550
if ($checkMode !== null) {
@@ -60,7 +55,10 @@ public function validate(&$value, $schema = null, $checkMode = null)
6055
$this->factory->getSchemaStorage()->addSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI, $schema);
6156

6257
$validator = $this->factory->createInstanceFor('schema');
63-
$validator->check($value, $schema);
58+
$validator->check(
59+
$value,
60+
$this->factory->getSchemaStorage()->getSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI)
61+
);
6462

6563
$this->factory->setConfig($initialCheckMode);
6664

0 commit comments

Comments
 (0)