Skip to content

Commit db70803

Browse files
authored
Merge pull request #126 from php-api-clients/better-generated-example-values-for-formatted-strings
Better generated examples values for formatted strings
2 parents 37077a6 + a41a1e0 commit db70803

File tree

5 files changed

+265
-5
lines changed

5 files changed

+265
-5
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"eventsauce/object-hydrator": "^1.2",
3636
"react/async": "^4.0",
3737
"wyrihaximus/react-awaitable-observable": "^1.0",
38-
"ckr/arraymerger": "^3.0"
38+
"ckr/arraymerger": "^3.0",
39+
"ramsey/uuid": "^4.7"
3940
},
4041
"autoload": {
4142
"psr-4": {

composer.lock

Lines changed: 238 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Gatherer/Property.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ApiClients\Tools\OpenApiClientGenerator\Representation\PropertyType;
77
use cebe\openapi\spec\Schema as baseSchema;
88
use Ckr\Util\ArrayMerger;
9+
use Ramsey\Uuid\Uuid;
910

1011
final class Property
1112
{
@@ -45,11 +46,11 @@ public static function gather(
4546
);
4647
if ($type->payload instanceof \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema) {
4748
if (count($type->payload->properties) === 0) {
48-
$type = new PropertyType('scalar', 'mixed', false);
49+
$type = new PropertyType('scalar', null, 'mixed', false);
4950
}
5051
} else if ($type->payload instanceof PropertyType && $type->payload->payload instanceof \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema) {
5152
if (count($type->payload->payload->properties) === 0) {
52-
$type = new PropertyType('scalar', 'mixed', false);
53+
$type = new PropertyType('scalar', null, 'mixed', false);
5354
}
5455
}
5556
$exampleData = self::generateExampleData($exampleData, $type, $propertyName);
@@ -77,7 +78,23 @@ private static function generateExampleData(mixed $exampleData, PropertyType $ty
7778
} elseif ($type->payload === 'bool') {
7879
return false;
7980
} elseif ($type->payload === 'string') {
80-
return 'generated_' . $propertyName;
81+
if ($type->format === 'uri') {
82+
return 'https://example.com/';
83+
}
84+
if ($type->format === 'date-time') {
85+
return date(\DateTimeInterface::RFC3339, 0);
86+
}
87+
if ($type->format === 'uuid') {
88+
return Uuid::getFactory()->uuid6()->toString();
89+
}
90+
if ($type->format === 'ipv4') {
91+
return '127.0.0.1';
92+
}
93+
if ($type->format === 'ipv6') {
94+
return '::1';
95+
}
96+
97+
return 'generated_' . $propertyName . '_' . ($type->format ?? 'null');
8198
}
8299
}
83100

src/Gatherer/Type.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static function gather(
6767
if ($type === 'array') {
6868
return new PropertyType(
6969
'array',
70+
null,
7071
Type::gather($className, $propertyName, $property->items, $required, $schemaRegistry),
7172
$nullable
7273
);
@@ -93,6 +94,7 @@ public static function gather(
9394
if ($type === '') {
9495
return new PropertyType(
9596
'scalar',
97+
null,
9698
'mixed',
9799
false,
98100
);
@@ -101,6 +103,7 @@ public static function gather(
101103
if ($type === 'object') {
102104
return new PropertyType(
103105
'object',
106+
null,
104107
Schema::gather(
105108
$schemaRegistry->get($property, $className . '\\' . Utils::className($propertyName)),
106109
$property,
@@ -112,6 +115,7 @@ public static function gather(
112115

113116
return new PropertyType(
114117
'scalar',
118+
$property->format ?? null,
115119
$type,
116120
$nullable,
117121
);

src/Representation/PropertyType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ final class PropertyType
66
{
77
public function __construct(
88
public readonly string $type,
9+
public readonly ?string $format,
910
public readonly string|Schema|PropertyType $payload,
1011
public readonly bool $nullable,
1112
){

0 commit comments

Comments
 (0)