Skip to content

Commit f92567a

Browse files
authored
Do not ignore validation of referenced schemas (#117)
1 parent fa87be2 commit f92567a

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/Schema.php

+4
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,10 @@ private function processObject($data, Context $options, $path, $result = null)
736736
$ref->setImported($refResult);
737737
return $refResult;
738738
} catch (InvalidValue $exception) {
739+
if ($this->objectItemClass === 'Swaggest\JsonSchema\Schema') {
740+
throw $exception;
741+
}
742+
739743
$ref->unsetImported();
740744
$skipValidation = $options->skipValidation;
741745
$options->skipValidation = true;
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit\Suite;
4+
5+
use Swaggest\JsonSchema\InvalidValue;
6+
use Swaggest\JsonSchema\Schema;
7+
8+
class Issue116Test extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testIssue()
11+
{
12+
$schemaData = json_decode('{
13+
"$schema": "https://json-schema.org/draft/2020-12/schema",
14+
"title": "Parent",
15+
"type": "object",
16+
"required": [
17+
"title",
18+
"child"
19+
],
20+
"properties": {
21+
"title": {
22+
"type": "string"
23+
},
24+
"second_prop": {
25+
"type": "uri"
26+
},
27+
"child": {
28+
"type": "object",
29+
"required": [
30+
"title"
31+
],
32+
"properties": {
33+
"title": {
34+
"type": "string"
35+
}
36+
}
37+
}
38+
}
39+
}');
40+
41+
$exceptionCaught = false;
42+
try {
43+
Schema::import($schemaData);
44+
} catch (InvalidValue $e) {
45+
$exceptionCaught = true;
46+
47+
$this->assertEquals('No valid results for anyOf {
48+
0: Enum failed, enum: ["array","boolean","integer","null","number","object","string"], data: "uri" at #->properties:properties->additionalProperties:second_prop->properties:type->anyOf[0]
49+
1: Array expected, "uri" received at #->properties:properties->additionalProperties:second_prop->properties:type->anyOf[1]
50+
} at #->properties:properties->additionalProperties:second_prop->properties:type', $e->getMessage());
51+
}
52+
53+
$this->assertTrue($exceptionCaught);
54+
}
55+
56+
57+
public function testIssueFile()
58+
{
59+
$path = __DIR__ . '/schema.json';
60+
$path = substr($path, strlen(getcwd()) + 1);
61+
62+
$exceptionCaught = false;
63+
try {
64+
Schema::import($path);
65+
} catch (InvalidValue $e) {
66+
$exceptionCaught = true;
67+
68+
$this->assertEquals('No valid results for anyOf {
69+
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]
70+
1: Array expected, "uri" received at #->$ref:tests/src/PHPUnit/Suite/schema.json->properties:properties->additionalProperties:second_prop->properties:type->anyOf[1]
71+
} at #->$ref:tests/src/PHPUnit/Suite/schema.json->properties:properties->additionalProperties:second_prop->properties:type', $e->getMessage());
72+
}
73+
74+
$this->assertTrue($exceptionCaught);
75+
76+
}
77+
}

tests/src/PHPUnit/Suite/schema.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "Parent",
4+
"type": "object",
5+
"required": [
6+
"title",
7+
"child"
8+
],
9+
"properties": {
10+
"title": {
11+
"type": "string"
12+
},
13+
"second_prop": {
14+
"type": "uri"
15+
},
16+
"child": {
17+
"type": "object",
18+
"required": [
19+
"title"
20+
],
21+
"properties": {
22+
"title": {
23+
"type": "string"
24+
}
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)