Skip to content
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

[Validator][ConstraintViolation] Implemented Serializable #25386

Closed
wants to merge 2 commits into from

Conversation

alexmarucci
Copy link

@alexmarucci alexmarucci commented Dec 7, 2017

Implemented serializable and ignore the root property to avoid exception.

Q A
Branch? 3.3
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #24537
License MIT
Doc PR

@Simperfit
Copy link
Contributor

If it's a bugfix it should be in the lower branch where the bug exists.

Could you please check why the tests are not passing ?

@nicolas-grekas nicolas-grekas added this to the 3.3 milestone Dec 10, 2017
@nicolas-grekas nicolas-grekas changed the base branch from master to 3.3 December 10, 2017 16:46
@nicolas-grekas
Copy link
Member

Reading the linked issue, I understand why this has been submitted ($this->root may be unserializable), but from a local pov, not sure it makes sense.
Any other idea?

@xabbuh xabbuh modified the milestones: 3.3, 3.4 Jan 29, 2018
@xabbuh
Copy link
Member

xabbuh commented Jan 29, 2018

moving to the 3.4 milestone as the last bugfix release for 3.3 was published today

@alexmarucci
Copy link
Author

Another solution could be to check in FormError if the cause itself is serializable, as @ogizanagi suggest in the linked issue.
Something like:
$this->isSerializable($this->cause) ? $this->cause : null,
That would prevent the Exception to be trown.
isSerializable would be a private function inside FormError class like so;
private function isSerializable($property) { try { serialize( $property ); return true; } catch (\Exception $e) { return false; } }

What do you guys think about that solution ?

@nicolas-grekas
Copy link
Member

Implementing isSerializable is generally not possible: some internal classes throw a fatal error instead of an exception. This would be very fragile.
Any other approach that would not require this? @ogizanagi maybe?

@ogizanagi
Copy link
Member

Unfortunately, I don't have much more appart making ConstraintViolation implement \Serializable as here and ignore FormError::$cause, ConstraintViolation::$cause,$root in the serialization, unless scalar or implementing \Serializable.
Or implement Form::__sleep() or FormConfigInterface::__sleep().
But that's more workarounds than proper fixes :/

{
public function testSerializeFormError()
{
if (!class_exists(ConstraintViolation::class)) {
Copy link
Member

Choose a reason for hiding this comment

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

this is not required as symfony/validator is listed under require-dev

@xabbuh
Copy link
Member

xabbuh commented Sep 13, 2018

👍 for implementing \Serializable and ignoring the cause if it cannot be serialised

@alexmarucci
Copy link
Author

alexmarucci commented Sep 13, 2018

Hey Guys,

Couldn't find the reason why tests are failing on travis. If someone has any idea is more than welcome.

@xabbuh xabbuh added the Form label Sep 13, 2018
@xabbuh xabbuh changed the base branch from 3.3 to 2.8 September 14, 2018 09:09
@ro0NL
Copy link
Contributor

ro0NL commented Sep 14, 2018

Note you can already serialize the violation list using the Serializer, e.g. https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php

Which will work with any ConstraintViolationListInterface object, not just ConstraintViolation. But implementing serializable here doesnt hurt i guess :)

@xabbuh xabbuh modified the milestones: 3.4, 2.8 Sep 14, 2018
@fabpot
Copy link
Member

fabpot commented Oct 10, 2018

Do we still need this PR after @ro0NL's comment?
If yes, this should be rebased on master as this is a new feature.

@ogizanagi
Copy link
Member

ogizanagi commented Oct 10, 2018

I missed @ro0NL 's comment on this PR. I might be misinterpreting, but I fail to understand how it relates to the original issue. Yes, constraint violations can be serialized using the Symfony serializer.
But the original issue is about serializing a FormError using PHP as FormError implements \Serializable.

However, I don't know the use-case, and perhaps what's wrong here is letting users think a FormError can be safely serialized using PHP (not sure why the interface was added on this class in the first place? Probably for an older version of the profiler). Should we deprecate the \Serializable implementation in FormError?

@fabpot
Copy link
Member

fabpot commented Oct 10, 2018

Deprecating \Serializable on FormError sounds like a great idea. I don't think we want to support serializing for errors anyway.

@xabbuh
Copy link
Member

xabbuh commented Oct 10, 2018

Aren't form errors serialised when the profile is written to the filesystem?

@ogizanagi
Copy link
Member

ogizanagi commented Oct 10, 2018

@xabbuh : I don't think that's true anymore. We serialize Data (VarDumper) clones.

@nicolas-grekas
Copy link
Member

I'm closing here as serializing is not a primary use case of the class. Using VarDumper for cloning is used internally - and should/could be used in similar userland cases IMHO - (using the Serializer component would work also).

Thanks @alexmarucci for helping this move forward.

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.

None yet

8 participants