From 0bcf3eb14b02eea903701788707607053c90a662 Mon Sep 17 00:00:00 2001 From: Erayd Date: Tue, 13 Feb 2018 18:37:38 +1300 Subject: [PATCH 1/3] Subschema should be dereferenced prior to applying the default value Note that siblings of $ref are ignored, per the spec. --- tests/Constraints/DefaultPropertiesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Constraints/DefaultPropertiesTest.php b/tests/Constraints/DefaultPropertiesTest.php index bcf90b2c..8811f893 100644 --- a/tests/Constraints/DefaultPropertiesTest.php +++ b/tests/Constraints/DefaultPropertiesTest.php @@ -121,7 +121,7 @@ public function getValidTests() ), array(// #15 infinite recursion via $ref (object) '{}', - '{"properties":{"propertyOne": {"$ref": "#","default": {}}}}', + '{"properties":{"propertyOne": {"$ref": "#","default": "valueOne"}}, "default": {}}', '{"propertyOne":{}}' ), array(// #16 infinite recursion via $ref (array) From 91f5891f006a0e794760d4ab604e93e97c3b1efa Mon Sep 17 00:00:00 2001 From: Erayd Date: Tue, 13 Feb 2018 18:38:46 +1300 Subject: [PATCH 2/3] Resolve property definition subschemas before checking for a default --- src/JsonSchema/Constraints/UndefinedConstraint.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index d88c5361..bea668a6 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -241,6 +241,7 @@ protected function applyDefaultValues(&$value, $schema, $path) if (isset($schema->properties) && LooseTypeCheck::isObject($value)) { // $value is an object or assoc array, and properties are defined - treat as an object foreach ($schema->properties as $currentProperty => $propertyDefinition) { + $propertyDefinition = $this->factory->getSchemaStorage()->resolveRefSchema($propertyDefinition); if ( !LooseTypeCheck::propertyExists($value, $currentProperty) && property_exists($propertyDefinition, 'default') From 8a5ac14157709b0bfb877a6eb85d9cc8d3e0a681 Mon Sep 17 00:00:00 2001 From: Erayd Date: Tue, 13 Feb 2018 18:49:43 +1300 Subject: [PATCH 3/3] Also dereference array definitions prior to default value check --- src/JsonSchema/Constraints/UndefinedConstraint.php | 1 + tests/Constraints/DefaultPropertiesTest.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index bea668a6..3b0c8eb0 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -265,6 +265,7 @@ protected function applyDefaultValues(&$value, $schema, $path) } // $value is an array, and items are defined - treat as plain array foreach ($items as $currentItem => $itemDefinition) { + $itemDefinition = $this->factory->getSchemaStorage()->resolveRefSchema($itemDefinition); if ( !array_key_exists($currentItem, $value) && property_exists($itemDefinition, 'default') diff --git a/tests/Constraints/DefaultPropertiesTest.php b/tests/Constraints/DefaultPropertiesTest.php index 8811f893..cc5eb909 100644 --- a/tests/Constraints/DefaultPropertiesTest.php +++ b/tests/Constraints/DefaultPropertiesTest.php @@ -126,7 +126,7 @@ public function getValidTests() ), array(// #16 infinite recursion via $ref (array) '[]', - '{"items":[{"$ref":"#","default":[]}]}', + '{"items":[{"$ref":"#","default":"valueOne"}], "default": []}', '[[]]' ), array(// #17 default top value does not overwrite defined null