Skip to content

Commit

Permalink
bug #54372 [Config] Fix YamlReferenceDumper handling of array examp…
Browse files Browse the repository at this point in the history
…les (MatTheCat)

This PR was merged into the 5.4 branch.

Discussion
----------

[Config] Fix `YamlReferenceDumper` handling of array examples

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #54326
| License       | MIT

#54326 was based on a wrong branch.

It seems example arrays were considered as default values in some cases. Now that they are displayed as comments, this PR ensures the default value makes sense.

Commits
-------

4d4b552 [Config] Fix `YamlReferenceDumper` handling of array examples
  • Loading branch information
fabpot committed Mar 23, 2024
2 parents afc658d + 4d4b552 commit 7ba3d8e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\Config\Definition\NodeInterface;
use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Config\Definition\ScalarNode;
use Symfony\Component\Config\Definition\VariableNode;
use Symfony\Component\Yaml\Inline;

/**
Expand Down Expand Up @@ -90,19 +89,12 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul
$children = $this->getPrototypeChildren($node);
}

if (!$children) {
if ($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue())) {
$default = '';
} elseif (!\is_array($example)) {
$default = '[]';
}
if (!$children && !($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue()))) {
$default = '[]';
}
} elseif ($node instanceof EnumNode) {
$comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
$default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
} elseif (VariableNode::class === \get_class($node) && \is_array($example)) {
// If there is an array example, we are sure we dont need to print a default value
$default = '';
} else {
$default = '~';

Expand Down Expand Up @@ -170,7 +162,7 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul

$this->writeLine('# '.$message.':', $depth * 4 + 4);

$this->writeArray(array_map([Inline::class, 'dump'], $example), $depth + 1);
$this->writeArray(array_map([Inline::class, 'dump'], $example), $depth + 1, true);
}

if ($children) {
Expand All @@ -191,7 +183,7 @@ private function writeLine(string $text, int $indent = 0)
$this->reference .= sprintf($format, $text)."\n";
}

private function writeArray(array $array, int $depth)
private function writeArray(array $array, int $depth, bool $asComment = false)
{
$isIndexed = array_values($array) === $array;

Expand All @@ -202,14 +194,16 @@ private function writeArray(array $array, int $depth)
$val = $value;
}

$prefix = $asComment ? '# ' : '';

if ($isIndexed) {
$this->writeLine('- '.$val, $depth * 4);
$this->writeLine($prefix.'- '.$val, $depth * 4);
} else {
$this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
$this->writeLine(sprintf('%s%-20s %s', $prefix, $key.':', $val), $depth * 4);
}

if (\is_array($value)) {
$this->writeArray($value, $depth + 1);
$this->writeArray($value, $depth + 1, $asComment);
}
}
}
Expand Down
Expand Up @@ -109,6 +109,8 @@ enum=""
</pipou>
<array-with-array-example-and-no-default-value />
</config>
EOL
Expand Down
Expand Up @@ -114,11 +114,11 @@ enum: ~ # One of "this"; "that"
# which should be indented
child3: ~ # Example: 'example setting'
scalar_prototyped: []
variable:
variable: ~
# Examples:
- foo
- bar
# - foo
# - bar
parameters:
# Prototype: Parameter name
Expand All @@ -142,6 +142,11 @@ enum: ~ # One of "this"; "that"
# Prototype
name: []
array_with_array_example_and_no_default_value: []
# Examples:
# - foo
# - bar
custom_node: true
EOL;
Expand Down
Expand Up @@ -96,6 +96,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode('array_with_array_example_and_no_default_value')
->example(['foo', 'bar'])
->end()
->append(new CustomNodeDefinition('acme'))
->end()
;
Expand Down

0 comments on commit 7ba3d8e

Please sign in to comment.