Skip to content

Commit b5cb6c3

Browse files
authored
Merge pull request #91 from php-api-clients/add-example-data-to-schemas-for-testing
Add example data to schemas for testing
2 parents 0663d8f + b073b3d commit b5cb6c3

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

src/Gatherer/Property.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static function gather(
1717
baseSchema $property,
1818
SchemaRegistry $schemaRegistry,
1919
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Property {
20+
$exampleData = null;
2021
$propertyName = str_replace([
2122
'@',
2223
'+',
@@ -82,7 +83,15 @@ public static function gather(
8283
$schemaRegistry,
8384
)
8485
);
86+
$exampleData = $type->payload->example;
8587
} else {
88+
if ($type === 'int') {
89+
$exampleData = 13;
90+
} elseif ($type === 'bool') {
91+
$exampleData = false;
92+
} else {
93+
$exampleData = 'generated_' . $propertyName;
94+
}
8695
$type = new PropertyType(
8796
'scalar',
8897
$type
@@ -93,6 +102,6 @@ public static function gather(
93102
$type = [$type];
94103
}
95104

96-
return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Property($propertyName, $property->description ?? '', $type, $nullable);
105+
return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Property($propertyName, $property->description ?? '', $exampleData, $type, $nullable);
97106
}
98107
}

src/Gatherer/Schema.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@ public static function gather(
2020
}
2121

2222
$properties = [];
23+
$example = [];
2324
foreach ($schema->properties as $propertyName => $property) {
24-
$properties[] = Property::gather(
25+
$gatheredProperty = Property::gather(
2526
$className,
2627
$propertyName,
2728
is_array($schema->required) && !in_array($propertyName, $schema->required, false),
2829
$property,
2930
$schemaRegistry
3031
);
32+
$properties[] = $gatheredProperty;
33+
$example[$gatheredProperty->name] = $gatheredProperty->exampleData;
3134
}
3235
return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema(
3336
$className,
3437
$schema->title ?? '',
3538
$schema->description ?? '',
39+
$example,
3640
$properties,
3741
$schema,
3842
$isArray,

src/Generator/Schema.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Jawira\CaseConverter\Convert;
1010
use PhpParser\Builder\Param;
1111
use PhpParser\BuilderFactory;
12+
use PhpParser\BuilderHelpers;
1213
use PhpParser\Node;
1314
use PhpParser\Node\Stmt\Class_;
1415
use Psr\Http\Message\RequestInterface;
@@ -66,6 +67,16 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
6667
],
6768
Class_::MODIFIER_PUBLIC
6869
)
70+
)->addStmt(
71+
new Node\Stmt\ClassConst(
72+
[
73+
new Node\Const_(
74+
'SCHEMA_EXAMPLE_DATA',
75+
(new BuilderFactory)->val($schema->example),
76+
),
77+
],
78+
Class_::MODIFIER_PUBLIC
79+
)
6980
);
7081

7182
$constructor = (new BuilderFactory())->method('__construct')->makePublic();

src/Representation/Property.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ final class Property
77
public function __construct(
88
public readonly string $name,
99
public readonly string $description,
10+
public readonly mixed $exampleData,
1011
/** @var array<PropertyType> */
1112
public readonly array $type,
1213
public readonly bool $nullable,

src/Representation/Schema.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public function __construct(
1010
public readonly string $className,
1111
public readonly string $title,
1212
public readonly string $description,
13+
/** @var array<mixed> */
14+
public readonly array $example,
1315
/** @var array<Property> */
1416
public readonly array $properties,
1517
public readonly baseSchema $schema,

0 commit comments

Comments
 (0)