Skip to content

Commit 8f98892

Browse files
committed
More predictable names for inline schemas
1 parent 727938f commit 8f98892

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

src/Generator/Schema.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ private static function fillUpSchema(string $name, string $namespace, string $cl
118118

119119
if ($property->type === 'array' && $property->items instanceof OpenAPiSchema) {
120120
// if (array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) {
121-
$methodDocBlock[] = '@return array<\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->items) . '>';
122-
$propertyDocBlock[] = '@var array<\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->items) . '>';
123-
$propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->items) . '::class)';
121+
$methodDocBlock[] = '@return array<\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->items, $className . '\\' . (new Convert($propertyName))->toPascal()) . '>';
122+
$propertyDocBlock[] = '@var array<\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->items, $className . '\\' . (new Convert($propertyName))->toPascal()) . '>';
123+
$propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\HydrateArray(\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->items, $className . '\\' . (new Convert($propertyName))->toPascal()) . '::class)';
124124
// } elseif ($property->items->type === 'object') {
125125
// yield from self::generate($name . '::' . $propertyName, $namespace . '\\' . $className, (new Convert($propertyName))->toPascal(), $property->items, $schemaClassNameMap, $rootNamespace);
126126
// $methodDocBlock[] = '@return array<\\' . $namespace . '\\' . $className . '\\' . (new Convert($propertyName))->toPascal() . '>';
@@ -155,21 +155,21 @@ private static function fillUpSchema(string $name, string $namespace, string $cl
155155
}
156156

157157
if (is_array($property->anyOf) && $property->anyOf[0] instanceof OpenAPiSchema/* && array_key_exists(spl_object_hash($property->anyOf[0]), $schemaClassNameMap)*/) {
158-
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->anyOf[0]);
158+
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->anyOf[0], $className . '\\' . (new Convert($propertyName))->toPascal());
159159
$propertyStmt->setType($nullable . $fqcnn);
160160
$method->setReturnType($nullable . $fqcnn);
161161
$propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)';
162162
$setDefaylt = false;
163163
} else if (is_array($property->allOf) && $property->allOf[0] instanceof OpenAPiSchema/* && array_key_exists(spl_object_hash($property->allOf[0]), $schemaClassNameMap)*/) {
164-
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->allOf[0]);
164+
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaRegistry->get($property->allOf[0], $className . '\\' . (new Convert($propertyName))->toPascal());
165165
$propertyStmt->setType($nullable . $fqcnn);
166166
$method->setReturnType($nullable . $fqcnn);
167167
$propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)';
168168
$setDefaylt = false;
169169
}
170170

171171
if ($property->type === 'object' && $property instanceof OpenAPiSchema/* && array_key_exists(spl_object_hash($property), $schemaClassNameMap)*/) {
172-
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaRegistry->get($property);
172+
$fqcnn = '\\' . $rootNamespace . '\\' . $schemaRegistry->get($property, $className . '\\' . (new Convert($propertyName))->toPascal());
173173
$propertyStmt->setType($nullable . $fqcnn);
174174
$method->setReturnType($nullable . $fqcnn);
175175
$propertyDocBlock[] = '@\WyriHaximus\Hydrator\Attribute\Hydrate(' . $fqcnn . '::class)';

src/SchemaRegistry.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function addClassName(string $className, Schema $schema): void
2626
$this->json[json_encode($schema->getSerializableData())] = $className;
2727
}
2828

29-
public function get(\cebe\openapi\spec\Schema $schema): string
29+
public function get(\cebe\openapi\spec\Schema $schema, string $fallbackName = ''): string
3030
{
3131
$hash = spl_object_hash($schema);
3232
if (array_key_exists($hash, $this->splHash)) {
@@ -38,14 +38,15 @@ public function get(\cebe\openapi\spec\Schema $schema): string
3838
return $this->json[$json];
3939
}
4040

41-
$name = 'c_' . md5($json);
41+
$name = $fallbackName === '' ? 'c_' . md5($json) : $fallbackName;
42+
$className = $fallbackName === '' ? Generator::className('Unknown\C_' . md5($json)) : $fallbackName;
4243
$this->unknownSchemas[$hash] = [
4344
'name' => $name,
44-
'className' => Generator::className('Unknown\C_' . md5($json)),
45+
'className' => $className,
4546
'schema' => $schema,
4647
];
4748

48-
return $this->unknownSchemas[$hash]['className'];
49+
return $className;
4950
}
5051

5152
public function unknownSchemas(): iterable

0 commit comments

Comments
 (0)