diff --git a/src/Model/GeneratorConfiguration.php b/src/Model/GeneratorConfiguration.php index 5bc9744..90f9980 100644 --- a/src/Model/GeneratorConfiguration.php +++ b/src/Model/GeneratorConfiguration.php @@ -41,6 +41,8 @@ class GeneratorConfiguration protected $errorRegistryClass = ErrorRegistryException::class; /** @var bool */ protected $serialization = false; + /** @var bool */ + protected $cacheEnabled = true; /** @var ClassNameGeneratorInterface */ protected $classNameGenerator; @@ -254,6 +256,18 @@ public function setImplicitNull(bool $allowImplicitNull): self return $this; } + public function setCacheEnabled(bool $cacheEnabled): self + { + $this->cacheEnabled = $cacheEnabled; + + return $this; + } + + public function isCacheEnabled(): bool + { + return $this->cacheEnabled; + } + private function initFilter(): void { $this diff --git a/src/Model/SchemaDefinition/SchemaDefinition.php b/src/Model/SchemaDefinition/SchemaDefinition.php index 0bf81a4..0c3f96b 100644 --- a/src/Model/SchemaDefinition/SchemaDefinition.php +++ b/src/Model/SchemaDefinition/SchemaDefinition.php @@ -67,7 +67,10 @@ public function resolveReference( $key = implode('-', $originalPath); - if (!$this->resolvedPaths->offsetExists($key)) { + $isCacheEnabled = $this->schemaProcessor->getGeneratorConfiguration()->isCacheEnabled(); + $isResolvedPath = $this->resolvedPaths->offsetExists($key); + + if (!$isCacheEnabled || !$isResolvedPath) { // create a dummy entry for the path first. If the path is used recursive the recursive usages will point // to the currently created property $this->resolvedPaths->offsetSet($key, null); diff --git a/tests/AbstractPHPModelGeneratorTestCase.php b/tests/AbstractPHPModelGeneratorTestCase.php index 78d4ada..982307c 100644 --- a/tests/AbstractPHPModelGeneratorTestCase.php +++ b/tests/AbstractPHPModelGeneratorTestCase.php @@ -130,6 +130,7 @@ protected function generateClassFromFile( bool $originalClassNames = false, bool $implicitNull = true, string $schemaProviderClass = RecursiveDirectoryProvider::class, + bool $cacheEnabled = true, ): string { return $this->generateClass( file_get_contents($this->getSchemaFilePath($file)), @@ -137,6 +138,7 @@ protected function generateClassFromFile( $originalClassNames, $implicitNull, $schemaProviderClass, + $cacheEnabled ); } @@ -156,6 +158,7 @@ protected function generateClassFromFileTemplate( bool $escape = true, bool $implicitNull = true, string $schemaProviderClass = RecursiveDirectoryProvider::class, + bool $cacheEnabled = true, ): string { return $this->generateClass( sprintf( @@ -170,6 +173,7 @@ protected function generateClassFromFileTemplate( false, $implicitNull, $schemaProviderClass, + $cacheEnabled ); } @@ -186,10 +190,12 @@ protected function generateClass( bool $originalClassNames = false, bool $implicitNull = true, string $schemaProviderClass = RecursiveDirectoryProvider::class, + bool $cacheEnabled = true, ): string { $generatorConfiguration = ($generatorConfiguration ?? (new GeneratorConfiguration())->setCollectErrors(false)) ->setImplicitNull($implicitNull) - ->setOutputEnabled(false); + ->setOutputEnabled(false) + ->setCacheEnabled($cacheEnabled); $baseDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'PHPModelGeneratorTest'; diff --git a/tests/Objects/ReferencePropertyTest.php b/tests/Objects/ReferencePropertyTest.php index 4a963ad..5024b85 100644 --- a/tests/Objects/ReferencePropertyTest.php +++ b/tests/Objects/ReferencePropertyTest.php @@ -6,6 +6,7 @@ use PHPModelGenerator\Exception\ErrorRegistryException; use PHPModelGenerator\Exception\FileSystemException; +use PHPModelGenerator\Exception\Object\RequiredValueException; use PHPModelGenerator\Exception\ValidationException; use PHPModelGenerator\Exception\RenderException; use PHPModelGenerator\Exception\SchemaException; @@ -547,4 +548,40 @@ public function invalidValuesForMultiplePropertiesWithIdenticalReferenceDataProv ], ]; } + + /** + * @throws FileSystemException + * @throws RenderException + * @throws SchemaException + */ + public function testValidCreateObjectRequiredAndOptionalPropertiesWithDefinitionValue(): void + { + $className = $this->generateClassFromFile( + 'RequiredAndOptionalPropertiesWithDefinitionValue.json', + cacheEnabled: false, + ); + + $object = new $className(['requiredProperty' => 'red']); + + $this->assertSame('red', $object->getRequiredProperty()); + $this->assertNull($object->getOptionalProperty()); + } + + /** + * @throws FileSystemException + * @throws RenderException + * @throws SchemaException + */ + public function testInvalidCreateObjectRequiredAndOptionalPropertiesWithDefinitionValue(): void + { + $className = $this->generateClassFromFile( + 'RequiredAndOptionalPropertiesWithDefinitionValue.json', + cacheEnabled: true, + ); + + $this->expectException(RequiredValueException::class); + $this->expectExceptionMessage('Missing required value for optionalProperty'); + + new $className(['requiredProperty' => 'red']); + } } diff --git a/tests/Schema/ReferencePropertyTest/RequiredAndOptionalPropertiesWithDefinitionValue.json b/tests/Schema/ReferencePropertyTest/RequiredAndOptionalPropertiesWithDefinitionValue.json new file mode 100644 index 0000000..aefdb29 --- /dev/null +++ b/tests/Schema/ReferencePropertyTest/RequiredAndOptionalPropertiesWithDefinitionValue.json @@ -0,0 +1,19 @@ +{ + "definitions": { + "forOptionalAndRequiredAtTheSameTime": { + "type": "string" + } + }, + "type": "object", + "properties": { + "requiredProperty": { + "$ref": "#/definitions/forOptionalAndRequiredAtTheSameTime" + }, + "optionalProperty": { + "$ref": "#/definitions/forOptionalAndRequiredAtTheSameTime" + } + }, + "required": [ + "requiredProperty" + ] +}