Skip to content

Commit 155cf24

Browse files
committed
Default $bStrictObjectTypeChecking to true
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
1 parent 30589df commit 155cf24

7 files changed

+32
-6
lines changed

Diff for: README.rst

+21-5
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,33 @@ setter methods by setting ``$bIgnoreVisibility`` to true:
477477
Simple types instead of objects
478478
===============================
479479
When a variable's type is a class and JSON data is a simple type
480-
like ``string``, JsonMapper passes this value to the class' constructor.
481-
482-
If you do not want this, set ``$bStrictObjectTypeChecking`` to ``true``:
480+
like ``string``, JsonMapper can pass this value to the class' constructor
481+
when configured to do so:
483482

484483
.. code:: php
485484
486485
$jm = new JsonMapper();
487-
$jm->bStrictObjectTypeChecking = true;
486+
$jm->bStrictObjectTypeChecking = false;
488487
$jm->map(...);
489488
490-
An exception is then thrown in such cases.
489+
This can be used to automatically initialize DateTime objects
490+
from date strings.
491+
492+
Disabling this strict object type checks may lead to problems, though:
493+
494+
- When a class does not have a constructor or no constructor parameter,
495+
the value will get lost
496+
- When the constructor has more than 1 required parameter, it will crash.
497+
- When the constructor's parameter type does not match the one of the
498+
data in JSON, it will crash
499+
- ``@required`` properties will not be filled
500+
501+
.. note::
502+
The default value changed from ``false`` to ``true`` in version 5 to
503+
increase security.
504+
505+
Now you have to opt in if you want to pass simple types to
506+
the class constructor.
491507

492508

493509
Passing arrays to ``map()``

Diff for: src/JsonMapper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class JsonMapper
6464
*
6565
* @var boolean
6666
*/
67-
public $bStrictObjectTypeChecking = false;
67+
public $bStrictObjectTypeChecking = true;
6868

6969
/**
7070
* Throw an exception, if null value is found

Diff for: tests/ArrayTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testMapTypedArray()
4848
public function testMapTypedSimpleArray()
4949
{
5050
$jm = new JsonMapper();
51+
$jm->bStrictObjectTypeChecking = false;
5152
$jm->bStrictNullTypesInArrays = false;
5253
$sn = $jm->map(
5354
json_decode('{"typedSimpleArray":["2014-01-02",null,"2014-05-07"]}'),
@@ -510,6 +511,7 @@ public function testMapArrayStrangeKeys()
510511
public function testMapTypedSimpleArrayFromObject()
511512
{
512513
$jm = new JsonMapper();
514+
$jm->bStrictObjectTypeChecking = false;
513515
$sn = $jm->map(
514516
json_decode('{"typedSimpleArray":{"en-US":"2014-01-02"}}'),
515517
new JsonMapperTest_Array()
@@ -587,6 +589,7 @@ public function testMapArrayFromVariadicFunctionWithSimpleType()
587589
public function testMapArrayFromVariadicFunctionWithObjectType()
588590
{
589591
$jm = new JsonMapper();
592+
$jm->bStrictObjectTypeChecking = false;
590593
/** @var JsonMapperTest_VariadicArray $sn */
591594
$sn = $jm->map(
592595
json_decode('{"variadicDateTime":["2014-01-02","2014-05-07"]}'),

Diff for: tests/ClassMapTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public static function classMapTestData()
5959
public function testClassMap($classMapValue)
6060
{
6161
$jm = new JsonMapper();
62+
$jm->bStrictObjectTypeChecking = false;
6263
$jm->classMap[self::CLASS_MAP_CLASS] = $classMapValue;
6364
$sn = $jm->map(
6465
json_decode('{"pPlainObject":"'.self::CLASS_MAP_DATA.'"}'),
@@ -75,6 +76,7 @@ public function testClassMap($classMapValue)
7576
public function testNamespaceKeyWithLeadingBackslash()
7677
{
7778
$jm = new JsonMapper();
79+
$jm->bStrictObjectTypeChecking = false;
7880
$jm->classMap['\\namespacetest\\model\\User']
7981
= \namespacetest\Unit::class;
8082
$data = $jm->map(
@@ -88,6 +90,7 @@ public function testNamespaceKeyWithLeadingBackslash()
8890
public function testNamespaceKeyNoLeadingBackslash()
8991
{
9092
$jm = new JsonMapper();
93+
$jm->bStrictObjectTypeChecking = false;
9194
$jm->classMap[\namespacetest\model\User::class]
9295
= \namespacetest\Unit::class;
9396
$data = $jm->map(

Diff for: tests/Enums_PHP81_Test.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Enums_PHP81_Test extends \PHPUnit\Framework\TestCase
2222
public function testEnumMapping()
2323
{
2424
$jm = new JsonMapper();
25+
$jm->bStrictObjectTypeChecking = false;
2526
/** @var \Enums\ObjectWithEnum $sn */
2627
$sn = $jm->map(
2728
json_decode(self::TEST_DATA),

Diff for: tests/NamespaceTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function testMapChildClassNamespace()
5353
public function testMapChildClassConstructorNamespace()
5454
{
5555
$mapper = new \JsonMapper();
56+
$mapper->bStrictObjectTypeChecking = false;
5657
$json = '{"user":"John Smith"}';
5758
$res = $mapper->map(json_decode($json), new UnitData());
5859
$this->assertInstanceOf(\namespacetest\UnitData::class, $res);
@@ -106,6 +107,7 @@ public function testMapCustomArrayObject()
106107
public function testSetterNamespacedTypeHint()
107108
{
108109
$mapper = new \JsonMapper();
110+
$mapper->bStrictObjectTypeChecking = false;
109111
$json = '{"namespacedTypeHint":"Foo"}';
110112
$res = $mapper->map(json_decode($json), new UnitData());
111113
$this->assertInstanceOf(\namespacetest\UnitData::class, $res);

Diff for: tests/ObjectTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function testMapObjectByClassName()
5252
public function testMapDateTime()
5353
{
5454
$jm = new JsonMapper();
55+
$jm->bStrictObjectTypeChecking = false;
5556
$sn = $jm->map(
5657
json_decode('{"datetime":"2014-04-01T00:00:00+02:00"}'),
5758
new JsonMapperTest_Object()

0 commit comments

Comments
 (0)