Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/JsonSchema/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ private function processOther()
(string)$names->not => 1,
(string)$names->definitions => 1,
(string)$names->enum => 1,
(string)$names->if => 1,
(string)$names->then => 1,
(string)$names->else => 1,
(string)$names->fromRef => 1,
(string)$names->originPath => 1,
);
Expand Down Expand Up @@ -362,25 +365,29 @@ private function processOther()

private function processLogic()
{
if ($this->schema->not !== null) {
$this->result->addSnippet(
$this->copyTo(new SchemaBuilder(
$this->schema->not,
"{$this->varName}->not",
$this->path . '->not',
$this->phpBuilder
))->build()
);
$names = Schema::names();
foreach (array($names->not, $names->if, $names->then, $names->else) as $keyword) {
if ($this->schema->$keyword !== null) {
$this->result->addSnippet(
$this->copyTo(new SchemaBuilder(
$this->schema->$keyword,
"{$this->varName}->{$keyword}",
$this->path . '->' . $keyword,
$this->phpBuilder
))->build()
);
}

}

foreach (array('anyOf', 'oneOf', 'allOf') as $logic) {
if ($this->schema->$logic !== null) {
foreach ($this->schema->$logic as $index => $schema) {
foreach (array($names->anyOf, $names->oneOf, $names->allOf) as $keyword) {
if ($this->schema->$keyword !== null) {
foreach ($this->schema->$keyword as $index => $schema) {
$this->result->addSnippet(
$this->copyTo(new SchemaBuilder(
$schema,
"{$this->varName}->{$logic}[{$index}]",
$this->path . "->{$logic}[{$index}]",
"{$this->varName}->{$keyword}[{$index}]",
$this->path . "->{$keyword}[{$index}]",
$this->phpBuilder
))->build()
);
Expand Down
94 changes: 94 additions & 0 deletions tests/src/PHPUnit/Issues/Issue5Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Swaggest\PhpCodeBuilder\Tests\PHPUnit\Issues;

use Swaggest\JsonSchema\Exception\ObjectException;
use Swaggest\JsonSchema\Schema;
use Swaggest\PhpCodeBuilder\App\PhpApp;
use Swaggest\PhpCodeBuilder\JsonSchema\ClassHookCallback;
use Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder;
use Swaggest\PhpCodeBuilder\PhpClass;
use Swaggest\PhpCodeBuilder\Tests\Tmp\Issue5\Sample;

/**
* @see https://github.com/swaggest/php-code-builder/issues/5
*/
class Issue5Test extends \PHPUnit_Framework_TestCase
{
function testIssue5()
{
$schemaJson = <<<'JSON'
{
"type": "object",
"properties": {
"foo": {
"type": "string"
},
"bar": {
"type": "string"
}
},
"if": {
"properties": {
"foo": {
"enum": [
"bar"
]
}
}
},
"then": {
"required": [
"bar"
]
}
}
JSON;

$appPath = realpath(__DIR__ . '/../../Tmp') . '/Issue5';
$appNs = 'Swaggest\PhpCodeBuilder\Tests\Tmp\\Issue5';

$app = new PhpApp();
$app->setNamespaceRoot($appNs, '.');

$schema = Schema::import(json_decode($schemaJson));
$builder = new PhpBuilder();
$builder->buildSetters = false;
$builder->makeEnumConstants = true;

$builder->classCreatedHook = new ClassHookCallback(
function (PhpClass $class, $path, $schema) use ($app, $appNs) {
$class->setNamespace($appNs);
if ('#' === $path) {
$class->setName('Sample'); // Class name for root schema
}
$app->addClass($class);
}
);


$builder->getType($schema);

$app->clearOldFiles($appPath);
$app->store($appPath);

exec('git diff ' . $appPath, $out);
$out = implode("\n", $out);
$this->assertSame('', $out, "Generated files changed");

}


function testGeneratedInvalid()
{
$this->setExpectedException(ObjectException::class, 'Required property missing: bar, data: {"foo":"bar"} at #->$ref[#/definitions/Swaggest\PhpCodeBuilder\Tests\Tmp\Issue5\Sample]->then');
Sample::import((object)array('foo' => 'bar'));
}

function testGeneratedValid()
{
Sample::import((object)array('foo' => 'bar', 'bar' => 'ok'));
Sample::import((object)array('foo' => 'not-bar'));
}

}
31 changes: 31 additions & 0 deletions tests/src/Tmp/Issue5/IfClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @file ATTENTION!!! The code below was carefully crafted by a mean machine.
* Please consider to NOT put any emotional human-generated modifications as the splendid AI will throw them away with no mercy.
*/

namespace Swaggest\PhpCodeBuilder\Tests\Tmp\Issue5;

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


class IfClass extends ClassStructure
{
const BAR = 'bar';

public $foo;

/**
* @param Properties|static $properties
* @param Schema $ownerSchema
*/
public static function setUpProperties($properties, Schema $ownerSchema)
{
$properties->foo = new Schema();
$properties->foo->enum = array(
self::BAR,
);
}
}
37 changes: 37 additions & 0 deletions tests/src/Tmp/Issue5/Sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* @file ATTENTION!!! The code below was carefully crafted by a mean machine.
* Please consider to NOT put any emotional human-generated modifications as the splendid AI will throw them away with no mercy.
*/

namespace Swaggest\PhpCodeBuilder\Tests\Tmp\Issue5;

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


class Sample extends ClassStructure
{
/** @var string */
public $foo;

/** @var string */
public $bar;

/**
* @param Properties|static $properties
* @param Schema $ownerSchema
*/
public static function setUpProperties($properties, Schema $ownerSchema)
{
$properties->foo = Schema::string();
$properties->bar = Schema::string();
$ownerSchema->type = 'object';
$ownerSchema->if = IfClass::schema();
$ownerSchema->then = new Schema();
$ownerSchema->then->required = array(
0 => 'bar',
);
}
}