Skip to content
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
17 changes: 17 additions & 0 deletions src/Generator/ClassTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public function transform(
}

if ($type === Types::Object) {
$property = $this->applyNamespaceToSchema($schema, $property);

$inlineObject = $this->transformInlineObject(
$configuration,
$openApi,
Expand Down Expand Up @@ -511,6 +513,8 @@ private function resolveArrayType(
}

if ($arrayType === Types::Object) {
$itemsSchema = $this->applyNamespaceToSchema($schema, $itemsSchema);

$inlineObject = $this->transformInlineObject(
$configuration,
$openApi,
Expand Down Expand Up @@ -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;
}
}
18 changes: 14 additions & 4 deletions test/Acceptance/AcceptanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Reinfi\OpenApiModels\Test\Acceptance;

use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;

class AcceptanceTest extends TestCase
{
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Api\Schema\Test;

use ArrayAccess;
use ArrayIterator;
use BadMethodCallException;
use Countable;
use IteratorAggregate;
use JsonSerializable;
use Traversable;

/**
* @implements \ArrayAccess<int, Test23InlineObjectArrayNamespaceItems>
* @implements \IteratorAggregate<Test23InlineObjectArrayNamespaceItems>
*/
readonly class Test23InlineObjectArrayNamespace implements IteratorAggregate, Countable, ArrayAccess, JsonSerializable
{
/** @var array<Test23InlineObjectArrayNamespaceItems> $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<Test23InlineObjectArrayNamespaceItems>
*/
public function jsonSerialize(): array
{
return $this->items;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Api\Schema\Test;

readonly class Test23InlineObjectArrayNamespaceItems
{
public function __construct(
public string $id,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Api\Schema\Test;

readonly class Test23InlineObjectNamespace
{
public function __construct(
public Test23InlineObjectNamespaceUser $user,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Api\Schema\Test;

readonly class Test23InlineObjectNamespaceUser
{
public function __construct(
public string $id,
) {
}
}
66 changes: 66 additions & 0 deletions test/Acceptance/ExpectedClasses/Schema/Test23InlineObjectArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Api\Schema;

use ArrayAccess;
use ArrayIterator;
use BadMethodCallException;
use Countable;
use IteratorAggregate;
use JsonSerializable;
use Traversable;

/**
* @implements \ArrayAccess<int, Test23InlineObjectArrayItems>
* @implements \IteratorAggregate<Test23InlineObjectArrayItems>
*/
readonly class Test23InlineObjectArray implements IteratorAggregate, Countable, ArrayAccess, JsonSerializable
{
/** @var array<Test23InlineObjectArrayItems> $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<Test23InlineObjectArrayItems>
*/
public function jsonSerialize(): array
{
return $this->items;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Api\Schema;

readonly class Test23InlineObjectArrayItems
{
public function __construct(
public string $id,
) {
}
}
37 changes: 36 additions & 1 deletion test/spec/acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
title: Acceptance Test
version: 0.0.1

paths: {}
paths: { }

components:
schemas:
Expand Down Expand Up @@ -451,6 +451,41 @@ components:
Test22:
$ref: './models/TestObject22.yaml'

Test23InlineObjectArray:
type: array
items:
type: object
properties:
id:
type: string
required:
- id

Test23InlineObjectArrayNamespace:
x-php-namespace: Test
type: array
items:
type: object
properties:
id:
type: string
required:
- id
-
Test23InlineObjectNamespace:
x-php-namespace: Test
type: object
properties:
user:
type: object
properties:
id:
type: string
required:
- id
required:
- user

requestBodies:
RequestBody1:
content:
Expand Down
Loading