diff --git a/src/Generator/ClassTransformer.php b/src/Generator/ClassTransformer.php index a5da35e..8ab5ecc 100644 --- a/src/Generator/ClassTransformer.php +++ b/src/Generator/ClassTransformer.php @@ -151,6 +151,8 @@ public function transform( } if ($type === Types::Object) { + $property = $this->applyNamespaceToSchema($schema, $property); + $inlineObject = $this->transformInlineObject( $configuration, $openApi, @@ -511,6 +513,8 @@ private function resolveArrayType( } if ($arrayType === Types::Object) { + $itemsSchema = $this->applyNamespaceToSchema($schema, $itemsSchema); + $inlineObject = $this->transformInlineObject( $configuration, $openApi, @@ -790,4 +794,17 @@ private function resolveNamespace( return $this->namespaceResolver->resolveNamespace($openApiType, $schema); } + + private function applyNamespaceToSchema(Schema $baseSchema, Schema $schema): Schema + { + // @phpstan-ignore-next-line + $xPhpNamespace = $baseSchema->{'x-php-namespace'} ?? null; + + if (is_string($xPhpNamespace)) { + // @phpstan-ignore-next-line + $schema->{'x-php-namespace'} = $xPhpNamespace; + } + + return $schema; + } } diff --git a/test/Acceptance/AcceptanceTest.php b/test/Acceptance/AcceptanceTest.php index 6753deb..cb3534e 100644 --- a/test/Acceptance/AcceptanceTest.php +++ b/test/Acceptance/AcceptanceTest.php @@ -5,6 +5,9 @@ namespace Reinfi\OpenApiModels\Test\Acceptance; use PHPUnit\Framework\TestCase; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; +use SplFileInfo; class AcceptanceTest extends TestCase { @@ -19,12 +22,19 @@ public function testApplicationRuns(): void self::assertNotNull($output); self::assertNotFalse($output); - $expectedFiles = glob(__DIR__ . '/ExpectedClasses/**/*.php'); - self::assertNotFalse($expectedFiles); + $baseDirectoryPath = __DIR__ . '/ExpectedClasses'; + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDirectoryPath)); + $expectedFiles = []; + foreach ($iterator as $file) { + if ($file instanceof SplFileInfo && $file->isFile() && $file->getExtension() === 'php') { + $expectedFiles[] = $file->getPathname(); + } + } foreach ($expectedFiles as $file) { - $fileName = basename($file); - $fileDirectory = basename(dirname($file)); + $fileWithoutBaseDirectory = str_replace($baseDirectoryPath, '', $file); + $fileName = basename($fileWithoutBaseDirectory); + $fileDirectory = ltrim(dirname($fileWithoutBaseDirectory)); self::assertFileEquals($file, sprintf(__DIR__ . '/../output/%s/%s', $fileDirectory, $fileName)); } diff --git a/test/Acceptance/ExpectedClasses/Schema/Test/Test23InlineObjectArrayNamespace.php b/test/Acceptance/ExpectedClasses/Schema/Test/Test23InlineObjectArrayNamespace.php new file mode 100644 index 0000000..9140443 --- /dev/null +++ b/test/Acceptance/ExpectedClasses/Schema/Test/Test23InlineObjectArrayNamespace.php @@ -0,0 +1,66 @@ + + * @implements \IteratorAggregate + */ +readonly class Test23InlineObjectArrayNamespace implements IteratorAggregate, Countable, ArrayAccess, JsonSerializable +{ + /** @var array $items */ + private array $items; + + public function __construct(Test23InlineObjectArrayNamespaceItems ...$items) + { + $this->items = $items; + } + + public function getIterator(): Traversable + { + return new ArrayIterator($this->items); + } + + public function count(): int + { + return count($this->items); + } + + public function offsetExists(mixed $offset): bool + { + return isset($this->items[$offset]); + } + + public function offsetGet(mixed $offset): ?Test23InlineObjectArrayNamespaceItems + { + return $this->items[$offset] ?? null; + } + + public function offsetSet(mixed $offset, mixed $value): void + { + throw new BadMethodCallException('Object is readOnly'); + } + + public function offsetUnset(mixed $offset): void + { + throw new BadMethodCallException('Object is readOnly'); + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return $this->items; + } +} diff --git a/test/Acceptance/ExpectedClasses/Schema/Test/Test23InlineObjectArrayNamespaceItems.php b/test/Acceptance/ExpectedClasses/Schema/Test/Test23InlineObjectArrayNamespaceItems.php new file mode 100644 index 0000000..4ba61b6 --- /dev/null +++ b/test/Acceptance/ExpectedClasses/Schema/Test/Test23InlineObjectArrayNamespaceItems.php @@ -0,0 +1,13 @@ + + * @implements \IteratorAggregate + */ +readonly class Test23InlineObjectArray implements IteratorAggregate, Countable, ArrayAccess, JsonSerializable +{ + /** @var array $items */ + private array $items; + + public function __construct(Test23InlineObjectArrayItems ...$items) + { + $this->items = $items; + } + + public function getIterator(): Traversable + { + return new ArrayIterator($this->items); + } + + public function count(): int + { + return count($this->items); + } + + public function offsetExists(mixed $offset): bool + { + return isset($this->items[$offset]); + } + + public function offsetGet(mixed $offset): ?Test23InlineObjectArrayItems + { + return $this->items[$offset] ?? null; + } + + public function offsetSet(mixed $offset, mixed $value): void + { + throw new BadMethodCallException('Object is readOnly'); + } + + public function offsetUnset(mixed $offset): void + { + throw new BadMethodCallException('Object is readOnly'); + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return $this->items; + } +} diff --git a/test/Acceptance/ExpectedClasses/Schema/Test23InlineObjectArrayItems.php b/test/Acceptance/ExpectedClasses/Schema/Test23InlineObjectArrayItems.php new file mode 100644 index 0000000..addeca5 --- /dev/null +++ b/test/Acceptance/ExpectedClasses/Schema/Test23InlineObjectArrayItems.php @@ -0,0 +1,13 @@ +