Skip to content

[Yaml] Yaml::dump() inserts unneeded newlines for arrays #15782

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
johnknl opened this issue Sep 14, 2015 · 6 comments
Closed

[Yaml] Yaml::dump() inserts unneeded newlines for arrays #15782

johnknl opened this issue Sep 14, 2015 · 6 comments
Labels

Comments

@johnknl
Copy link

johnknl commented Sep 14, 2015

I'm sure I'm not the first to notice, and maybe I didn't search well enough, but couldn't find an existing issue regarding this.

Yaml::dump() will produce arrays like this:

parameters:
  -
    name: username
    in: path
    description: 'name that need to be deleted'
    required: true
    type: string
  -
    in: body
    name: body
    description: 'Updated user object'
    required: true
    schema:
      $ref: '#/definitions/User'

Instead of the more readable and preferred:

parameters:
  - name: username
    in: path
    description: 'name that need to be deleted'
    required: true
    type: string
  - in: body
    name: body
    description: 'Updated user object'
    required: true
    schema:
      $ref: '#/definitions/User'

I first noticed this using "incenteev/composer-parameter-handler" (no mention of the issue there either), which will produce this:

# This file is auto-generated during the composer install
imports:
    -
        resource: parameters_base.yml

From a parameters.yml.dist which looks like this:

imports:
    - { resource: parameters_base.yml }

Obviously the inlining is configurable in Yaml:dump, the extra newline, as far as I can tell, is not.

@jakzal jakzal added the Yaml label Sep 29, 2015
@fabpot
Copy link
Member

fabpot commented Oct 5, 2015

Both are valid, so there is no bug.

@fabpot fabpot closed this as completed Oct 5, 2015
@johnknl
Copy link
Author

johnknl commented Oct 5, 2015

Euhr.. Nobody said it was a bug, I said "more readable and preferred". Call it an "improvement".

@ddanielou
Copy link

ddanielou commented Dec 9, 2020

Revisiting this after a few years… A quick and dirty workaround is to preg_replace('/\-\n\s+/', '- ', Yaml::dump($array, 6)). I used 6 as the inline level, adjust accordingly. Also, can't guarantee it won't mess with more complicated structures.

@adriangronau
Copy link

Adding to the dirty workaround, using an indent of two spaces aligns the properties of the array item nicely: preg_replace('/\-\n\s+/', '- ', Yaml::dump($array, 6, 2))

@donquixote
Copy link
Contributor

donquixote commented Jun 17, 2022

Another version, which may be more robust in edge cases, e.g. if you have text that would cause false positives in the regex match.

  function ymlDump(mixed $data, $inline = 99, $indent = 2, $flags = Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK): string {
    $marker = \uniqid('_', true);
    $dumper = new class($marker) extends Dumper {
      public function __construct(
        private string $marker,
      ) {
        parent::__construct(2);
      }
      public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string {
        return $this->marker . parent::dump($input, $inline, $indent, $flags);
      }
    };
    $yml = $dumper->dump($data, $inline, $indent, $flags);
    $yml = \preg_replace(
      sprintf("@-\n%s +@", \preg_quote($marker, '@')),
      '- ',
      $yml,
    );
    $yml = \str_replace($marker, '', $yml);
    return $yml;
  }

@donquixote
Copy link
Contributor

donquixote commented Jun 21, 2022

Note that this only works well if indentation = 2.
Otherwise it would look inconsistent.

map:
    submap:
        a: A
        b: B
list:  # which of these is better?
    - a: A
      b: B
    -   a: A
        b: B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants