From 7366b8e362005fe5b3eeae49e7aa84817d895612 Mon Sep 17 00:00:00 2001 From: adlacruzes Date: Thu, 3 Jun 2021 20:15:35 +0200 Subject: [PATCH 1/2] Fix JsonFile when using custom json schema with no "name" and "description" properties --- src/Composer/Json/JsonFile.php | 4 ++- tests/Composer/Test/Json/JsonFileTest.php | 32 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 07a6f922f..95ae95ea2 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -180,6 +180,7 @@ class JsonFile { $content = file_get_contents($this->path); $data = json_decode($content); + $requiredSchemaData = array(); if (null === $data && 'null' !== $content) { self::validateSyntax($content, $this->path); @@ -187,6 +188,7 @@ class JsonFile if (null === $schemaFile) { $schemaFile = __DIR__ . self::COMPOSER_SCHEMA_PATH; + $requiredSchemaData = array('name', 'description'); } // Prepend with file:// only when not using a special schema already (e.g. in the phar) @@ -198,7 +200,7 @@ class JsonFile if ($schema !== self::LAX_SCHEMA) { $schemaData->additionalProperties = false; - $schemaData->required = array('name', 'description'); + $schemaData->required = $requiredSchemaData; } $validator = new Validator(); diff --git a/tests/Composer/Test/Json/JsonFileTest.php b/tests/Composer/Test/Json/JsonFileTest.php index eb0646ae4..d93b0414d 100644 --- a/tests/Composer/Test/Json/JsonFileTest.php +++ b/tests/Composer/Test/Json/JsonFileTest.php @@ -207,6 +207,38 @@ class JsonFileTest extends TestCase unlink($file); } + public function testCustomSchemaValidationLax() + { + $file = tempnam(sys_get_temp_dir(), 'c'); + file_put_contents($file, '{ "custom": "property", "another custom": "property" }'); + + $schema = tempnam(sys_get_temp_dir(), 'c'); + file_put_contents($schema, '{ "properties": { "custom": { "type": "string" }}}'); + + $json = new JsonFile($file); + + $this->assertTrue($json->validateSchema(JsonFile::LAX_SCHEMA, $schema)); + + unlink($file); + unlink($schema); + } + + public function testCustomSchemaValidationStrict() + { + $file = tempnam(sys_get_temp_dir(), 'c'); + file_put_contents($file, '{ "custom": "property" }'); + + $schema = tempnam(sys_get_temp_dir(), 'c'); + file_put_contents($schema, '{ "properties": { "custom": { "type": "string" }}}'); + + $json = new JsonFile($file); + + $this->assertTrue($json->validateSchema(JsonFile::STRICT_SCHEMA, $schema)); + + unlink($file); + unlink($schema); + } + public function testParseErrorDetectMissingCommaMultiline() { $json = '{ From 1bfec451e26be283b99f0168a4151c59274f7f8f Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 4 Jun 2021 08:05:42 +0200 Subject: [PATCH 2/2] Make sure the LAX_SCHEMA handling disables required/additionalProperties as it used to do but strict schema requires properties only for the composer schema --- src/Composer/Json/JsonFile.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 95ae95ea2..1b662fa91 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -180,15 +180,15 @@ class JsonFile { $content = file_get_contents($this->path); $data = json_decode($content); - $requiredSchemaData = array(); if (null === $data && 'null' !== $content) { self::validateSyntax($content, $this->path); } + $isComposerSchemaFile = false; if (null === $schemaFile) { + $isComposerSchemaFile = true; $schemaFile = __DIR__ . self::COMPOSER_SCHEMA_PATH; - $requiredSchemaData = array('name', 'description'); } // Prepend with file:// only when not using a special schema already (e.g. in the phar) @@ -198,16 +198,17 @@ class JsonFile $schemaData = (object) array('$ref' => $schemaFile); - if ($schema !== self::LAX_SCHEMA) { + if ($schema === self::LAX_SCHEMA) { + $schemaData->additionalProperties = true; + $schemaData->required = array(); + } elseif ($schema === self::STRICT_SCHEMA && $isComposerSchemaFile) { $schemaData->additionalProperties = false; - $schemaData->required = $requiredSchemaData; + $schemaData->required = array('name', 'description'); } $validator = new Validator(); $validator->check($data, $schemaData); - // TODO add more validation like check version constraints and such, perhaps build that into the arrayloader? - if (!$validator->isValid()) { $errors = array(); foreach ((array) $validator->getErrors() as $error) {