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

Dynamic exclusion strategy, Variable "object" is not valid #826

Closed
InfopactMLoos opened this issue Oct 11, 2017 · 3 comments
Closed

Dynamic exclusion strategy, Variable "object" is not valid #826

InfopactMLoos opened this issue Oct 11, 2017 · 3 comments

Comments

@InfopactMLoos
Copy link

Q A
Bug report? not sure?
Feature request? no
BC Break report? no
RFC? no

Steps required to reproduce the problem

  1. Enable Expression Language exclusion strategy as per: https://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies#dynamic-exclusion-strategy
  2. Use the object variable in a expression within the Expose annotation on a class property, like so:
@Serializer\Expose(if="object.isRedacted() === false")
  1. Call deserialize on a string using this class, like so:
$serializer->deserialize($requestBody, 'MyClass', 'json');
  1. Receive exception:
Variable \"object\" is not valid around position 3 for expression `!(object.isRedacted() === false)`.

Expected Result

  • I expect a successful deserialization.

Actual Result

  • Serialization failed with above exception message. As I looked closer at the code it seems that the object variable is only set when there is a SerializationContext. Meaning ofcourse that this variable is not available during deserialization. You can find this in class JMS\Serializer\Exclusion\ExpressionLanguageExclusionStrategy line 60.
    Although this might be quite logical since the object is still being created (and I actually don't need this expression to be evaluated during deserialization), however how do I only enable it on serialization? Or actually what would be the best way to do this? I can ofcourse extend the expression but since I have this expression on like 106 properties I would like to keep these expressions as simple and clean as possible. Or maybe I should write a custom exclusion strategy...
@goetas
Copy link
Collaborator

goetas commented Oct 16, 2017

You are right. This is an unexpected behavior of the current implementation.

A possible workaround for this can be

@Serializer\Expose(if="context.getDirection() === 1 && object.isRedacted() === false").

Actually this solution will require anyway to update the ExpressionLanguageExclusionStrategy.php
(see #827)

This solution might work but I guess there are better solutions... alternative ideas?

@InfopactMLoos
Copy link
Author

InfopactMLoos commented Oct 16, 2017

Yes I indeed already tried the workaround (forgot to mention) but the full expresion is always evaluated. Your commit seems promising though I will have a look at it.
I was also thinking if it might be possible to catch the exception and just return false if the variable does not exist. But that might give other problems as the exception returned seems to be a generic, Symfony\Component\ExpressionLanguage\SyntaxError.

@goetas
Copy link
Collaborator

goetas commented Oct 19, 2017

solved by #827

@goetas goetas closed this as completed Oct 19, 2017
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

2 participants