From 1fc1379028a2fb2e6675fcfbcaa2000e3e9f414b Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Wed, 11 Sep 2024 11:49:35 +0200 Subject: [PATCH] [Yaml] Add support for dumping `null` as an empty value by using the `Yaml::DUMP_NULL_AS_EMPTY` flag --- src/Symfony/Component/Yaml/CHANGELOG.md | 1 + src/Symfony/Component/Yaml/Dumper.php | 25 +++++--- src/Symfony/Component/Yaml/Inline.php | 10 +++- .../Component/Yaml/Tests/DumperTest.php | 57 +++++++++++++++++++ .../Component/Yaml/Tests/ParserTest.php | 27 +++++++++ src/Symfony/Component/Yaml/Yaml.php | 1 + 6 files changed, 110 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index 05b23cd7b06fe..284c49ed0ab62 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Deprecate parsing duplicate mapping keys whose value is `null` + * Add support for dumping `null` as an empty value by using the `Yaml::DUMP_NULL_AS_EMPTY` flag 7.1 --- diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index f8ea205a62e03..91b2ec29d5255 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -41,6 +41,15 @@ public function __construct(private int $indentation = 4) * @param int-mask-of $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string */ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags = 0): string + { + if ($flags & Yaml::DUMP_NULL_AS_EMPTY && $flags & Yaml::DUMP_NULL_AS_TILDE) { + throw new \InvalidArgumentException('The Yaml::DUMP_NULL_AS_EMPTY and Yaml::DUMP_NULL_AS_TILDE flags cannot be used together.'); + } + + return $this->doDump($input, $inline, $indent, $flags); + } + + private function doDump(mixed $input, int $inline = 0, int $indent = 0, int $flags = 0, int $nestingLevel = 0): string { $output = ''; $prefix = $indent ? str_repeat(' ', $indent) : ''; @@ -51,9 +60,9 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags } if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || !$input) { - $output .= $prefix.Inline::dump($input, $flags); + $output .= $prefix.Inline::dump($input, $flags, 0 === $nestingLevel); } elseif ($input instanceof TaggedValue) { - $output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix); + $output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix, $nestingLevel); } else { $dumpAsMap = Inline::isHash($input); @@ -105,10 +114,10 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags } if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) { - $output .= ' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n"; + $output .= ' '.$this->doDump($value->getValue(), $inline - 1, 0, $flags, $nestingLevel + 1)."\n"; } else { $output .= "\n"; - $output .= $this->dump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags); + $output .= $this->doDump($value->getValue(), $inline - 1, $dumpAsMap ? $indent + $this->indentation : $indent + 2, $flags, $nestingLevel + 1); } continue; @@ -126,7 +135,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $willBeInlined ? ' ' : "\n", - $this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags) + $this->doDump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags, $nestingLevel + 1) ).($willBeInlined ? "\n" : ''); } } @@ -134,7 +143,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags return $output; } - private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string + private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix, int $nestingLevel): string { $output = \sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag()); @@ -150,10 +159,10 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i } if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) { - return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n"; + return $output.' '.$this->doDump($value->getValue(), $inline - 1, 0, $flags, $nestingLevel + 1)."\n"; } - return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags); + return $output."\n".$this->doDump($value->getValue(), $inline - 1, $indent, $flags, $nestingLevel + 1); } private function getBlockIndentationIndicator(string $value): string diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 34ef66e654c5b..c310fe12b2829 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -100,7 +100,7 @@ public static function parse(string $value, int $flags = 0, array &$references = * * @throws DumpException When trying to dump PHP resource */ - public static function dump(mixed $value, int $flags = 0): string + public static function dump(mixed $value, int $flags = 0, bool $rootLevel = false): string { switch (true) { case \is_resource($value): @@ -138,7 +138,7 @@ public static function dump(mixed $value, int $flags = 0): string case \is_array($value): return self::dumpArray($value, $flags); case null === $value: - return self::dumpNull($flags); + return self::dumpNull($flags, $rootLevel); case true === $value: return 'true'; case false === $value: @@ -253,12 +253,16 @@ private static function dumpHashArray(array|\ArrayObject|\stdClass $value, int $ return \sprintf('{ %s }', implode(', ', $output)); } - private static function dumpNull(int $flags): string + private static function dumpNull(int $flags, bool $rootLevel = false): string { if (Yaml::DUMP_NULL_AS_TILDE & $flags) { return '~'; } + if (Yaml::DUMP_NULL_AS_EMPTY & $flags && !$rootLevel) { + return ''; + } + return 'null'; } diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index 24758b810445b..bb3ba62fa778a 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -216,6 +216,63 @@ public function testObjectSupportDisabledWithExceptions() $this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE); } + public function testDumpWithMultipleNullFlagsFormatsThrows() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The Yaml::DUMP_NULL_AS_EMPTY and Yaml::DUMP_NULL_AS_TILDE flags cannot be used together.'); + + $this->dumper->dump(['foo' => 'bar'], 0, 0, Yaml::DUMP_NULL_AS_EMPTY | Yaml::DUMP_NULL_AS_TILDE); + } + + public function testDumpNullAsEmptyInExpandedMapping() + { + $expected = "qux:\n foo: bar\n baz: \n"; + + $this->assertSame($expected, $this->dumper->dump(['qux' => ['foo' => 'bar', 'baz' => null]], 2, flags: Yaml::DUMP_NULL_AS_EMPTY)); + } + + public function testDumpNullAsEmptyWithObject() + { + $class = new \stdClass(); + $class->foo = 'bar'; + $class->baz = null; + + $this->assertSame("foo: bar\nbaz: \n", $this->dumper->dump($class, 2, flags: Yaml::DUMP_NULL_AS_EMPTY | Yaml::DUMP_OBJECT_AS_MAP)); + } + + public function testDumpNullAsEmptyDumpsWhenInInlineMapping() + { + $expected = "foo: \nqux: { foo: bar, baz: }\n"; + + $this->assertSame($expected, $this->dumper->dump(['foo' => null, 'qux' => ['foo' => 'bar', 'baz' => null]], 1, flags: Yaml::DUMP_NULL_AS_EMPTY)); + } + + public function testDumpNullAsEmptyDumpsNestedMaps() + { + $expected = "foo: \nqux:\n foo: bar\n baz: \n"; + + $this->assertSame($expected, $this->dumper->dump(['foo' => null, 'qux' => ['foo' => 'bar', 'baz' => null]], 10, flags: Yaml::DUMP_NULL_AS_EMPTY)); + } + + public function testDumpNullAsEmptyInExpandedSequence() + { + $expected = "qux:\n - foo\n - \n - bar\n"; + + $this->assertSame($expected, $this->dumper->dump(['qux' => ['foo', null, 'bar']], 2, flags: Yaml::DUMP_NULL_AS_EMPTY)); + } + + public function testDumpNullAsEmptyWhenInInlineSequence() + { + $expected = "foo: \nqux: [foo, , bar]\n"; + + $this->assertSame($expected, $this->dumper->dump(['foo' => null, 'qux' => ['foo', null, 'bar']], 1, flags: Yaml::DUMP_NULL_AS_EMPTY)); + } + + public function testDumpNullAsEmptyAtRoot() + { + $this->assertSame('null', $this->dumper->dump(null, 2, flags: Yaml::DUMP_NULL_AS_EMPTY)); + } + /** * @dataProvider getEscapeSequences */ diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index fad946946b503..3850f041042f3 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -52,6 +52,33 @@ public function testTopLevelNull() $this->assertSameData($expected, $data); } + public function testEmptyValueInExpandedMappingIsSupported() + { + $yml = <<<'YAML' +foo: + bar: + baz: qux +YAML; + + $data = $this->parser->parse($yml); + $expected = ['foo' => ['bar' => null, 'baz' => 'qux']]; + $this->assertSameData($expected, $data); + } + + public function testEmptyValueInExpandedSequenceIsSupported() + { + $yml = <<<'YAML' +foo: + - bar + - + - baz +YAML; + + $data = $this->parser->parse($yml); + $expected = ['foo' => ['bar', null, 'baz']]; + $this->assertSameData($expected, $data); + } + public function testTaggedValueTopLevelNumber() { $yml = '!number 5'; diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index 36b451988017a..03395b9770f3e 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -35,6 +35,7 @@ class Yaml public const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024; public const DUMP_NULL_AS_TILDE = 2048; public const DUMP_NUMERIC_KEY_AS_STRING = 4096; + public const DUMP_NULL_AS_EMPTY = 8192; /** * Parses a YAML file into a PHP value.