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

Getting 0.0 after float deserialization #1429

Open
ASrey opened this issue Aug 31, 2022 · 2 comments
Open

Getting 0.0 after float deserialization #1429

ASrey opened this issue Aug 31, 2022 · 2 comments

Comments

@ASrey
Copy link

ASrey commented Aug 31, 2022

Q A
Bug report? yes ?
Feature request? no
BC Break report? no
RFC? yes

Steps required to reproduce the problem

I have an entity which contains this value

    /**
     * @Assert\Type(type="float")
     * @ORM\Column(type="float", nullable=true)
     * @JMS\Groups({"User:write"})
     * @JMS\Expose()
     * @JMS\Type("float")
     */
    private $weight;

At some point i'll try to deserialize a JSON request into this entity with

$user = new User();
$context = DeserializationContext::create();
$context->setAttribute('target', $user);
$context->setGroups(['User:write']);
$serializer->deserialize(json_encode($userData), User::class, 'json', $context);
$validate = $validator->validate($user);
if (!empty($validate)) {
    foreach ($validate as $error) {
        $errors[$error->getPropertyPath()] = $error->getMessage();
    }
}

Expected Result

  • Having an error when weight in userData is a string.
  • Or at least let the actual string without any parsing.

Actual Result

  • weight userData is parse as 0.0f after deserialization.
  • FYI: same result by removing @jms\Type("float") in entity

Further discussion

I do think this is due to https://github.com/schmittjoh/serializer/blob/master/src/Type/Parser.php#L47 which parse "A" to '0.0';
My goal is to be able to get this error after validation of my entity by $validator->validate($user);

I have achived what i want by some hum hum cheating.

  /**
     * @Assert\Type(type="float")
     * @ORM\Column(type="float", nullable=true)
     * @JMS\Groups({"api_read", "UserInfo:read", "api_write", "UserInfo:write", "Appointment:read_parent"})
     * @JMS\Expose()
     * @JMS\Type("string")
     * @JMS\Accessor(getter="getWeight",setter="setWeight")
     */
    private $weight; 
[...]
 public function setWeight($weight): self
    {
         $float_value = (float) $weight;
         $is_string_float = ( strval($float_value) == $weight );
         if($is_string_float)
             $this->weight = (float) $float_value;
        else
            $this->weight = $weight;
        return $this;
    }

Explanations:

  • I let my param as a string with @JMS\Type("string")
  • @JMS\Accessor(getter="getWeight",setter="setWeight") let me have a custom setter which let me set the float value OR let the original param.
  • I can get my exception after validation of my entity

Do did i missed here? Does anyone have a better solution instead of this cheat?
Does the serialization is supposed to parse a string into a float even is it's not a valid float?

Thank you all

@scyzoryck
Copy link
Collaborator

Hello @ASrey!
May JsonDeserializationStrictVisitor will solve your issues? More info you can find here: https://github.com/schmittjoh/serializer/pull/1401/files will solve your issue?

Best, Marcin.

@ASrey
Copy link
Author

ASrey commented Sep 19, 2022

It seems to be a good start! thanks.
I'll dive deeper into it and trying to make it works within my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants