Skip to content

Commit

Permalink
properly exporting $ref property
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed May 2, 2018
1 parent 4711c51 commit bb3bd9b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
36 changes: 33 additions & 3 deletions src/Constraint/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class Properties extends ObjectItem implements Constraint
/** @var Schema */
protected $__schema;

/**
* Property to data mapping, example ["ref" => "$ref"]
* @var array
*/
public $__defaultMapping = array();

public function lock()
{
$this->__isReadOnly = true;
Expand Down Expand Up @@ -53,9 +59,6 @@ public static function create()
return new static;
}

/** @var Schema|null */
private $additionalProperties;

/** @var Egg[][] */
public $nestedProperties = array();

Expand Down Expand Up @@ -95,4 +98,31 @@ public function isEmpty()
{
return (count($this->__arrayOfData) + count($this->nestedProperties)) === 0;
}

public function jsonSerialize()
{
$result = $this->__arrayOfData;
if ($this->__nestedObjects) {
foreach ($this->__nestedObjects as $object) {
foreach ($object->toArray() as $key => $value) {
$result[$key] = $value;
}
}
}

if (isset($this->__defaultMapping)) {
$mappedResult = new \stdClass();
foreach ($result as $key => $value) {
if (isset($this->__defaultMapping[$key])) {
$mappedResult->{$this->__defaultMapping[$key]} = $value;
} else {
$mappedResult->$key = $value;
}
}
return $mappedResult;
}

return (object)$result;
}

}
4 changes: 4 additions & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public function addPropertyMapping($dataName, $propertyName, $mapping = self::DE
{
$this->__dataToProperty[$mapping][$dataName] = $propertyName;
$this->__propertyToData[$mapping][$propertyName] = $dataName;

if ($mapping === self::DEFAULT_MAPPING && $this->properties instanceof Properties) {
$this->properties->__defaultMapping[$propertyName] = $dataName;
}
return $this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ public function out($data, Context $options = null)
*/
public function getPropertyNames()
{
return array_keys($this->schema->getProperties()->toArray());
return $this->schema->getPropertyNames();
}

/**
* @return string[]
*/
public function getNestedPropertyNames()
{
return $this->schema->getProperties()->nestedPropertyNames;
return $this->schema->getNestedPropertyNames();
}

public function nested()
Expand Down
27 changes: 27 additions & 0 deletions tests/src/Helper/RefClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Swaggest\JsonSchema\Tests\Helper;


use Swaggest\JsonSchema\Constraint\Format;
use Swaggest\JsonSchema\Constraint\Properties;
use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchema\Structure\ClassStructure;

class RefClass extends ClassStructure
{

public $ref;

/**
* @param Properties|static $properties
* @param Schema $ownerSchema
*/
public static function setUpProperties($properties, Schema $ownerSchema)
{
$properties->ref = Schema::string();
$properties->ref->format = Format::URI_REFERENCE;

$ownerSchema->addPropertyMapping('$ref', self::names()->ref);
}
}
12 changes: 12 additions & 0 deletions tests/src/PHPUnit/Schema/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchema\Structure\ObjectItem;
use Swaggest\JsonSchema\Tests\Helper\RefClass;

class ExportTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -162,4 +163,15 @@ public function testVeryNestedObjectWithReference()
$this->assertTrue($exported->veryNestedObject->nesteder instanceof \stdClass);
}

public function testRefClass()
{
$schema = RefClass::schema()->exportSchema();
$this->assertSame('{"properties":{"$ref":{"type":"string","format":"uri-reference"}}}', json_encode($schema));
$schemaData = Schema::export($schema);
$this->assertSame(
'{"properties":{"$ref":{"type":"string","format":"uri-reference"}}}',
json_encode($schemaData, JSON_UNESCAPED_SLASHES)
);
}

}

0 comments on commit bb3bd9b

Please sign in to comment.