Skip to content

Commit 1972ffe

Browse files
authored
Merge pull request #88 from swaggest/keep-proper-object-class
Keep proper object class
2 parents 1f36c46 + 6a2e24f commit 1972ffe

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

src/Schema.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -593,38 +593,38 @@ private function processObject($data, Context $options, $path, $result = null)
593593

594594
if ($this->useObjectAsArray) {
595595
$result = array();
596-
} elseif (!$result instanceof ObjectItemContract) {
596+
} else {
597597
//* todo check performance impact
598598
if (null === $this->objectItemClass) {
599-
$result = new ObjectItem();
599+
if (!$result instanceof ObjectItemContract) {
600+
$result = new ObjectItem();
601+
$result->setDocumentPath($path);
602+
}
600603
} else {
601604
$className = $this->objectItemClass;
602605
if ($options->objectItemClassMapping !== null) {
603606
if (isset($options->objectItemClassMapping[$className])) {
604607
$className = $options->objectItemClassMapping[$className];
605608
}
606609
}
607-
$result = new $className;
608-
}
609-
//*/
610-
611-
612-
if ($result instanceof ClassStructure) {
613-
if ($result->__validateOnSet) {
614-
$result->__validateOnSet = false;
615-
/** @noinspection PhpUnusedLocalVariableInspection */
616-
/* todo check performance impact
617-
$validateOnSetHandler = new ScopeExit(function () use ($result) {
618-
$result->__validateOnSet = true;
619-
});
610+
if (null !== $result && get_class($result) !== $className) {
611+
$result = new $className;
612+
//* todo check performance impact
613+
if ($result instanceof ClassStructure) {
614+
$result->setDocumentPath($path);
615+
if ($result->__validateOnSet) {
616+
$result->__validateOnSet = false;
617+
/** @noinspection PhpUnusedLocalVariableInspection */
618+
/* todo check performance impact
619+
$validateOnSetHandler = new ScopeExit(function () use ($result) {
620+
$result->__validateOnSet = true;
621+
});
622+
//*/
623+
}
624+
}
620625
//*/
621626
}
622627
}
623-
624-
//* todo check performance impact
625-
if ($result instanceof ObjectItemContract) {
626-
$result->setDocumentPath($path);
627-
}
628628
//*/
629629
}
630630
}

tests/src/Helper/ClassWithAllOf.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
4+
namespace Swaggest\JsonSchema\Tests\Helper;
5+
6+
7+
use Swaggest\JsonSchema\Constraint\Properties;
8+
use Swaggest\JsonSchema\Schema;
9+
use Swaggest\JsonSchema\Structure\ClassStructure;
10+
11+
class ClassWithAllOf extends ClassStructure
12+
{
13+
public $myProperty;
14+
15+
/**
16+
* @param Properties|static $properties
17+
* @param Schema $ownerSchema
18+
*/
19+
public static function setUpProperties($properties, Schema $ownerSchema)
20+
{
21+
$properties->myProperty = Schema::string();
22+
23+
$not = new Schema();
24+
$not->not = Schema::integer();
25+
$ownerSchema->allOf[0] = $not;
26+
}
27+
28+
29+
}

tests/src/PHPUnit/ClassStructure/ClassStructureTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
use Swaggest\JsonSchema\Exception\TypeException;
7+
use Swaggest\JsonSchema\Tests\Helper\ClassWithAllOf;
78
use Swaggest\JsonSchema\Tests\Helper\LevelThreeClass;
89
use Swaggest\JsonSchema\Tests\Helper\SampleStructure;
910
use Swaggest\JsonSchema\Tests\Helper\StructureWithItems;
@@ -88,5 +89,12 @@ public function testSampleInvalid()
8889
));
8990
}
9091

92+
public function testAllOfClassInstance()
93+
{
94+
$value = ClassWithAllOf::import((object)array('myProperty'=>'abc'));
95+
$this->assertSame('abc', $value->myProperty);
96+
$this->assertTrue($value instanceof ClassWithAllOf);
97+
}
98+
9199

92100
}

0 commit comments

Comments
 (0)