Skip to content

[Yaml] Add support for dumping null as an empty string by using the Yaml::DUMP_NULL_AS_EMPTY flag #58232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate parsing duplicate mapping keys whose value is `null`
* Add support for dumping `null` as an empty string by using the `Yaml::DUMP_NULL_AS_EMPTY` flag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think that „empty string“ is the correct term here. An empty string would be '' or "".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"As an empty value" maybe?


7.1
---
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public function __construct(int $indentation = 4)
*/
public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags = 0): string
{
if ($flags & Yaml::DUMP_NULL_AS_TILDE && $flags & Yaml::DUMP_NULL_AS_EMPTY) {
throw new \InvalidArgumentException('The Yaml::DUMP_NULL_AS_TILDE and Yaml::DUMP_NULL_AS_EMPTY flags cannot be used together.');
}

$output = '';
$prefix = $indent ? str_repeat(' ', $indent) : '';
$dumpObjectAsInlineMap = true;
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ private static function dumpNull(int $flags): string
return '~';
}

if (Yaml::DUMP_NULL_AS_EMPTY & $flags) {
return '';
}

return 'null';
}

Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,11 @@ public function testDumpNullAsTilde()
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
}

public function testDumpNullAsEmpty()
{
$this->assertSame('{ foo: }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_EMPTY));
}

/**
* @dataProvider getNumericKeyData
*/
Expand Down Expand Up @@ -1012,6 +1017,14 @@ private function assertSameData($expected, $actual)
var_export($actual, true)
);
}

public function testUseDumpAsTildeAndDumpAsEmptyFlagsTogether()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The Yaml::DUMP_NULL_AS_TILDE and Yaml::DUMP_NULL_AS_EMPTY flags cannot be used together.');

$this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE | Yaml::DUMP_NULL_AS_EMPTY);
}
}

class A
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading