Skip to content

Correctly detect arrays of schema properties #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2023
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
77 changes: 12 additions & 65 deletions src/Gatherer/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
93 changes: 93 additions & 0 deletions src/Gatherer/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace ApiClients\Tools\OpenApiClientGenerator\Gatherer;

use ApiClients\Tools\OpenApiClientGenerator\Utils;
use ApiClients\Tools\OpenApiClientGenerator\Representation\PropertyType;
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
use cebe\openapi\spec\Schema as baseSchema;
use Jawira\CaseConverter\Convert;

final class Type
{
public static function gather(
string $className,
string $propertyName,
baseSchema $property,
bool $required,
SchemaRegistry $schemaRegistry,
): PropertyType {
$type = $property->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,
);
}
}
6 changes: 6 additions & 0 deletions src/Generator/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Representation/PropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
){
}
}