-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
Introduce strict deserializer visitor #1401
Conversation
This seems ok, but how to make it developer friendly? how to let them choose between json and json strict? would it make sense to have it as some sort of option for the existing JsonDeserializationVisitor ? |
it would be good to add this visitor in the list of available visitors and let devs choose if use the struct or not strict deserialization |
@goetas according to docs, I'd expect people do this $serializer = \JMS\Serializer\SerializerBuilder::create()
->setDeserializationVisitor('json', new JsonDeserializationStrictVisitorFactory())
->build(); I'm using bundle so not really sure how people use jms without it. I'd expect they use the way mentioned in docs 🤔 . Also, it would be great to make this default in v4. |
where do I find it? |
what about having something as this
by adding it by default in serializer/src/SerializerBuilder.php Line 348 in 404e9cd
(it would be nice to support it better in the bundle too) |
Allowing |
yea but unfortuantly this is not enough, you should add serializer/src/Handler/DateHandler.php Line 39 in 404e9cd
The bundle does not use the |
I'll look into a bundle support once this is merged. The tests found the issue you've mentioned, fixing it now. |
@goetas This is rather unpleasant though: Looking at the implications, I'd stay with I think it is not really a new format so introducing |
Ok, why not addign then a constructor option to the "old" |
@goetas much better than |
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.
I like strict
idea for deserialization! Thanks 🙇♂️
@goetas is there anything else? Thanks |
thank you! |
Sounds reasonable, will try.
…On Wed, May 11, 2022, 10:32 Asmir Mustafic ***@***.***> wrote:
Ok, why not addign then a constructor option to the "old"
JsonDeserializationVisitorFactory that based on it returns a strict
visitor or a non strict one?
—
Reply to this email directly, view it on GitHub
<#1401 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACQAJLDDABP3BT2XEUKZ3TVJNWDRANCNFSM5RCBAFRA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Recently, I've found that
false
is cast to0
forint
propertiesnull
is cast to""
forstring
propertiesThis greatly modifies the input data that is not desired.
Consider a case when I want to deserialize json
[1,2,3]
and validate that all values are ofint
type. However, json["1", "2", "3"]
is cast to[1,2,3]
so I cannot validate it after deserialization and such input is forwarded further into application.Therefore, I've created strict visitor that does not cast values to desire types and invalid value conversion does not happen anymore. Also, nulls are handled properly. But - I had to change return types of
DeserializationVisitorInterface
to allow null.This behaviour is compliant with e.g.
DateHandler
which returnsnull
value fornull
json. So it all rather behaves as expected now.serializer/src/Handler/DateHandler.php
Lines 193 to 199 in 91652cc
(contains #1400)