Skip to content

Commit

Permalink
#58 add support for native classes as data
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Nov 11, 2018
1 parent f5ce9a1 commit 96822a6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Constraint/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function isValid($types, $data, $version)
foreach ($types as $type) {
switch ($type) {
case self::OBJECT:
$ok = $data instanceof \stdClass;
$ok = is_object($data);
break;
case self::ARR:
$ok = is_array($data);
Expand Down
6 changes: 3 additions & 3 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ private function processObject($data, Context $options, $path, $result = null)

$array = array();
if (!empty($this->__dataToProperty[$options->mapping])) { // todo skip on $options->validateOnly
foreach ((array)$data as $key => $value) {
foreach (!$data instanceof \stdClass ? get_object_vars($data) : (array)$data as $key => $value) {
if ($import) {
if (isset($this->__dataToProperty[$options->mapping][$key])) {
$key = $this->__dataToProperty[$options->mapping][$key];
Expand All @@ -754,7 +754,7 @@ private function processObject($data, Context $options, $path, $result = null)
$array[$key] = $value;
}
} else {
$array = (array)$data;
$array = !$data instanceof \stdClass ? get_object_vars($data) : (array)$data;
}

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

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

Expand Down
22 changes: 22 additions & 0 deletions tests/src/Helper/SimpleClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Swaggest\JsonSchema\Tests\Helper;


class SimpleClass
{
public $id;
public $username;
public $email;


// checking leak of private/protected properties
protected $temp1;
private $temp2;

public function __construct()
{
$this->temp1 = 'temp1';
$this->temp2 = 'temp2';
}
}
47 changes: 47 additions & 0 deletions tests/src/PHPUnit/Suite/Issue58Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Swaggest\JsonSchema\Tests\PHPUnit\Suite;

use Swaggest\JsonSchema\Constraint\Format;
use Swaggest\JsonSchema\InvalidValue;
use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchema\Tests\Helper\SimpleClass;

class Issue58Test extends \PHPUnit_Framework_TestCase
{
private function getData()
{
$data = new SimpleClass();
$data->id = 1234;
$data->username = "John";
$data->email = "john@doe.com";
return $data;
}

private function getSchema()
{
$schema = Schema::object();
$schema
->setProperty('id', Schema::integer())
->setProperty('username', Schema::string())
->setProperty('email', Schema::string()->setFormat(Format::EMAIL));

// checking leak of private/protected properties
$schema->additionalProperties = false;
return $schema;
}

public function testSimpleClass()
{
$this->getSchema()->out($this->getData());
}

public function testSimpleClassFailed()
{
$data = $this->getData();
$data->email = 'bla-bla';
$this->setExpectedException(get_class(new InvalidValue()));
$this->getSchema()->out($data);
}

}

0 comments on commit 96822a6

Please sign in to comment.