Skip to content

Commit d79d29e

Browse files
committed
Fix missing required check for required referenced property inside a composition
1 parent 9df6a30 commit d79d29e

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

src/Model/Property/PropertyProxy.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class PropertyProxy extends AbstractProperty
2121
{
2222
/** @var string */
2323
protected $key;
24-
/** @var string */
25-
protected $name;
2624
/** @var ResolvedDefinitionsCollection */
2725
protected $definitionsCollection;
2826

@@ -249,4 +247,11 @@ public function isInternal(): bool
249247
{
250248
return $this->getProperty()->isInternal();
251249
}
250+
251+
public function __clone()
252+
{
253+
$cloneKey = $this->key . uniqid();
254+
$this->definitionsCollection->offsetSet($cloneKey, clone $this->definitionsCollection->offsetGet($this->key));
255+
$this->key = $cloneKey;
256+
}
252257
}

tests/Objects/RequiredPropertyTest.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

3-
namespace PHPModelGenerator\Tests\Basic;
3+
namespace PHPModelGenerator\Tests\Objects;
44

5+
use PHPModelGenerator\Exception\ErrorRegistryException;
56
use PHPModelGenerator\Exception\FileSystemException;
67
use PHPModelGenerator\Exception\ValidationException;
78
use PHPModelGenerator\Exception\RenderException;
@@ -41,6 +42,7 @@ public function requiredDefinitionsDataProvider(): array
4142
[
4243
'Defined property' => ['RequiredStringProperty.json'],
4344
'Undefined property' => ['RequiredUndefinedProperty.json'],
45+
'Reference in composition' => ['RequiredReferencePropertyInComposition.json'],
4446
]
4547
);
4648
}
@@ -67,12 +69,12 @@ public function validStringPropertyValueProvider(): array
6769
*/
6870
public function testNotProvidedRequiredPropertyThrowsAnException(bool $implicitNull, string $file): void
6971
{
70-
$this->expectException(ValidationException::class);
71-
$this->expectExceptionMessage("Missing required value for property");
72+
$this->expectException(ErrorRegistryException::class);
73+
$this->expectExceptionMessageMatches("/Missing required value for property/");
7274

7375
$className = $this->generateClassFromFile(
7476
$file,
75-
(new GeneratorConfiguration())->setCollectErrors(false),
77+
(new GeneratorConfiguration())->setCollectErrors(true),
7678
false,
7779
$implicitNull
7880
);
@@ -81,14 +83,14 @@ public function testNotProvidedRequiredPropertyThrowsAnException(bool $implicitN
8183
}
8284

8385
/**
84-
* @dataProvider implicitNullDataProvider
86+
* @dataProvider requiredStringPropertyDataProvider
8587
*
8688
* @param bool $implicitNull
8789
*/
88-
public function testRequiredPropertyType(bool $implicitNull): void
90+
public function testRequiredPropertyType(bool $implicitNull, string $schemaFile): void
8991
{
9092
$className = $this->generateClassFromFile(
91-
'RequiredStringProperty.json',
93+
$schemaFile,
9294
(new GeneratorConfiguration())->setImmutable(false),
9395
false,
9496
$implicitNull
@@ -125,26 +127,37 @@ public function testUndefinedRequiredPropertyType(bool $implicitNull): void
125127
}
126128

127129
/**
128-
* @dataProvider implicitNullDataProvider
130+
* @dataProvider requiredStringPropertyDataProvider
129131
*
130132
* @param bool $implicitNull
131133
*
132134
* @throws FileSystemException
133135
* @throws RenderException
134136
* @throws SchemaException
135137
*/
136-
public function testNullProvidedForRequiredPropertyThrowsAnException(bool $implicitNull): void
138+
public function testNullProvidedForRequiredPropertyThrowsAnException(bool $implicitNull, string $schemaFile): void
137139
{
138-
$this->expectException(ValidationException::class);
139-
$this->expectExceptionMessage("Invalid type for property");
140+
$this->expectException(ErrorRegistryException::class);
141+
$this->expectExceptionMessageMatches("/Invalid type for property/");
140142

141143
$className = $this->generateClassFromFile(
142-
'RequiredStringProperty.json',
143-
(new GeneratorConfiguration())->setCollectErrors(false),
144+
$schemaFile,
145+
(new GeneratorConfiguration())->setCollectErrors(true),
144146
false,
145147
$implicitNull
146148
);
147149

148150
new $className(['property' => null]);
149151
}
152+
153+
public function requiredStringPropertyDataProvider(): array
154+
{
155+
return $this->combineDataProvider(
156+
$this->implicitNullDataProvider(),
157+
[
158+
'RequiredStringProperty' => ['RequiredStringProperty.json'],
159+
'RequiredReferencePropertyInComposition' => ['RequiredReferencePropertyInComposition.json'],
160+
]
161+
);
162+
}
150163
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "object",
3+
"definitions": {
4+
"property": {
5+
"type": "string"
6+
}
7+
},
8+
"allOf": [
9+
{
10+
"type": "object",
11+
"properties": {
12+
"property": {
13+
"$ref": "#/definitions/property"
14+
}
15+
},
16+
"required": [
17+
"property"
18+
]
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)