Skip to content

Commit 9c8f190

Browse files
authored
Export additional and pattern properties (#99)
1 parent 0f50979 commit 9c8f190

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed

src/Schema.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,9 @@ private function processObject($data, Context $options, $path, $result = null)
911911
if ($found || !$import) {
912912
$result->$propertyName = $value;
913913
} elseif (!isset($result->$propertyName)) {
914-
$result->$propertyName = $value;
914+
if (self::PROP_REF !== $propertyName || empty($result->__fromRef)) {
915+
$result->$propertyName = $value;
916+
}
915917
}
916918
}
917919
}

src/Structure/ClassStructureTrait.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,15 @@ public function jsonSerialize()
100100
$result = new \stdClass();
101101
$schema = static::schema();
102102
$properties = $schema->getProperties();
103+
$processed = array();
103104
if (null !== $properties) {
104105
foreach ($properties->getDataKeyMap() as $propertyName => $dataName) {
105106
$value = $this->$propertyName;
107+
106108
// Value is exported if exists.
107109
if (null !== $value || array_key_exists($propertyName, $this->__arrayOfData)) {
108110
$result->$dataName = $value;
111+
$processed[$propertyName] = true;
109112
continue;
110113
}
111114

@@ -129,6 +132,14 @@ public function jsonSerialize()
129132
}
130133
}
131134

135+
if (!empty($this->__arrayOfData)) {
136+
foreach ($this->__arrayOfData as $name => $value) {
137+
if (!isset($processed[$name])) {
138+
$result->$name = $this->{$name};
139+
}
140+
}
141+
}
142+
132143
return $result;
133144
}
134145

tests/src/Helper/DbId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DbId extends AbstractMeta implements ClassStructureContract
2222
public static function setUpProperties($properties, Schema $ownerSchema)
2323
{
2424
$properties->table = DbTable::schema();
25-
$ownerSchema->__fromRef = null;
25+
$ownerSchema->setFromRef(false);
2626
}
2727

2828

tests/src/Helper/SampleProperties.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\Helper;
4+
5+
use Swaggest\JsonSchema\Helper;
6+
use Swaggest\JsonSchema\Schema;
7+
use Swaggest\JsonSchema\Exception\StringException;
8+
use Swaggest\JsonSchema\Structure\ClassStructure;
9+
10+
/**
11+
* @property string $propOne
12+
* @property int $propTwo
13+
* @property $recursion
14+
*/
15+
class SampleProperties extends ClassStructure
16+
{
17+
const X_PROPERTY_PATTERN = '^x-';
18+
19+
/**
20+
* @param \Swaggest\JsonSchema\Constraint\Properties|static $properties
21+
* @param Schema $schema
22+
*/
23+
public static function setUpProperties($properties, Schema $schema)
24+
{
25+
$schema->type = Schema::OBJECT;
26+
$schema->additionalProperties = Schema::object();
27+
$schema->setPatternProperty('^x-', Schema::string());
28+
}
29+
30+
/**
31+
* @param string $name
32+
* @param string $value
33+
* @return self
34+
* @throws InvalidValue
35+
* @codeCoverageIgnoreStart
36+
*/
37+
public function setXValue($name, $value)
38+
{
39+
if (!preg_match(Helper::toPregPattern(self::X_PROPERTY_PATTERN), $name)) {
40+
throw new StringException('Pattern mismatch', StringException::PATTERN_MISMATCH);
41+
}
42+
$this->addPatternPropertyName(self::X_PROPERTY_PATTERN, $name);
43+
$this->{$name} = $value;
44+
return $this;
45+
}
46+
/** @codeCoverageIgnoreEnd */
47+
48+
}

tests/src/PHPUnit/ClassStructure/ClassStructureTest.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Swaggest\JsonSchema\Tests\PHPUnit\ClassStructure;
44

5-
65
use Swaggest\JsonSchema\Exception\TypeException;
76
use Swaggest\JsonSchema\Tests\Helper\ClassWithAllOf;
87
use Swaggest\JsonSchema\Tests\Helper\LevelThreeClass;
8+
use Swaggest\JsonSchema\Tests\Helper\SampleProperties;
99
use Swaggest\JsonSchema\Tests\Helper\SampleStructure;
1010
use Swaggest\JsonSchema\Tests\Helper\StructureWithItems;
1111

@@ -91,10 +91,55 @@ public function testSampleInvalid()
9191

9292
public function testAllOfClassInstance()
9393
{
94-
$value = ClassWithAllOf::import((object)array('myProperty'=>'abc'));
94+
$value = ClassWithAllOf::import((object)array('myProperty' => 'abc'));
9595
$this->assertSame('abc', $value->myProperty);
9696
$this->assertTrue($value instanceof ClassWithAllOf);
9797
}
9898

99+
public function testAdditionalProperties()
100+
{
101+
$properties = new SampleProperties();
102+
$properties->propOne = (object)array(
103+
'subOne' => 'one',
104+
'subTwo' => 'two'
105+
);
106+
107+
$exported = SampleProperties::export($properties);
108+
$json = <<<JSON
109+
{
110+
"propOne": {
111+
"subOne": "one",
112+
"subTwo": "two"
113+
}
114+
}
115+
JSON;
116+
$this->assertSame($json, json_encode($exported, JSON_PRETTY_PRINT), 'With flag to true');
117+
}
118+
119+
public function testPatternProperties()
120+
{
121+
$properties = new SampleProperties();
122+
$properties->setXValue('x-foo', 'bar');
123+
$properties->setXValue('x-baz', 'gnu');
124+
125+
$exported = SampleProperties::export($properties);
126+
$json = <<<JSON
127+
{
128+
"x-foo": "bar",
129+
"x-baz": "gnu"
130+
}
131+
JSON;
132+
$this->assertSame($json, json_encode($exported, JSON_PRETTY_PRINT), 'With flag to true');
133+
}
134+
135+
/**
136+
* @expectedException Swaggest\JsonSchema\Exception\StringException
137+
* @expectedExceptionMessage Pattern mismatch
138+
*/
139+
public function testPatternPropertiesMismatch()
140+
{
141+
$properties = new SampleProperties();
142+
$properties->setXValue('xfoo', 'bar');
143+
}
99144

100145
}

0 commit comments

Comments
 (0)