Skip to content

Commit cb1e65c

Browse files
authored
#58 add support for native classes as data (#59)
#58 add support for native classes as data
1 parent f5ce9a1 commit cb1e65c

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

src/Constraint/Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static function isValid($types, $data, $version)
6464
foreach ($types as $type) {
6565
switch ($type) {
6666
case self::OBJECT:
67-
$ok = $data instanceof \stdClass;
67+
$ok = is_object($data);
6868
break;
6969
case self::ARR:
7070
$ok = is_array($data);

src/Schema.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ private function processIf($data, Context $options, $path)
530530
}
531531

532532
/**
533-
* @param \stdClass $data
533+
* @param object $data
534534
* @param Context $options
535535
* @param string $path
536536
* @throws InvalidValue
@@ -568,7 +568,7 @@ private function processObjectRequired($data, Context $options, $path)
568568
}
569569

570570
/**
571-
* @param \stdClass $data
571+
* @param object $data
572572
* @param Context $options
573573
* @param string $path
574574
* @param ObjectItemContract|null $result
@@ -741,7 +741,7 @@ private function processObject($data, Context $options, $path, $result = null)
741741

742742
$array = array();
743743
if (!empty($this->__dataToProperty[$options->mapping])) { // todo skip on $options->validateOnly
744-
foreach ((array)$data as $key => $value) {
744+
foreach (!$data instanceof \stdClass ? get_object_vars($data) : (array)$data as $key => $value) {
745745
if ($import) {
746746
if (isset($this->__dataToProperty[$options->mapping][$key])) {
747747
$key = $this->__dataToProperty[$options->mapping][$key];
@@ -754,7 +754,7 @@ private function processObject($data, Context $options, $path, $result = null)
754754
$array[$key] = $value;
755755
}
756756
} else {
757-
$array = (array)$data;
757+
$array = !$data instanceof \stdClass ? get_object_vars($data) : (array)$data;
758758
}
759759

760760
if (!$options->skipValidation) {
@@ -1135,7 +1135,7 @@ public function process($data, Context $options, $path = '#', $result = null)
11351135
$result = $this->processAllOf($data, $options, $path);
11361136
}
11371137

1138-
if ($data instanceof \stdClass) {
1138+
if (is_object($data)) {
11391139
$result = $this->processObject($data, $options, $path, $result);
11401140
}
11411141

tests/src/Helper/SimpleClass.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\Helper;
4+
5+
6+
class SimpleClass
7+
{
8+
public $id;
9+
public $username;
10+
public $email;
11+
12+
13+
// checking leak of private/protected properties
14+
protected $temp1;
15+
private $temp2;
16+
17+
public function __construct()
18+
{
19+
$this->temp1 = 'temp1';
20+
$this->temp2 = 'temp2';
21+
}
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit\Suite;
4+
5+
use Swaggest\JsonSchema\Constraint\Format;
6+
use Swaggest\JsonSchema\InvalidValue;
7+
use Swaggest\JsonSchema\Schema;
8+
use Swaggest\JsonSchema\Tests\Helper\SimpleClass;
9+
10+
class Issue58Test extends \PHPUnit_Framework_TestCase
11+
{
12+
private function getData()
13+
{
14+
$data = new SimpleClass();
15+
$data->id = 1234;
16+
$data->username = "John";
17+
$data->email = "john@doe.com";
18+
return $data;
19+
}
20+
21+
private function getSchema()
22+
{
23+
$schema = Schema::object();
24+
$schema
25+
->setProperty('id', Schema::integer())
26+
->setProperty('username', Schema::string())
27+
->setProperty('email', Schema::string()->setFormat(Format::EMAIL));
28+
29+
// checking leak of private/protected properties
30+
$schema->additionalProperties = false;
31+
return $schema;
32+
}
33+
34+
public function testSimpleClass()
35+
{
36+
$this->getSchema()->out($this->getData());
37+
}
38+
39+
public function testSimpleClassFailed()
40+
{
41+
$data = $this->getData();
42+
$data->email = 'bla-bla';
43+
$this->setExpectedException(get_class(new InvalidValue()));
44+
$this->getSchema()->out($data);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)