diff --git a/src/Gatherer/Property.php b/src/Gatherer/Property.php index 3248508..f28fbd1 100644 --- a/src/Gatherer/Property.php +++ b/src/Gatherer/Property.php @@ -17,6 +17,7 @@ public static function gather( baseSchema $property, SchemaRegistry $schemaRegistry, ): \ApiClients\Tools\OpenApiClientGenerator\Representation\Property { + $exampleData = null; $propertyName = str_replace([ '@', '+', @@ -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 @@ -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); } } diff --git a/src/Gatherer/Schema.php b/src/Gatherer/Schema.php index 1045e90..bf50c41 100644 --- a/src/Gatherer/Schema.php +++ b/src/Gatherer/Schema.php @@ -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, diff --git a/src/Generator/Schema.php b/src/Generator/Schema.php index ecc92d4..8eeada8 100644 --- a/src/Generator/Schema.php +++ b/src/Generator/Schema.php @@ -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; @@ -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(); diff --git a/src/Representation/Property.php b/src/Representation/Property.php index 2323ea7..686a6c0 100644 --- a/src/Representation/Property.php +++ b/src/Representation/Property.php @@ -7,6 +7,7 @@ final class Property public function __construct( public readonly string $name, public readonly string $description, + public readonly mixed $exampleData, /** @var array */ public readonly array $type, public readonly bool $nullable, diff --git a/src/Representation/Schema.php b/src/Representation/Schema.php index a0f1f12..cea9f8e 100644 --- a/src/Representation/Schema.php +++ b/src/Representation/Schema.php @@ -10,6 +10,8 @@ public function __construct( public readonly string $className, public readonly string $title, public readonly string $description, + /** @var array */ + public readonly array $example, /** @var array */ public readonly array $properties, public readonly baseSchema $schema,