Skip to content

Do not ignore validation of referenced schemas #117

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

Merged
merged 2 commits into from
May 12, 2021
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: 4 additions & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@ private function processObject($data, Context $options, $path, $result = null)
$ref->setImported($refResult);
return $refResult;
} catch (InvalidValue $exception) {
if ($this->objectItemClass === 'Swaggest\JsonSchema\Schema') {
throw $exception;
}

$ref->unsetImported();
$skipValidation = $options->skipValidation;
$options->skipValidation = true;
Expand Down
77 changes: 77 additions & 0 deletions tests/src/PHPUnit/Suite/Issue116Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Swaggest\JsonSchema\Tests\PHPUnit\Suite;

use Swaggest\JsonSchema\InvalidValue;
use Swaggest\JsonSchema\Schema;

class Issue116Test extends \PHPUnit_Framework_TestCase
{
public function testIssue()
{
$schemaData = json_decode('{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Parent",
"type": "object",
"required": [
"title",
"child"
],
"properties": {
"title": {
"type": "string"
},
"second_prop": {
"type": "uri"
},
"child": {
"type": "object",
"required": [
"title"
],
"properties": {
"title": {
"type": "string"
}
}
}
}
}');

$exceptionCaught = false;
try {
Schema::import($schemaData);
} catch (InvalidValue $e) {
$exceptionCaught = true;

$this->assertEquals('No valid results for anyOf {
0: Enum failed, enum: ["array","boolean","integer","null","number","object","string"], data: "uri" at #->properties:properties->additionalProperties:second_prop->properties:type->anyOf[0]
1: Array expected, "uri" received at #->properties:properties->additionalProperties:second_prop->properties:type->anyOf[1]
} at #->properties:properties->additionalProperties:second_prop->properties:type', $e->getMessage());
}

$this->assertTrue($exceptionCaught);
}


public function testIssueFile()
{
$path = __DIR__ . '/schema.json';
$path = substr($path, strlen(getcwd()) + 1);

$exceptionCaught = false;
try {
Schema::import($path);
} catch (InvalidValue $e) {
$exceptionCaught = true;

$this->assertEquals('No valid results for anyOf {
0: Enum failed, enum: ["array","boolean","integer","null","number","object","string"], data: "uri" at #->$ref:tests/src/PHPUnit/Suite/schema.json->properties:properties->additionalProperties:second_prop->properties:type->anyOf[0]
1: Array expected, "uri" received at #->$ref:tests/src/PHPUnit/Suite/schema.json->properties:properties->additionalProperties:second_prop->properties:type->anyOf[1]
} at #->$ref:tests/src/PHPUnit/Suite/schema.json->properties:properties->additionalProperties:second_prop->properties:type', $e->getMessage());
}

$this->assertTrue($exceptionCaught);

}
}
28 changes: 28 additions & 0 deletions tests/src/PHPUnit/Suite/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Parent",
"type": "object",
"required": [
"title",
"child"
],
"properties": {
"title": {
"type": "string"
},
"second_prop": {
"type": "uri"
},
"child": {
"type": "object",
"required": [
"title"
],
"properties": {
"title": {
"type": "string"
}
}
}
}
}