From f11e4e2713222e92556a76334573a1559ec32528 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Tue, 28 Feb 2023 11:14:24 +0100 Subject: [PATCH] Correctly detect arrays of schema properties --- src/Gatherer/Property.php | 77 ++++-------------------- src/Gatherer/Type.php | 93 +++++++++++++++++++++++++++++ src/Generator/Schema.php | 6 ++ src/Representation/PropertyType.php | 3 +- 4 files changed, 113 insertions(+), 66 deletions(-) create mode 100644 src/Gatherer/Type.php diff --git a/src/Gatherer/Property.php b/src/Gatherer/Property.php index 8349ce8..e67e7a4 100644 --- a/src/Gatherer/Property.php +++ b/src/Gatherer/Property.php @@ -37,84 +37,31 @@ public static function gather( '', ], $propertyName); - $type = $property->type; - $nullable = !$required; - - if ( - is_array($type) && - count($type) === 2 && - ( - in_array(null, $type) || - in_array("null", $type) - ) - ) { - foreach ($type as $pt) { - if ($pt !== null && $pt !== "null") { - $type = $pt; - break; - } - } - - $nullable = true; - } - - if (is_string($type)) { - $type = str_replace([ - 'integer', - 'number', - 'any', - 'null', - 'boolean', - ], [ - 'int', - 'int', - '', - '', - 'bool', - ], $type); - } else { - $type = ''; - } - if ($type === '') { - $type = new PropertyType( - 'scalar', - 'mixed' - ); - $nullable = false; - } else if ($type === 'object') { - $type = new PropertyType( - 'object', - Schema::gather( - $schemaRegistry->get($property, $className . '\\' . Utils::className($propertyName)), - $property, - $schemaRegistry, - ) - ); + $type = Type::gather( + $className, + $propertyName, + $property, + $required, + $schemaRegistry, + ); + if ($type->type=== 'object') { $exampleData = ($exampleData ?? []) + $type->payload->example; } else { if ($exampleData === null) { - if ($type === 'int') { + if ($type->type === 'int') { $exampleData = 13; - } elseif ($type === 'bool') { + } elseif ($type->type === 'bool') { $exampleData = false; } else { $exampleData = 'generated_' . $propertyName; } } - $type = new PropertyType( - 'scalar', - $type - ); } - if ($type->payload === 'array') { + if ($type->type === 'array') { $exampleData = [$exampleData]; } - if (!is_array($type)) { - $type = [$type]; - } - - return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Property($propertyName, $property->description ?? '', $exampleData, $type, $nullable); + return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Property($propertyName, $property->description ?? '', $exampleData, [$type], $type->nullable); } } diff --git a/src/Gatherer/Type.php b/src/Gatherer/Type.php new file mode 100644 index 0000000..51717f5 --- /dev/null +++ b/src/Gatherer/Type.php @@ -0,0 +1,93 @@ +type; + $nullable = !$required; + + if ($type === 'array') { + return new PropertyType( + 'array', + Type::gather($className, $propertyName, $property->items, $required, $schemaRegistry), + $nullable + ); + } + + if ( + is_array($type) && + count($type) === 2 && + ( + in_array(null, $type) || + in_array("null", $type) + ) + ) { + foreach ($type as $pt) { + if ($pt !== null && $pt !== "null") { + $type = $pt; + break; + } + } + + $nullable = true; + } + + if (is_string($type)) { + $type = str_replace([ + 'integer', + 'number', + 'any', + 'null', + 'boolean', + ], [ + 'int', + 'int', + '', + '', + 'bool', + ], $type); + } else { + $type = ''; + } + + if ($type === '') { + return new PropertyType( + 'scalar', + 'mixed', + false, + ); + } + + if ($type === 'object') { + return new PropertyType( + 'object', + Schema::gather( + $schemaRegistry->get($property, $className . '\\' . Utils::className($propertyName)), + $property, + $schemaRegistry, + ), + $nullable, + ); + } + + return new PropertyType( + 'scalar', + $type, + $nullable, + ); + } +} diff --git a/src/Generator/Schema.php b/src/Generator/Schema.php index 6d5d1b9..db1c907 100644 --- a/src/Generator/Schema.php +++ b/src/Generator/Schema.php @@ -95,6 +95,12 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie $types = []; foreach ($property->type as $type) { + if ($type->type === 'array') { + $propertyDocBlock[] = '@var array<' . ($type->payload->payload instanceof \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema ? 'Schema\\' . $type->payload->payload->className : $type->payload->payload) . '>'; + $types[] = 'array'; + continue; + } + if ($type->payload instanceof \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema) { $types[] = 'Schema\\' . $type->payload->className; continue; diff --git a/src/Representation/PropertyType.php b/src/Representation/PropertyType.php index 0f8c49d..858069c 100644 --- a/src/Representation/PropertyType.php +++ b/src/Representation/PropertyType.php @@ -6,7 +6,8 @@ final class PropertyType { public function __construct( public readonly string $type, - public readonly string|Schema $payload, + public readonly string|Schema|PropertyType $payload, + public readonly bool $nullable, ){ } }