Skip to content

Commit 2c2147a

Browse files
committed
Make as many unit tests as possible pass
1 parent a39ed4d commit 2c2147a

File tree

141 files changed

+5494
-1520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+5494
-1520
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"jawira/case-converter": "^3.5",
2222
"kwn/number-to-words": "^2.6",
2323
"league/openapi-psr7-validator": "^0.21",
24+
"league/uri": "^6.8 || ^7.3",
2425
"nikic/php-parser": "^4.15",
2526
"nunomaduro/termwind": "^1.15",
2627
"ondram/ci-detector": "^4.1",
@@ -55,10 +56,10 @@
5556
],
5657
"config": {
5758
"allow-plugins": {
58-
"wyrihaximus/composer-update-bin-autoload-path": true,
59-
"infection/extension-installer": true,
6059
"dealerdirect/phpcodesniffer-composer-installer": true,
61-
"ergebnis/composer-normalize": true
60+
"ergebnis/composer-normalize": true,
61+
"infection/extension-installer": true,
62+
"wyrihaximus/composer-update-bin-autoload-path": true
6263
},
6364
"platform": {
6465
"php": "8.2.13"

composer.lock

Lines changed: 360 additions & 286 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/templates/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"devizzent/cebe-php-openapi": "^1",
2121
"eventsauce/object-hydrator": "^1.1",
2222
"league/openapi-psr7-validator": "^0.21",
23+
"league/uri": "^7.3 || ^6.8",
2324
"psr/http-message": "^1.0",
2425
"react/http": "^1.8",
2526
"react/async": "^4.0",

src/Gatherer/ExampleData.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
use ReverseRegex\Lexer;
1515
use ReverseRegex\Parser;
1616

17+
use function explode;
1718
use function gettype;
1819
use function is_array;
1920
use function is_string;
2021
use function Safe\date;
2122
use function Safe\json_encode;
2223
use function strlen;
24+
use function strpos;
2325

2426
final class ExampleData
2527
{
2628
public static function gather(mixed $exampleData, PropertyType $type, string $propertyName): Representation\ExampleData
2729
{
28-
if ($type->type === 'array') {
30+
if ($type->type === 'array' || $type->type === 'union') {
2931
if ($type->payload instanceof Schema) {
3032
$exampleData = ArrayMerger::doMerge(
3133
$type->payload->example,
@@ -34,6 +36,11 @@ public static function gather(mixed $exampleData, PropertyType $type, string $pr
3436
);
3537
} elseif ($type->payload instanceof PropertyType) {
3638
return self::gather($exampleData, $type->payload, $propertyName);
39+
40+
// } elseif (is_array($type->payload)) {
41+
// $exampleData = [
42+
// self::gather($exampleData, $type->payload[0], $propertyName),
43+
// ];
3744
}
3845

3946
return new Representation\ExampleData($exampleData, $exampleData instanceof Node\Expr ? $exampleData : self::turnArrayIntoNode((array) $exampleData));
@@ -90,6 +97,12 @@ public static function determiteType(mixed $exampleData): Representation\Example
9097
/** @phpstan-ignore-next-line */
9198
public static function scalarData(int $seed, string $type, string|null $format, string|null $pattern = null): Representation\ExampleData
9299
{
100+
if (strpos($type, '|') !== false) {
101+
[$firstType] = explode('|', $type);
102+
103+
return self::scalarData($seed, $firstType, $format, $pattern);
104+
}
105+
93106
if ($type === 'int' || $type === '?int') {
94107
return new Representation\ExampleData($seed, new Node\Scalar\LNumber($seed));
95108
}
@@ -149,6 +162,23 @@ public static function scalarData(int $seed, string $type, string|null $format,
149162
return new Representation\ExampleData('generated', new Node\Scalar\String_('generated'));
150163
}
151164

165+
if ($type === 'array' || $type === '?array') {
166+
$string = self::scalarData($seed, 'string', $format, $pattern);
167+
168+
return new Representation\ExampleData(
169+
[
170+
$string->raw,
171+
],
172+
new Node\Expr\Array_(
173+
[
174+
new Node\Expr\ArrayItem(
175+
$string->node,
176+
),
177+
],
178+
),
179+
);
180+
}
181+
152182
return new Representation\ExampleData(
153183
null,
154184
new Node\Expr\ConstFetch(

src/Gatherer/Operation.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use ApiClients\Tools\OpenApiClientGenerator\ClassString;
88
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_;
9+
use ApiClients\Tools\OpenApiClientGenerator\Registry\Contract as ContractRegistry;
910
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
1011
use ApiClients\Tools\OpenApiClientGenerator\Registry\ThrowableSchema;
1112
use ApiClients\Tools\OpenApiClientGenerator\Representation\Header;
@@ -45,11 +46,20 @@ public static function gather(
4546
openAPIOperation $operation,
4647
ThrowableSchema $throwableSchemaRegistry,
4748
SchemaRegistry $schemaRegistry,
49+
ContractRegistry $contractRegistry,
4850
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Operation {
4951
$returnType = [];
5052
$parameters = [];
5153
$empties = [];
5254
foreach ($operation->parameters as $parameter) {
55+
$types = is_array($parameter->schema->type) ? $parameter->schema->type : [$parameter->schema->type];
56+
if (count($parameter->schema->oneOf ?? []) > 0) {
57+
$types = [];
58+
foreach ($parameter->schema->oneOf as $oneOfSchema) {
59+
$types[] = $oneOfSchema->type;
60+
}
61+
}
62+
5363
$parameterType = str_replace([
5464
'integer',
5565
'any',
@@ -58,7 +68,7 @@ public static function gather(
5868
'int',
5969
'string|object',
6070
'bool',
61-
], implode('|', is_array($parameter->schema->type) ? $parameter->schema->type : [$parameter->schema->type]));
71+
], implode('|', $types));
6272

6373
$parameters[] = new Parameter(
6474
(new Convert($parameter->name))->toCamel(),
@@ -82,7 +92,7 @@ public static function gather(
8292
);
8393
$requestBody[] = new OperationRequestBody(
8494
$contentType,
85-
Schema::gather($baseNamespace, $requestBodyClassname, $requestBodyDetails->schema, $schemaRegistry),
95+
Schema::gather($baseNamespace, $requestBodyClassname, $requestBodyDetails->schema, $schemaRegistry, $contractRegistry),
8696
);
8797
}
8898
}
@@ -115,6 +125,7 @@ public static function gather(
115125
$contentTypeMediaType->schema,
116126
true,
117127
$schemaRegistry,
128+
$contractRegistry,
118129
),
119130
);
120131
if ($isError) {
@@ -139,6 +150,7 @@ public static function gather(
139150
),
140151
$headerSpec->schema,
141152
$schemaRegistry,
153+
$contractRegistry,
142154
), ExampleData::determiteType($headerSpec->example));
143155
}
144156

src/Gatherer/Path.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ApiClients\Tools\OpenApiClientGenerator\ClassString;
88
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_;
99
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Voter;
10+
use ApiClients\Tools\OpenApiClientGenerator\Registry\Contract as ContractRegistry;
1011
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
1112
use ApiClients\Tools\OpenApiClientGenerator\Registry\ThrowableSchema;
1213
use ApiClients\Tools\OpenApiClientGenerator\Utils;
@@ -23,6 +24,7 @@ public static function gather(
2324
string $path,
2425
PathItem $pathItem,
2526
SchemaRegistry $schemaRegistry,
27+
ContractRegistry $contractRegistry,
2628
ThrowableSchema $throwableSchemaRegistry,
2729
Voter|null $voters,
2830
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Path {
@@ -45,6 +47,7 @@ public static function gather(
4547
$operation,
4648
$throwableSchemaRegistry,
4749
$schemaRegistry,
50+
$contractRegistry,
4851
);
4952

5053
if ($voters !== null && is_array($voters->listOperation)) {
@@ -75,6 +78,7 @@ public static function gather(
7578
$operation,
7679
$throwableSchemaRegistry,
7780
$schemaRegistry,
81+
$contractRegistry,
7882
);
7983
}
8084
}
@@ -105,6 +109,7 @@ public static function gather(
105109
$operation,
106110
$throwableSchemaRegistry,
107111
$schemaRegistry,
112+
$contractRegistry,
108113
);
109114
}
110115

src/Gatherer/Property.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace ApiClients\Tools\OpenApiClientGenerator\Gatherer;
66

77
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_;
8+
use ApiClients\Tools\OpenApiClientGenerator\Registry\Contract as ContractRegistry;
89
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
910
use ApiClients\Tools\OpenApiClientGenerator\Representation;
1011
use cebe\openapi\spec\Schema as baseSchema;
@@ -13,6 +14,7 @@
1314
use PhpParser\Node;
1415

1516
use function array_filter;
17+
use function array_key_exists;
1618
use function array_values;
1719
use function count;
1820
use function is_array;
@@ -30,6 +32,7 @@ public static function gather(
3032
bool $required,
3133
baseSchema $property,
3234
SchemaRegistry $schemaRegistry,
35+
ContractRegistry $contractRegistry,
3336
): Representation\Property {
3437
$enum = [];
3538
$exampleData = null;
@@ -80,14 +83,19 @@ static function ($matches) {
8083
$property,
8184
$required,
8285
$schemaRegistry,
86+
$contractRegistry,
8387
);
8488

8589
if ($property->type === 'array' && is_array($type->payload)) {
8690
$arrayItemsRaw = [];
8791
$arrayItemsNode = [];
8892

8993
foreach ($type->payload as $index => $arrayItem) {
90-
$arrayItemExampleData = ExampleData::gather($exampleData, $arrayItem, $propertyName . str_pad('', $index + 1, '_'));
94+
$arrayItemExampleData = ExampleData::gather(
95+
$exampleData,
96+
$arrayItem->type === 'union' ? $arrayItem->payload[(array_key_exists($index, $arrayItem->payload) ? $index : 0)] : $arrayItem,
97+
$propertyName . str_pad('', $index + 1, '_'),
98+
);
9199
$arrayItemsRaw[] = $arrayItemExampleData->raw;
92100
$arrayItemsNode[] = new Node\Expr\ArrayItem($arrayItemExampleData->node);
93101
}

src/Gatherer/Schema.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use ApiClients\Tools\OpenApiClientGenerator\ClassString;
88
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_;
9+
use ApiClients\Tools\OpenApiClientGenerator\Registry\Contract as ContractRegistry;
910
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
1011
use ApiClients\Tools\OpenApiClientGenerator\Utils;
1112
use cebe\openapi\spec\Schema as baseSchema;
@@ -22,6 +23,7 @@ public static function gather(
2223
string $className,
2324
baseSchema $schema,
2425
SchemaRegistry $schemaRegistry,
26+
ContractRegistry $contractRegistry,
2527
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema {
2628
$className = Utils::className($className);
2729
$isArray = $schema->type === 'array';
@@ -38,12 +40,13 @@ public static function gather(
3840
$className,
3941
(string) $propertyName,
4042
in_array(
41-
$propertyName,
43+
(string) $propertyName,
4244
$schema->required ?? [],
4345
false,
4446
),
4547
$property,
4648
$schemaRegistry,
49+
$contractRegistry,
4750
);
4851
$properties[] = $gatheredProperty;
4952

@@ -60,10 +63,16 @@ public static function gather(
6063
}
6164

6265
$example[$gatheredProperty->sourceName] = $gatheredProperty->example->raw;
66+
67+
foreach ($property->enum ?? [] as $value) {
68+
$example[$gatheredProperty->sourceName] = $value;
69+
break;
70+
}
6371
}
6472

6573
return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema(
6674
ClassString::factory($baseNamespace, 'Schema\\' . $className),
75+
ClassString::factory($baseNamespace, 'Contract\\' . $className),
6776
ClassString::factory($baseNamespace, 'Error\\' . $className),
6877
ClassString::factory($baseNamespace, 'ErrorSchemas\\' . $className),
6978
$schema->title ?? '',

0 commit comments

Comments
 (0)