8000 The yml format is not supported by the YamlEncoder · Issue #28768 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

The yml format is not supported by the YamlEncoder #28768

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
soullivaneuh opened this issue Oct 8, 2018 · 6 comments
Closed

The yml format is not supported by the YamlEncoder #28768

soullivaneuh opened this issue Oct 8, 2018 · 6 comments

Comments

@soullivaneuh
Copy link
Contributor

Symfony version(s) affected: 4.1.6

Description

The YamlEncoder currently support only the yaml format:

AFAIK, the .yml file format is also accepted for Yaml format. IMO, it should be added to the support of this encoder.

@soullivaneuh
Copy link
Contributor Author

Possible workaround with service decoration:

namespace App\Serializer\Encoder;

use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;

/**
 * @see https://github.com/symfony/symfony/issues/28768
 */
final class YamlEncoder implements EncoderInterface, DecoderInterface
{
    private const FORMAT = 'yml';

    /**
     * @var \Symfony\Component\Serializer\Encoder\YamlEncoder
     */
    private $encoder;

    public function __construct(\Symfony\Component\Serializer\Encoder\YamlEncoder $encoder)
    {
        $this->encoder = $encoder;
    }

    /**
     * {@inheritdoc}
     */
    public function encode($data, $format, array $context = array())
    {
        return $this->encoder->encode($data, $format, $context);
    }

    /**
     * {@inheritdoc}
     */
    public function supportsEncoding($format)
    {
        return $this->encoder->supportsEncoding($format) || self::FORMAT === $format;
    }

    /**
     * {@inheritdoc}
     */
    public function decode($data, $format, array $context = array())
    {
        return $this->encoder->decode($data, $format, $context);
    }

    /**
     * {@inheritdoc}
     */
    public function supportsDecoding($format)
    {
        return $this->encoder->supportsDecoding($format) || self::FORMAT === $format;
    }
}
services:
    App\Serializer\Encoder\YamlEncoder:
        decorates: serializer.encoder.yaml

@javiereguiluz
Copy link
Member

This looks too restrictive to me too. I think we should support .yml too. Thanks!

@dunglas
Copy link
Member
dunglas commented Oct 8, 2018

AFAIK, the yml extension is discouraged but can be used. But I'm not aware of the yml string used as format name.

@soullivaneuh
Copy link
Contributor Author

But I'm not aware of the yml string used as format name.

This is not the direct issue, but API system like FOSRest will use it. If you call domain.com/api/user.yml, yml will be send as format.

Am I wrong?

@dunglas
Copy link
Member
dunglas commented Oct 9, 2018

API Platform does it too. But in this case, it's better to add an adapter in FOSRest/API Platform to pass yaml as format to the Serializer if the extension yml is passed. WDYT?

@nicolas-grekas
Copy link
Member

Yml is so common and non-ambiguous that I'd be fine supporting it out of the box personally.

@fabpot fabpot closed this as completed Oct 11, 2018
fabpot added a commit that referenced this issue Oct 11, 2018
This PR was merged into the 4.2-dev branch.

Discussion
----------

YamlEncoder handle yml format

| Q             | A
| ------------- | ---
| Branch?       | ?
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28768
| License       | MIT

`Symfony\Component\Serializer\Encoder\YamlEncoder` now handle the `yml` format too

```
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\YamlEncoder;

$serializer = new Serializer([], [new YamlEncoder()]);
$content = file_get_contents(__DIR__ . '/test.yml');
$data = $serializer->decode($content, YamlEncoder::ALTERNATIVE_FORMAT);
```

Let me know if something is wrong for you

Commits
-------

d8640f9 YamlEncoder handle yml extension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0