-
Notifications
You must be signed in to change notification settings - Fork 184
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
Constructor arguments are not validated when constructing object types from flat types #226
Comments
|
I would argue that this is totally expected. The task of this json mapper is to move data out of a JSON string into objects. It does not have the task to validate if the data in the JSON string is actually suitable to be passed into the objects - especially complain that public properties that have a random So either you apply a JSON schema validation step before the mapping happens, or you apply a validation of the result, or you create code that is able to work on the reality statement that was made by the JSON string only providing partial data. Update: I think I'll try to understand the situation a bit more now... |
|
I concur with @SvenRtbg here. |
|
However... I stepped on some inconsistencies handling null values in various use cases, and I hope to address them in a future PR. First though I want to get the test files organized, then deal with the test description (it lacks describing the use cases). |
|
Seems I missed off what I expected here - I expected that a The fields being uninitialized (and no error being generated) leads to unpredictable behaviour since the |
|
This seems to in a way be a superset of the NULL value handling in #233 - an array entry not exactly matching the required target object structure. |
|
This isn't specific to arrays, A bunch of extra checks would be needed here: Line 729 in 2868e36
I looked into doing it myself, but I ran into some headaches with validating type compatibility in the constructor inputs. The other stuff (checking presence of constructor, required number of args etc) should be fairly trivial. |
BC break! Passing simple types to the class constructor may lead to a bunch of unexpected behavior, which can result in crashes or violate expectations - see the list in the README. By enabling strict object type checks automatically, JsonMapper behaves more calculatable. This is a backwards compatibility break. The old behavior can be restored by setting the $bStrictObjectTypeChecking option to `false`. Resolves: #226 Resolves: #238
When
bStrictObjectTypeCheckingisfalse, a property of typeobjectmight have its constructor invoked with the value provided by the JSON if it wasn't compatible, which is useful for hydrating stuff likeDateTime.However, the code does not check for the following error conditions:
@requiredproperties which won't be set by invoking the constructor (violating expectations of user code)An example of the last case in particular that bit me on the ass:
ClientData.php:
ClientDataPersonaSkinPiece.php:
When used in the following manner:
results in the following unexpected output:
The text was updated successfully, but these errors were encountered: