From e9502aea7104e2c506130396ed28d00260479cf7 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 29 May 2021 15:23:53 +0200 Subject: [PATCH] Generate nested schemas on the fly --- openapi-client.yaml | 3 ++- src/Generator/Schema.php | 13 ++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/openapi-client.yaml b/openapi-client.yaml index ee522ba..025d0d2 100644 --- a/openapi-client.yaml +++ b/openapi-client.yaml @@ -1,3 +1,4 @@ -spec: https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.yaml +spec: https://raw.githubusercontent.com/octokit/webhooks/openapi/payload-schemas/openapi-schema.yml +#spec: https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.yaml namespace: ApiClients\Client\Github destination: generated diff --git a/src/Generator/Schema.php b/src/Generator/Schema.php index f90d39d..a3976bf 100644 --- a/src/Generator/Schema.php +++ b/src/Generator/Schema.php @@ -4,6 +4,7 @@ use ApiClients\Tools\OpenApiClientGenerator\File; use cebe\openapi\spec\Schema as OpenAPiSchema; +use Jawira\CaseConverter\Convert; use PhpParser\Builder\Param; use PhpParser\BuilderFactory; use PhpParser\Node; @@ -75,9 +76,15 @@ public static function generate(string $name, string $namespace, string $classNa ) ); if (is_string($property->type)) { - if ($property->type === 'array' && $property->items instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) { - $docBlock[] = '@var array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>'; - $docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '::class)'; + if ($property->type === 'array' && $property->items instanceof OpenAPiSchema) { + if (array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) { + $docBlock[] = '@var array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>'; + $docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '::class)'; + } elseif ($property->items->type === 'object') { + yield from self::generate($name . '::' . $propertyName, $namespace . '\\' . $className, (new Convert($propertyName))->toPascal(), $property->items, $schemaClassNameMap); + $docBlock[] = '@var array<\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '>'; + $docBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '::class)'; + } } $t = str_replace([ 'object',