-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Serialization groups support #12092
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
Conversation
b405e14
to
86c81a1
Compare
👎 The serializer Symfony meets the needs, and that's enough (well I think), and for further applications, there JMSSerializer exists, and have an exellent approach to resolve this problematic. I find that having a "simple" serializer (sf) and serializer under "Steroid" (jms) is good. What is the motivation to add this kind of functionality? |
@Prophet777 the initial motivation is adding groups support to HydraBundle. Of course this can be done directly in that bundle but as the code is generic and will work with any encoder (Hydra or anything else) I've opened this PR here. Note that this does not break BC and that groups support is not enabled by default. Pass no parameter to the normalizer's constructor and the Other motivations are:
And btw, the Serializer currently does not meet the need. Circular references are not handled and serializing a typical set of entities can lead to an infinite loop. I'll submit a PR to fix that soon. |
About the licensing issue see symfony/symfony-standard#535 |
$metadata->addAttributeGroup(lcfirst($matches[2]), $group); | ||
} | ||
} else { | ||
throw new \BadMethodCallException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like you copied the code from the Validator component without being careful about changing it to you needs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right!
I'll change the exception message.
@stof fixed. |
This PR is almost done. I've added a YAML and a XML loader. |
@dunglas we have the time to figure a way to handle this, given that it won't be part of 2.6 (we have reach the feature freeze) |
I've looked more in depth to this piece of code. Only |
btw .. need a rebase now |
4bd6281
to
e07a109
Compare
Rebased. |
490946d
to
dcf1d97
Compare
Rebased against master. Any update about the status of this PR now that 2.6 has been released? |
I agree with @dunglas that getting a better serializer in code in a good idea for reasons he explained above; of course, we need to keep the current philosophy of the component. Seeing that @dunglas is investing a lot of time in this component with great ideas and PRs, no one is really maintaining this component, I propose to let him move the component forward. What do you think @lsmith77 @Seldaek? |
+1 .. I have full trust in him |
+1 for me too, run with it and do great things! :) |
😺 |
@fabpot It's possible to host the new |
@dunglas I think the next step would be to open a PR on the docs as it needs to be documented before merge. |
Submitted a doc: symfony/symfony-docs/pull/4675 |
Thank you @dunglas. |
This PR was merged into the 3.0-dev branch. Discussion ---------- [Serializer] Serialization groups support | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT | Doc PR | symfony/symfony-docs#4675 This PR is a first attempt adding serialization groups to the `Serializer` component. Btw, it also add supports of ignored attributes for denormalization (only normalization is currently supported). Groups support is totally optional and is not enabled by default (in that case, the `Serializer` will have the current behavior). No BC spotted for now. To use it: ```php use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; class MyObj { /** * @groups({"group1", "group2"}) */ public $foo; /** * @groups({"group3"}) */ public $bar; } $obj = new MyObj(); $obj->foo = 'foo'; $obj->bar = 'bar'; $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); $normalizer = new PropertyNormalizer($classMetadataFactory); $serializer = new Serializer([$normalizer]); $data = $serializer->normalize($obj, null, ['groups' => ['group1']]); // $data = ['foo' => 'foo']; $obj2 = $serializer->denormalize(['foo' => 'foo', 'bar' => 'bar'], 'MyObj', null, ['groups' => ['group1', 'group3']); // $obj2 = MyObj(foo: 'foo', bar: 'bar') ``` Some work still need to be done: - [x] Add XML mapping - [x] Add YAML mapping - [x] Refactor tests The `ClassMetadata` code is largely inspired from the code of the `Validator` component. Duplicated code in `PropertyNormalizer` and `GetSetMethodNormalizer` has been moved in a new `AbstractNormalizer` class. This PR also make the interface of `PropertyNormalizer` fluent (like the current behavior of `GetSetMethodNormalizer`. Commits ------- dcf1d97 [Serializer] Serialization groups support
Can this feature be merged in 2.7? |
done |
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #12092). Discussion ---------- [Serializer] Serialization groups support | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT | Doc PR | symfony/symfony-docs#4675 This PR is a first attempt adding serialization groups to the `Serializer` component. Btw, it also add supports of ignored attributes for denormalization (only normalization is currently supported). Groups support is totally optional and is not enabled by default (in that case, the `Serializer` will have the current behavior). No BC spotted for now. To use it: ```php use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; class MyObj { /** * @groups({"group1", "group2"}) */ public $foo; /** * @groups({"group3"}) */ public $bar; } $obj = new MyObj(); $obj->foo = 'foo'; $obj->bar = 'bar'; $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); $normalizer = new PropertyNormalizer($classMetadataFactory); $serializer = new Serializer([$normalizer]); $data = $serializer->normalize($obj, null, ['groups' => ['group1']]); // $data = ['foo' => 'foo']; $obj2 = $serializer->denormalize(['foo' => 'foo', 'bar' => 'bar'], 'MyObj', null, ['groups' => ['group1', 'group3']); // $obj2 = MyObj(foo: 'foo', bar: 'bar') ``` Some work still need to be done: - [x] Add XML mapping - [x] Add YAML mapping - [x] Refactor tests The `ClassMetadata` code is largely inspired from the code of the `Validator` component. Duplicated code in `PropertyNormalizer` and `GetSetMethodNormalizer` has been moved in a new `AbstractNormalizer` class. This PR also make the interface of `PropertyNormalizer` fluent (like the current behavior of `GetSetMethodNormalizer`. Commits ------- 57a191b [Serializer] Serialization groups support
Thanks! |
@fabpot can you upload the XSD at the following URL http://symfony.com/schema/dic/services/services-1.0.xsd to make the Thanks! |
@dunglas the file loader is not based on the XSD on that url, but instead on the XSD downloaded with symfony. |
@wouterj not yet for the serializer but I'll add it soon. |
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] Doc for groups support | Q | A | --------------- | --- | Doc fix? | no | New docs? | yes symfony/symfony#12092 | Applies to | 2.7 This is the documentation for serialization groups support. Commits ------- e417395 [Serializer] Fix CS ae2b78c [Serializer] Add getter group example 745f412 [Serializer] Doc for groups support
This PR is a first attempt adding serialization groups to the
Serializer
component. Btw, it also add supports of ignored attributes for denormalization (only normalization is currently supported).Groups support is totally optional and is not enabled by default (in that case, the
Serializer
will have the current behavior). No BC spotted for now.To use it:
Some work still need to be done:
The
ClassMetadata
code is largely inspired from the code of theValidator
component. Duplicated code inPropertyNormalizer
andGetSetMethodNormalizer
has been moved in a newAbstractNormalizer
class.This PR also make the interface of
PropertyNormalizer
fluent (like the current behavior ofGetSetMethodNormalizer
.