Skip to content

Commit aa438e2

Browse files
authored
Merge pull request #39 from swaggest/export-ref-property
properly exporting $ref property
2 parents 4711c51 + bb3bd9b commit aa438e2

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

src/Constraint/Properties.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class Properties extends ObjectItem implements Constraint
2323
/** @var Schema */
2424
protected $__schema;
2525

26+
/**
27+
* Property to data mapping, example ["ref" => "$ref"]
28+
* @var array
29+
*/
30+
public $__defaultMapping = array();
31+
2632
public function lock()
2733
{
2834
$this->__isReadOnly = true;
@@ -53,9 +59,6 @@ public static function create()
5359
return new static;
5460
}
5561

56-
/** @var Schema|null */
57-
private $additionalProperties;
58-
5962
/** @var Egg[][] */
6063
public $nestedProperties = array();
6164

@@ -95,4 +98,31 @@ public function isEmpty()
9598
{
9699
return (count($this->__arrayOfData) + count($this->nestedProperties)) === 0;
97100
}
101+
102+
public function jsonSerialize()
103+
{
104+
$result = $this->__arrayOfData;
105+
if ($this->__nestedObjects) {
106+
foreach ($this->__nestedObjects as $object) {
107+
foreach ($object->toArray() as $key => $value) {
108+
$result[$key] = $value;
109+
}
110+
}
111+
}
112+
113+
if (isset($this->__defaultMapping)) {
114+
$mappedResult = new \stdClass();
115+
foreach ($result as $key => $value) {
116+
if (isset($this->__defaultMapping[$key])) {
117+
$mappedResult->{$this->__defaultMapping[$key]} = $value;
118+
} else {
119+
$mappedResult->$key = $value;
120+
}
121+
}
122+
return $mappedResult;
123+
}
124+
125+
return (object)$result;
126+
}
127+
98128
}

src/Schema.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public function addPropertyMapping($dataName, $propertyName, $mapping = self::DE
9191
{
9292
$this->__dataToProperty[$mapping][$dataName] = $propertyName;
9393
$this->__propertyToData[$mapping][$propertyName] = $dataName;
94+
95+
if ($mapping === self::DEFAULT_MAPPING && $this->properties instanceof Properties) {
96+
$this->properties->__defaultMapping[$propertyName] = $dataName;
97+
}
9498
return $this;
9599
}
96100

src/Wrapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ public function out($data, Context $options = null)
7575
*/
7676
public function getPropertyNames()
7777
{
78-
return array_keys($this->schema->getProperties()->toArray());
78+
return $this->schema->getPropertyNames();
7979
}
8080

8181
/**
8282
* @return string[]
8383
*/
8484
public function getNestedPropertyNames()
8585
{
86-
return $this->schema->getProperties()->nestedPropertyNames;
86+
return $this->schema->getNestedPropertyNames();
8787
}
8888

8989
public function nested()

tests/src/Helper/RefClass.php

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

tests/src/PHPUnit/Schema/ExportTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Swaggest\JsonSchema\Schema;
77
use Swaggest\JsonSchema\Structure\ObjectItem;
8+
use Swaggest\JsonSchema\Tests\Helper\RefClass;
89

910
class ExportTest extends \PHPUnit_Framework_TestCase
1011
{
@@ -162,4 +163,15 @@ public function testVeryNestedObjectWithReference()
162163
$this->assertTrue($exported->veryNestedObject->nesteder instanceof \stdClass);
163164
}
164165

166+
public function testRefClass()
167+
{
168+
$schema = RefClass::schema()->exportSchema();
169+
$this->assertSame('{"properties":{"$ref":{"type":"string","format":"uri-reference"}}}', json_encode($schema));
170+
$schemaData = Schema::export($schema);
171+
$this->assertSame(
172+
'{"properties":{"$ref":{"type":"string","format":"uri-reference"}}}',
173+
json_encode($schemaData, JSON_UNESCAPED_SLASHES)
174+
);
175+
}
176+
165177
}

0 commit comments

Comments
 (0)