Skip to content
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

Nested Compositions #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace PHPModelGenerator\PropertyProcessor\ComposedValue;

use PHPModelGenerator\Exception\SchemaException;
use PHPModelGenerator\Model\MethodInterface;
use PHPModelGenerator\Model\Property\CompositionPropertyDecorator;
use PHPModelGenerator\Model\Property\Property;
use PHPModelGenerator\Model\Property\PropertyInterface;
use PHPModelGenerator\Model\Property\PropertyType;
use PHPModelGenerator\Model\Schema;
Expand All @@ -16,7 +14,6 @@
use PHPModelGenerator\Model\Validator\ComposedPropertyValidator;
use PHPModelGenerator\Model\Validator\RequiredPropertyValidator;
use PHPModelGenerator\PropertyProcessor\Decorator\Property\ObjectInstantiationDecorator;
use PHPModelGenerator\PropertyProcessor\Decorator\SchemaNamespaceTransferDecorator;
use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\ClearTypeHintDecorator;
use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\CompositionTypeHintDecorator;
use PHPModelGenerator\PropertyProcessor\Property\AbstractValueProcessor;
Expand Down Expand Up @@ -90,6 +87,10 @@ function () use (&$resolvedCompositions, $property, $compositionProperties, $pro
$propertySchema
)
: null;

if ($this->mergedProperty) {
$property->setNestedSchema($this->mergedProperty->getNestedSchema());
}
}
}
);
Expand Down Expand Up @@ -175,6 +176,20 @@ protected function getCompositionProperties(PropertyInterface $property, JsonSch
}
});

if ($compositionProperty->isResolved()
&& $this instanceof OneOfProcessor
&& count($compositionProperty->getJsonSchema()->getJson()) === 1
&& array_key_exists('example', $compositionProperty->getJsonSchema()->getJson())
) {
if ($this->schemaProcessor->getGeneratorConfiguration()->isOutputEnabled()) {
// @codeCoverageIgnoreStart
echo "Warning: example OneOf branch for {$property->getName()} may lead to unexpected results, skipping branch\n";
// @codeCoverageIgnoreEnd
}

continue;
}

$compositionProperties[] = $compositionProperty;
}

Expand Down
4 changes: 4 additions & 0 deletions src/PropertyProcessor/Property/AbstractPropertyProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ protected function addComposedValueValidator(PropertyInterface $property, JsonSc
])
);

if ($composedProperty->getNestedSchema()) {
$property->setNestedSchema($composedProperty->getNestedSchema());
}

foreach ($composedProperty->getValidators() as $validator) {
$property->addValidator($validator->getValidator(), $validator->getPriority());
}
Expand Down
12 changes: 7 additions & 5 deletions src/SchemaProcessor/SchemaProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,15 @@ private function transferPropertiesToMergedSchema(
$property->getNestedSchema()->onAllPropertiesResolved(
function () use ($property, $schema, $mergedPropertySchema): void {
foreach ($property->getNestedSchema()->getProperties() as $nestedProperty) {
$mergedPropertySchema->addProperty(
$attachProperty = clone $nestedProperty;

// don't validate fields in merged properties. All fields were validated before
// corresponding to the defined constraints of the composition property.
(clone $nestedProperty)->filterValidators(static function (): bool {
return false;
})
);
$attachProperty->filterValidators(static function (): bool {
return false;
});

$mergedPropertySchema->addProperty($attachProperty);
}
}
);
Expand Down
46 changes: 46 additions & 0 deletions tests/Issues/Issue/Issue72Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace PHPModelGenerator\Tests\Issues\Issue;

use PHPModelGenerator\Tests\Issues\AbstractIssueTest;

class Issue72Test extends AbstractIssueTest
{
/**
* If a root OneOf composition uses branches which only provide examples (which should cause every input to pass)
* ignore those branches as it's most likely an error in the schema. The generation process will emit a warning.
*/
public function testExampleOneOfBranchIsSkipped(): void
{
$className = $this->generateClassFromFile('OneOfExample.json');

$object = new $className(['label' => 'Hannes']);
$this->assertSame('Hannes', $object->getLabel());
}

public function testNestedAllOf(): void
{
$this->markTestSkipped('Not functional yet');

$className = $this->generateClassFromFile('NestedAllOf.json');

$company = new $className([
'CEO' => [
'yearsInCompany' => 10,
'name' => 'Hannes',
'salary' => 10000,
'assistance' => [
'yearsInCompany' => 4,
'name' => 'Dieter',
'salary' => 8000,
],
],
]);

$this->assertSame(10, $company->getCEO()->getYearsInCompany());
$this->assertSame('Hannes', $company->getCEO()->getName());
$this->assertSame(10000, $company->getCEO()->getSalary());
}
}
60 changes: 60 additions & 0 deletions tests/Schema/Issues/72/NestedAllOf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"definitions": {
"basic": {
"allOf": [
{
"$ref": "#/definitions/identification"
},
{
"type": "object",
"properties": {
"yearsInCompany": {
"type": "integer"
}
}
}
]
},
"identification": {
"allOf": [
{
"$ref": "#/definitions/contract"
},
{
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
]
},
"contract": {
"type": "object",
"properties": {
"salary": {
"type": "integer"
}
}
}
},
"type": "object",
"properties": {
"CEO": {
"allOf": [
{
"$ref": "#/definitions/basic"
},
{
"type": "object",
"properties": {
"assistance": {
"$ref": "#/definitions/basic"
}
}
}
]
}
}
}
26 changes: 26 additions & 0 deletions tests/Schema/Issues/72/OneOfExample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"definitions": {
"name": {
"type": "object",
"properties": {
"label": {
"type": "string"
}
}
},
"nameExample": {
"example": {
"label": "ABC"
}
}
},
"type": "object",
"oneOf": [
{
"$ref": "#/definitions/name"
},
{
"$ref": "#/definitions/nameExample"
}
]
}