Skip to content

Commit 4dd2b04

Browse files
authored
Merge pull request #98 from php-api-clients/correctly-detect-arrays-of-schema-properties
Correctly detect arrays of schema properties
2 parents 753f8b6 + f11e4e2 commit 4dd2b04

File tree

4 files changed

+113
-66
lines changed

4 files changed

+113
-66
lines changed

src/Gatherer/Property.php

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,84 +37,31 @@ public static function gather(
3737
'',
3838
], $propertyName);
3939

40-
$type = $property->type;
41-
$nullable = !$required;
42-
43-
if (
44-
is_array($type) &&
45-
count($type) === 2 &&
46-
(
47-
in_array(null, $type) ||
48-
in_array("null", $type)
49-
)
50-
) {
51-
foreach ($type as $pt) {
52-
if ($pt !== null && $pt !== "null") {
53-
$type = $pt;
54-
break;
55-
}
56-
}
57-
58-
$nullable = true;
59-
}
60-
61-
if (is_string($type)) {
62-
$type = str_replace([
63-
'integer',
64-
'number',
65-
'any',
66-
'null',
67-
'boolean',
68-
], [
69-
'int',
70-
'int',
71-
'',
72-
'',
73-
'bool',
74-
], $type);
75-
} else {
76-
$type = '';
77-
}
78-
if ($type === '') {
79-
$type = new PropertyType(
80-
'scalar',
81-
'mixed'
82-
);
83-
$nullable = false;
84-
} else if ($type === 'object') {
85-
$type = new PropertyType(
86-
'object',
87-
Schema::gather(
88-
$schemaRegistry->get($property, $className . '\\' . Utils::className($propertyName)),
89-
$property,
90-
$schemaRegistry,
91-
)
92-
);
40+
$type = Type::gather(
41+
$className,
42+
$propertyName,
43+
$property,
44+
$required,
45+
$schemaRegistry,
46+
);
47+
if ($type->type=== 'object') {
9348
$exampleData = ($exampleData ?? []) + $type->payload->example;
9449
} else {
9550
if ($exampleData === null) {
96-
if ($type === 'int') {
51+
if ($type->type === 'int') {
9752
$exampleData = 13;
98-
} elseif ($type === 'bool') {
53+
} elseif ($type->type === 'bool') {
9954
$exampleData = false;
10055
} else {
10156
$exampleData = 'generated_' . $propertyName;
10257
}
10358
}
104-
$type = new PropertyType(
105-
'scalar',
106-
$type
107-
);
10859
}
10960

110-
if ($type->payload === 'array') {
61+
if ($type->type === 'array') {
11162
$exampleData = [$exampleData];
11263
}
11364

114-
if (!is_array($type)) {
115-
$type = [$type];
116-
}
117-
118-
return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Property($propertyName, $property->description ?? '', $exampleData, $type, $nullable);
65+
return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Property($propertyName, $property->description ?? '', $exampleData, [$type], $type->nullable);
11966
}
12067
}

src/Gatherer/Type.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace ApiClients\Tools\OpenApiClientGenerator\Gatherer;
4+
5+
use ApiClients\Tools\OpenApiClientGenerator\Utils;
6+
use ApiClients\Tools\OpenApiClientGenerator\Representation\PropertyType;
7+
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
8+
use cebe\openapi\spec\Schema as baseSchema;
9+
use Jawira\CaseConverter\Convert;
10+
11+
final class Type
12+
{
13+
public static function gather(
14+
string $className,
15+
string $propertyName,
16+
baseSchema $property,
17+
bool $required,
18+
SchemaRegistry $schemaRegistry,
19+
): PropertyType {
20+
$type = $property->type;
21+
$nullable = !$required;
22+
23+
if ($type === 'array') {
24+
return new PropertyType(
25+
'array',
26+
Type::gather($className, $propertyName, $property->items, $required, $schemaRegistry),
27+
$nullable
28+
);
29+
}
30+
31+
if (
32+
is_array($type) &&
33+
count($type) === 2 &&
34+
(
35+
in_array(null, $type) ||
36+
in_array("null", $type)
37+
)
38+
) {
39+
foreach ($type as $pt) {
40+
if ($pt !== null && $pt !== "null") {
41+
$type = $pt;
42+
break;
43+
}
44+
}
45+
46+
$nullable = true;
47+
}
48+
49+
if (is_string($type)) {
50+
$type = str_replace([
51+
'integer',
52+
'number',
53+
'any',
54+
'null',
55+
'boolean',
56+
], [
57+
'int',
58+
'int',
59+
'',
60+
'',
61+
'bool',
62+
], $type);
63+
} else {
64+
$type = '';
65+
}
66+
67+
if ($type === '') {
68+
return new PropertyType(
69+
'scalar',
70+
'mixed',
71+
false,
72+
);
73+
}
74+
75+
if ($type === 'object') {
76+
return new PropertyType(
77+
'object',
78+
Schema::gather(
79+
$schemaRegistry->get($property, $className . '\\' . Utils::className($propertyName)),
80+
$property,
81+
$schemaRegistry,
82+
),
83+
$nullable,
84+
);
85+
}
86+
87+
return new PropertyType(
88+
'scalar',
89+
$type,
90+
$nullable,
91+
);
92+
}
93+
}

src/Generator/Schema.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
9595

9696
$types = [];
9797
foreach ($property->type as $type) {
98+
if ($type->type === 'array') {
99+
$propertyDocBlock[] = '@var array<' . ($type->payload->payload instanceof \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema ? 'Schema\\' . $type->payload->payload->className : $type->payload->payload) . '>';
100+
$types[] = 'array';
101+
continue;
102+
}
103+
98104
if ($type->payload instanceof \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema) {
99105
$types[] = 'Schema\\' . $type->payload->className;
100106
continue;

src/Representation/PropertyType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ final class PropertyType
66
{
77
public function __construct(
88
public readonly string $type,
9-
public readonly string|Schema $payload,
9+
public readonly string|Schema|PropertyType $payload,
10+
public readonly bool $nullable,
1011
){
1112
}
1213
}

0 commit comments

Comments
 (0)