Skip to content

Commit e9502ae

Browse files
committed
Generate nested schemas on the fly
1 parent 07afa2b commit e9502ae

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

openapi-client.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
spec: https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.yaml
1+
spec: https://raw.githubusercontent.com/octokit/webhooks/openapi/payload-schemas/openapi-schema.yml
2+
#spec: https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.yaml
23
namespace: ApiClients\Client\Github
34
destination: generated

src/Generator/Schema.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ApiClients\Tools\OpenApiClientGenerator\File;
66
use cebe\openapi\spec\Schema as OpenAPiSchema;
7+
use Jawira\CaseConverter\Convert;
78
use PhpParser\Builder\Param;
89
use PhpParser\BuilderFactory;
910
use PhpParser\Node;
@@ -75,9 +76,15 @@ public static function generate(string $name, string $namespace, string $classNa
7576
)
7677
);
7778
if (is_string($property->type)) {
78-
if ($property->type === 'array' && $property->items instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) {
79-
$docBlock[] = '@var array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
80-
$docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '::class)';
79+
if ($property->type === 'array' && $property->items instanceof OpenAPiSchema) {
80+
if (array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) {
81+
$docBlock[] = '@var array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
82+
$docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '::class)';
83+
} elseif ($property->items->type === 'object') {
84+
yield from self::generate($name . '::' . $propertyName, $namespace . '\\' . $className, (new Convert($propertyName))->toPascal(), $property->items, $schemaClassNameMap);
85+
$docBlock[] = '@var array<\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '>';
86+
$docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '::class)';
87+
}
8188
}
8289
$t = str_replace([
8390
'object',

0 commit comments

Comments
 (0)