Skip to content

Creating getter and setter for additional properties on boolean true #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
22 changes: 20 additions & 2 deletions src/JsonSchema/PhpBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Swaggest\PhpCodeBuilder\PhpFlags;
use Swaggest\PhpCodeBuilder\PhpFunction;
use Swaggest\PhpCodeBuilder\PhpNamedVar;
use Swaggest\PhpCodeBuilder\PhpStdType;
use Swaggest\PhpCodeBuilder\Property\AdditionalPropertiesGetter;
use Swaggest\PhpCodeBuilder\Property\AdditionalPropertySetter;
use Swaggest\PhpCodeBuilder\Property\Getter;
Expand Down Expand Up @@ -73,6 +74,13 @@ public function __construct()
*/
public $declarePropertyDefaults = false;

/**
* Build setter and getter methods for additional properties
* on a boolean true value for `additionalProperties`.
* @var bool
*/
public $buildAdditionalPropertyMethodsOnTrue = false;

/**
* @param SchemaContract $schema
* @param string $path
Expand Down Expand Up @@ -210,9 +218,19 @@ private function makeClass($schema, $path)
}
}

$additionalPropertiesType = null;
$buildAdditionalPropertiesMethods = false;
if ($schema->additionalProperties instanceof Schema) {
$class->addMethod(new AdditionalPropertiesGetter($this->getType($schema->additionalProperties)));
$class->addMethod(new AdditionalPropertySetter($this->getType($schema->additionalProperties)));
$additionalPropertiesType = $this->getType($schema->additionalProperties);
$buildAdditionalPropertiesMethods = true;
} elseif ($this->buildAdditionalPropertyMethodsOnTrue && $schema->additionalProperties === true) {
$additionalPropertiesType = PhpStdType::mixed();
$buildAdditionalPropertiesMethods = true;
}

if ($buildAdditionalPropertiesMethods) {
$class->addMethod(new AdditionalPropertiesGetter($additionalPropertiesType));
$class->addMethod(new AdditionalPropertySetter($additionalPropertiesType));
}

if ($schema->patternProperties !== null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @method static mixed import($data, Swaggest\JsonSchema\Context $options = null)
*/
class Root extends Swaggest\JsonSchema\Structure\ClassStructure
{
/**
* @param Swaggest\JsonSchema\Constraint\Properties|static $properties
* @param Swaggest\JsonSchema\Schema $ownerSchema
*/
public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $ownerSchema)
{
$ownerSchema->type = Swaggest\JsonSchema\Schema::OBJECT;
$ownerSchema->additionalProperties = true;
}

/**
* @return array
* @codeCoverageIgnoreStart
*/
public function getAdditionalPropertyValues()
{
$result = array();
if (!$names = $this->getAdditionalPropertyNames()) {
return $result;
}
foreach ($names as $name) {
$result[$name] = $this->$name;
}
return $result;
}
/** @codeCoverageIgnoreEnd */

/**
* @param string $name
* @param mixed $value
* @return self
* @codeCoverageIgnoreStart
*/
public function setAdditionalPropertyValue($name, $value)
{
$this->addAdditionalPropertyName($name);
$this->{$name} = $value;
return $this;
}
/** @codeCoverageIgnoreEnd */
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @method static mixed import($data, Swaggest\JsonSchema\Context $options = null)
*/
class Root extends Swaggest\JsonSchema\Structure\ClassStructure
{
/**
* @param Swaggest\JsonSchema\Constraint\Properties|static $properties
* @param Swaggest\JsonSchema\Schema $ownerSchema
*/
public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $ownerSchema)
{
$ownerSchema->type = Swaggest\JsonSchema\Schema::OBJECT;
$ownerSchema->additionalProperties = true;
}
}

39 changes: 39 additions & 0 deletions tests/src/PHPUnit/JsonSchema/AdvancedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,4 +733,43 @@ public function testClassPropertyDefaultsDeactivated()
$this->assertContains($expected, $result);
}

public function testBuildingAdditionalPropertyMethodsOnTrue()
{
$schemaData = json_decode(<<<'JSON'
{
"type": "object",
"additionalProperties": true
}
JSON
);

// Keeping the old behavior
$schema = Schema::import($schemaData);
$builder = new PhpBuilder();
$class = $builder->getClass($schema, 'Root');

$result = '';
foreach ($builder->getGeneratedClasses() as $class) {
$result .= $class->class . "\n\n";
}

$expected = file_get_contents(__DIR__ . '/../../../resources/JsonSchema/AdvancedTest/' . __FUNCTION__ . '-old-behavior.txt');

$this->assertSame($expected, $result, 'Getter and setter not created (old behavior - flag: false)');

// Testing the new behavior with setted flag
$schema = Schema::import($schemaData);
$builder = new PhpBuilder();
$builder->buildAdditionalPropertyMethodsOnTrue = true;
$class = $builder->getClass($schema, 'Root');

$result = '';
foreach ($builder->getGeneratedClasses() as $class) {
$result .= $class->class . "\n\n";
}

$expected = file_get_contents(__DIR__ . '/../../../resources/JsonSchema/AdvancedTest/' . __FUNCTION__ . '-new-behavior.txt');

$this->assertSame($expected, $result, 'Getter and setter created (new behavior - flag: true)');
}
}