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

[BUGFIX] Cast empty schema arrays to object #409

Merged
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
4 changes: 2 additions & 2 deletions src/JsonSchema/Constraints/BaseConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ public static function arrayToObjectRecursive($array)
$json = json_encode($array);
if (json_last_error() !== \JSON_ERROR_NONE) {
$message = 'Unable to encode schema array as JSON';
if (version_compare(phpversion(), '5.5.0', '>=')) {
if (function_exists('json_last_error_msg')) {
$message .= ': ' . json_last_error_msg();
}
throw new InvalidArgumentException($message);
}

return json_decode($json);
return (object) json_decode($json);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will only do a shallow cast. Is that intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's deliberate. The JSON functions are already handling the deep cast - my understanding of this bug is that it's a shallow-cast problem only (that's the way it was reported).

Copy link

Choose a reason for hiding this comment

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

Yes, it is. json_decode already does the deep cast.

}
}
11 changes: 6 additions & 5 deletions src/JsonSchema/Constraints/SchemaConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
// passed schema
$validationSchema = $schema;
} elseif ($this->getTypeCheck()->propertyExists($element, $this->inlineSchemaProperty)) {
$inlineSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty);
if (is_array($inlineSchema)) {
$inlineSchema = json_decode(json_encode($inlineSchema));
}
// inline schema
$validationSchema = $inlineSchema;
$validationSchema = $this->getTypeCheck()->propertyGet($element, $this->inlineSchemaProperty);
} else {
throw new InvalidArgumentException('no schema found to verify against');
}

// cast array schemas to object
if (is_array($validationSchema)) {
$validationSchema = BaseConstraint::arrayToObjectRecursive($validationSchema);
}

// validate schema against whatever is defined in $validationSchema->$schema. If no
// schema is defined, assume self::DEFAULT_SCHEMA_SPEC (currently draft-04).
if ($this->factory->getConfig(self::CHECK_MODE_VALIDATE_SCHEMA)) {
Expand Down
7 changes: 7 additions & 0 deletions src/JsonSchema/SchemaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JsonSchema;

use JsonSchema\Constraints\BaseConstraint;
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Exception\UnresolvableJsonPointerException;
use JsonSchema\Iterator\ObjectIterator;
Expand Down Expand Up @@ -51,6 +52,12 @@ public function addSchema($id, $schema = null)
// schemas do not have an associated URI when passed via Validator::validate().
$schema = $this->uriRetriever->retrieve($id);
}

// cast array schemas to object
if (is_array($schema)) {
$schema = BaseConstraint::arrayToObjectRecursive($schema);
}

$objectIterator = new ObjectIterator($schema);
foreach ($objectIterator as $toResolveSchema) {
if (property_exists($toResolveSchema, '$ref') && is_string($toResolveSchema->{'$ref'})) {
Expand Down
10 changes: 4 additions & 6 deletions src/JsonSchema/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public function validate(&$value, $schema = null, $checkMode = null)
// reset errors prior to validation
$this->reset();

// make sure $schema is an object
if (is_array($schema)) {
$schema = self::arrayToObjectRecursive($schema);
}

// set checkMode
$initialCheckMode = $this->factory->getConfig();
if ($checkMode !== null) {
Expand All @@ -60,7 +55,10 @@ public function validate(&$value, $schema = null, $checkMode = null)
$this->factory->getSchemaStorage()->addSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI, $schema);

$validator = $this->factory->createInstanceFor('schema');
$validator->check($value, $schema);
$validator->check(
$value,
$this->factory->getSchemaStorage()->getSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI)
);

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

Expand Down