Skip to content

[Serializer][FrameworkBundle] Add a YAML encoder #19326

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
wants to merge 5 commits into from

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Jul 10, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR todo

Add YAML support to the Serializer.

@dunglas dunglas changed the title Yaml encoder [Serializer][FrameworkBundle] Add a YAML encoder Jul 10, 2016
private $parser;
private $defaultContext;

public function __construct(Dumper $dumper = null, Parser $parser = null, $defaultContext = array('yaml_inline' => 0, 'yaml_indent' => 0, 'yaml_flags' => 0))
Copy link
Contributor

Choose a reason for hiding this comment

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

Add typehint array $defaultContext

Copy link
Member

Choose a reason for hiding this comment

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

rather than putting a default value here, you should merge the default with the argument being received.
Otherwise, you have no guarantee about the available keys (and so your access to context options will be unsafe)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! Fixed.

@dunglas
Copy link
Member Author

dunglas commented Jul 17, 2016

ping @symfony/deciders

@@ -977,6 +978,12 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
$definition->addTag('serializer.normalizer', array('priority' => -900));
}

if (class_exists(YamlEncoder::class)) {
Copy link
Contributor

@GuilhemN GuilhemN Jul 18, 2016

Choose a reason for hiding this comment

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

You must also ensure that symfony/yaml is loaded as it is a dev dependency of the framework-bundle.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not necessary because of this particular check. If the class doesn't exist (composant not installed), the encoder will not be registered.

Copy link
Member

Choose a reason for hiding this comment

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

@dunglas yes, but here, YamlEncoder refer to the serializer component.

Copy link
Member Author

@dunglas dunglas Jul 19, 2016

Choose a reason for hiding this comment

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

In fact no. The class constant has a special behavior.

use Something\That\Not\Exist;

echo Exist::class;

This snippet will display the FQCN even if the class not exists.

Copy link
Member

Choose a reason for hiding this comment

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

Due use Symfony\Component\Serializer\Encoder\YamlEncoder; this line will not check whether or not a class exists in the Yaml namespace

Copy link
Contributor

@GuilhemN GuilhemN Jul 19, 2016

Choose a reason for hiding this comment

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

Your check ensures that the Serializer component is loaded but you don't know if the Yaml component is loaded too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok got it.

Copy link
Member

Choose a reason for hiding this comment

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

The existing check is useful though, to keep compatibility with serializer 3.1 (other checks above also ensure compat with different versions of the component)

Copy link
Member

Choose a reason for hiding this comment

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

You also need to ensure that the Yaml component is present in version 3.1 or higher (e.g. by checking for existence of one of the Yaml::* constants).

@dunglas
Copy link
Member Author

dunglas commented Aug 16, 2016

Comments fixed. OK to merge?

@fabpot
Copy link
Member

fabpot commented Sep 14, 2016

Thank you @dunglas.

@fabpot fabpot closed this Sep 14, 2016
fabpot added a commit that referenced this pull request Sep 14, 2016
…las)

This PR was squashed before being merged into the 3.2-dev branch (closes #19326).

Discussion
----------

[Serializer][FrameworkBundle] Add a YAML encoder

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | todo

Add YAML support to the Serializer.

Commits
-------

9366a7d [Serializer][FrameworkBundle] Add a YAML encoder
{
$context = array_merge($this->defaultContext, $context);

return Yaml::parse($data, $context['yaml_flags']);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't you use the injected parser instead of performing a static call?

Copy link
Member

Choose a reason for hiding this comment

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

I agree (see #20006).

fabpot added a commit that referenced this pull request Sep 21, 2016
This PR was merged into the 3.2-dev branch.

Discussion
----------

[Serializer] use the injected YAML parser

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19326 (comment)
| License       | MIT
| Doc PR        |

Commits
-------

a40c0e4 [Serializer] use the injected YAML parser
@fabpot fabpot mentioned this pull request Oct 27, 2016
xabbuh added a commit to symfony/symfony-docs that referenced this pull request Dec 12, 2016
This PR was submitted for the master branch but it was merged into the 3.2 branch instead (closes #7040).

Discussion
----------

[Serializer] Docs for CSV and YAML encoders

Docs for symfony/symfony#19197 and symfony/symfony#19326.

Commits
-------

5687972 [Serializer] Docs for CSV and YAML encoders.
wouterj added a commit to symfony/symfony-docs that referenced this pull request Oct 13, 2017
…r (GuilhemN)

This PR was submitted for the 3.2 branch but it was merged into the 3.3 branch instead (closes #7654).

Discussion
----------

[Serializer] Document the CsvEncoder and the YamlEncoder

Document the CsvEncoder and the YamlEncoder introduced in symfony/symfony#19197 and symfony/symfony#19326.

Commits
-------

6944ea1 [Serializer] Document the CsvEncoder and the YamlEncoder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants