Commit d2412ba4 authored by webchick's avatar webchick

Issue #1907170 by pwolanin, jhedstrom, alexpott, Heine: Very simple config files can't be read

parent 4d8bd967
......@@ -8,7 +8,8 @@
namespace Drupal\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Symfony\Component\Yaml\Yaml as Symfony;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
/**
* Default serialization for YAML using the Symfony component.
......@@ -20,7 +21,9 @@ class Yaml implements SerializationInterface {
*/
public static function encode($data) {
try {
return Symfony::dump($data, PHP_INT_MAX, 2, TRUE);
$yaml = new Dumper();
$yaml->setIndentation(2);
return $yaml->dump($data, PHP_INT_MAX, 0, TRUE, FALSE);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
......@@ -32,7 +35,10 @@ public static function encode($data) {
*/
public static function decode($raw) {
try {
return Symfony::parse($raw, TRUE);
$yaml = new Parser();
// Make sure we have a single trailing newline. A very simple config like
// 'foo: bar' with no newline will fail to parse otherwise.
return $yaml->parse($raw, TRUE, FALSE);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
......
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Serialization\YamlTest.
*/
namespace Drupal\Tests\Component\Serialization;
use Drupal\Component\Serialization\Yaml;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\Serialization\Yaml
* @group Serialization
*/
class YamlTest extends UnitTestCase {
/**
* @covers ::decode
*/
public function testDecode() {
// Test that files without line break endings are properly interpreted.
$yaml = 'foo: bar';
$expected = array(
'foo' => 'bar',
);
$this->assertSame($expected, Yaml::decode($yaml));
$yaml .= "\n";
$this->assertSame($expected, Yaml::decode($yaml));
$yaml .= "\n";
$this->assertSame($expected, Yaml::decode($yaml));
$yaml = "{}\n";
$expected = array();
$this->assertSame($expected, Yaml::decode($yaml));
$yaml = '';
$this->assertNULL(Yaml::decode($yaml));
$yaml .= "\n";
$this->assertNULL(Yaml::decode($yaml));
$yaml .= "\n";
$this->assertNULL(Yaml::decode($yaml));
}
/**
* @covers ::encode
*/
public function testEncode() {
$decoded = array(
'foo' => 'bar',
);
$this->assertSame('foo: bar' . "\n", Yaml::encode($decoded));
}
/**
* @covers ::getFileExtension
*/
public function testGetFileExtension() {
$this->assertEquals('yml', Yaml::getFileExtension());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment