1
0
Fork 0

Fix various issues with regard to schema conformity when modifying json, fixes #9515

pull/9517/head
Jordi Boggiano 2020-11-22 14:26:07 +01:00
parent 480a6439fd
commit c7da7e55c9
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 26 additions and 4 deletions

View File

@ -14,6 +14,7 @@ namespace Composer\Config;
use Composer\Json\JsonFile; use Composer\Json\JsonFile;
use Composer\Json\JsonManipulator; use Composer\Json\JsonManipulator;
use Composer\Json\JsonValidationException;
use Composer\Util\Silencer; use Composer\Util\Silencer;
/** /**
@ -251,14 +252,35 @@ class JsonConfigSource implements ConfigSourceInterface
$this->arrayUnshiftRef($args, $config); $this->arrayUnshiftRef($args, $config);
call_user_func_array($fallback, $args); call_user_func_array($fallback, $args);
// avoid ending up with arrays for keys that should be objects // avoid ending up with arrays for keys that should be objects
foreach (array('require', 'require-dev', 'conflict', 'provide', 'replace', 'suggest', 'config', 'autoload', 'autoload-dev') as $linkType) { foreach (array('require', 'require-dev', 'conflict', 'provide', 'replace', 'suggest', 'config', 'autoload', 'autoload-dev', 'scripts', 'scripts-descriptions', 'support', ) as $prop) {
if (isset($config[$linkType]) && $config[$linkType] === array()) { if (isset($config[$prop]) && $config[$prop] === array()) {
$config[$linkType] = new \stdClass; $config[$prop] = new \stdClass;
}
}
foreach (array('psr-0', 'psr-4') as $prop) {
if (isset($config['autoload'][$prop]) && $config['autoload'][$prop] === array()) {
$config['autoload'][$prop] = new \stdClass;
}
if (isset($config['autoload-dev'][$prop]) && $config['autoload-dev'][$prop] === array()) {
$config['autoload-dev'][$prop] = new \stdClass;
}
}
foreach (array('platform', 'http-basic', 'bearer', 'gitlab-token', 'gitlab-oauth', 'github-oauth', 'preferred-install') as $prop) {
if (isset($config['config'][$prop]) && $config['config'][$prop] === array()) {
$config['config'][$prop] = new \stdClass;
} }
} }
$this->file->write($config); $this->file->write($config);
} }
try {
$this->file->validateSchema(JsonFile::LAX_SCHEMA);
} catch (JsonValidationException $e) {
// restore contents to the original state
file_put_contents($this->file->getPath(), $contents);
throw new \RuntimeException('Failed to update composer.json with a valid format, reverting to the original content. Please report an issue to us with details (command you run and a copy of your composer.json).', 0, $e);
}
if ($newFile) { if ($newFile) {
Silencer::call('chmod', $this->file->getPath(), 0600); Silencer::call('chmod', $this->file->getPath(), 0600);
} }

View File

@ -492,7 +492,7 @@ class JsonManipulator
return $this->removeMainKey($key); return $this->removeMainKey($key);
} }
return false; return true;
} }
public function format($data, $depth = 0) public function format($data, $depth = 0)