Skip to content

Add example data to schemas for testing #91

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 27, 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
11 changes: 10 additions & 1 deletion src/Gatherer/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static function gather(
baseSchema $property,
SchemaRegistry $schemaRegistry,
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Property {
$exampleData = null;
$propertyName = str_replace([
'@',
'+',
Expand Down Expand Up @@ -82,7 +83,15 @@ public static function gather(
$schemaRegistry,
)
);
$exampleData = $type->payload->example;
} else {
if ($type === 'int') {
$exampleData = 13;
} elseif ($type === 'bool') {
$exampleData = false;
} else {
$exampleData = 'generated_' . $propertyName;
}
$type = new PropertyType(
'scalar',
$type
Expand All @@ -93,6 +102,6 @@ public static function gather(
$type = [$type];
}

return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Property($propertyName, $property->description ?? '', $type, $nullable);
return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Property($propertyName, $property->description ?? '', $exampleData, $type, $nullable);
}
}
6 changes: 5 additions & 1 deletion src/Gatherer/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ public static function gather(
}

$properties = [];
$example = [];
foreach ($schema->properties as $propertyName => $property) {
$properties[] = Property::gather(
$gatheredProperty = Property::gather(
$className,
$propertyName,
is_array($schema->required) && !in_array($propertyName, $schema->required, false),
$property,
$schemaRegistry
);
$properties[] = $gatheredProperty;
$example[$gatheredProperty->name] = $gatheredProperty->exampleData;
}
return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema(
$className,
$schema->title ?? '',
$schema->description ?? '',
$example,
$properties,
$schema,
$isArray,
Expand Down
11 changes: 11 additions & 0 deletions src/Generator/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Jawira\CaseConverter\Convert;
use PhpParser\Builder\Param;
use PhpParser\BuilderFactory;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -66,6 +67,16 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
],
Class_::MODIFIER_PUBLIC
)
)->addStmt(
new Node\Stmt\ClassConst(
[
new Node\Const_(
'SCHEMA_EXAMPLE_DATA',
(new BuilderFactory)->val($schema->example),
),
],
Class_::MODIFIER_PUBLIC
)
);

$constructor = (new BuilderFactory())->method('__construct')->makePublic();
Expand Down
1 change: 1 addition & 0 deletions src/Representation/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ final class Property
public function __construct(
public readonly string $name,
public readonly string $description,
public readonly mixed $exampleData,
/** @var array<PropertyType> */
public readonly array $type,
public readonly bool $nullable,
Expand Down
2 changes: 2 additions & 0 deletions src/Representation/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public function __construct(
public readonly string $className,
public readonly string $title,
public readonly string $description,
/** @var array<mixed> */
public readonly array $example,
/** @var array<Property> */
public readonly array $properties,
public readonly baseSchema $schema,
Expand Down