Skip to content

Commit cb4f784

Browse files
committed
Support the use of aliases
1 parent 1537f7d commit cb4f784

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

openapi-client.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace: ApiClients\Client\Github
44
destination: generated
55
schemas:
66
allowDuplication: true
7+
useAliasesForDuplication: true
78
voter:
89
listOperation:
910
- ApiClients\Tools\OpenApiClientGenerator\Voter\ListOperation\PageAndPerPageInQuery

src/Configuration/Schemas.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
public function __construct(
1010
#[MapFrom('allowDuplication')]
1111
public bool $allowDuplication,
12+
#[MapFrom('useAliasesForDuplication')]
13+
public bool $useAliasesForDuplication,
1214
) {
1315
}
1416
}

src/Generator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public function generate(string $namespace, string $destinationPath, Configurati
104104
*/
105105
private function all(string $namespace, string $rootPath, Configuration $configuration): iterable
106106
{
107-
$schemaRegistry = new SchemaRegistry();
108-
if ($configuration->schemas !== null && $configuration->schemas->allowDuplication !== null) {
109-
$schemaRegistry->setAllowDuplicatedSchemas($configuration->schemas->allowDuplication);
110-
}
107+
$schemaRegistry = new SchemaRegistry(
108+
$configuration->schemas !== null && $configuration->schemas->allowDuplication !== null ? $configuration->schemas->allowDuplication : false,
109+
$configuration->schemas !== null && $configuration->schemas->useAliasesForDuplication !== null ? $configuration->schemas->useAliasesForDuplication : false,
110+
);
111111
$schemas = [];
112112
$throwableSchemaRegistry = new ThrowableSchema();
113113
if (count($this->spec->components->schemas ?? []) > 0) {
@@ -171,6 +171,7 @@ private function all(string $namespace, string $rootPath, Configuration $configu
171171
yield from Schema::generate(
172172
$namespace,
173173
$schema,
174+
[...$schemaRegistry->aliasesForClassName($schema->className)],
174175
);
175176
if ($throwableSchemaRegistry->has($schema->className)) {
176177
yield from Error::generate(

src/Generator/Schema.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ final class Schema
2626
* @param OpenAPiSchema $schema
2727
* @return iterable<Node>
2828
*/
29-
public static function generate(string $namespace, \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema $schema): iterable
29+
public static function generate(string $namespace, \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema $schema, array $aliases): iterable
3030
{
31+
$className = $schema->className;
32+
if (count($aliases) > 0) {
33+
$className = 'AliasAbstract\\Abstract' . md5(json_encode($schema->schema->getSerializableData()));
34+
$aliases[] = $schema->className;
35+
}
3136
$factory = new BuilderFactory();
32-
$stmt = $factory->namespace(trim(Utils::dirname($namespace . '\\Schema\\' . $schema->className), '\\'));
37+
$stmt = $factory->namespace(trim(Utils::dirname($namespace . '\\Schema\\' . $className), '\\'));
3338

3439
$schemaJson = new Node\Stmt\ClassConst(
3540
[
@@ -43,7 +48,14 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
4348
Class_::MODIFIER_PUBLIC
4449
);
4550

46-
$class = $factory->class(trim(Utils::basename($schema->className), '\\'))->makeFinal()->makeReadonly()->addStmt(
51+
$class = $factory->class(trim(Utils::basename($className), '\\'))->makeReadonly();
52+
53+
if (count($aliases) === 0) {
54+
$class = $class->makeFinal();
55+
} else {
56+
$class = $class->makeAbstract();
57+
}
58+
$class->addStmt(
4759
$schemaJson
4860
)->addStmt(
4961
new Node\Stmt\ClassConst(
@@ -146,6 +158,14 @@ public static function generate(string $namespace, \ApiClients\Tools\OpenApiClie
146158
$class->addStmt($constructor);
147159

148160

149-
yield new File($namespace . 'Schema\\' . $schema->className, $stmt->addStmt($class)->getNode());
161+
yield new File($namespace . 'Schema\\' . $className, $stmt->addStmt($class)->getNode());
162+
163+
foreach ($aliases as $alias) {
164+
$aliasTms = $factory->namespace(trim(Utils::dirname($namespace . '\\Schema\\' . $alias), '\\'));
165+
166+
$aliasClass = $factory->class(trim(Utils::basename($alias), '\\'))->makeFinal()->makeReadonly()->extend('Schema\\' . $className);
167+
168+
yield new File($namespace . 'Schema\\' . $alias, $aliasTms->addStmt($aliasClass)->getNode());
169+
}
150170
}
151171
}

src/Registry/Schema.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ final class Schema
2525
* @var array<string, class-string>
2626
*/
2727
private array $unknownSchemasJson = [];
28+
/**
29+
* @var array<class-string, array<class-string>>
30+
*/
31+
private array $aliasses = [];
2832

29-
private bool $allowDuplicatedSchemas = false;
30-
31-
public function setAllowDuplicatedSchemas(bool $allow): void
33+
public function __construct(
34+
private readonly bool $allowDuplicatedSchemas,
35+
private readonly bool $useAliasesForDuplication,
36+
)
3237
{
33-
$this->allowDuplicatedSchemas = $allow;
3438
}
3539

3640
public function addClassName(string $className, openAPISchema $schema): void
@@ -62,6 +66,19 @@ public function get(openAPISchema $schema, string $fallbackName): string
6266
}
6367

6468
$className = Utils::fixKeyword($fallbackName);
69+
70+
if ($this->allowDuplicatedSchemas && $this->useAliasesForDuplication && array_key_exists($json, $this->json)) {
71+
$this->aliasses[$this->json[$json]][] = $className;
72+
73+
return $className;
74+
}
75+
76+
if ($this->allowDuplicatedSchemas && $this->useAliasesForDuplication && array_key_exists($json, $this->unknownSchemasJson)) {
77+
$this->aliasses[$this->unknownSchemasJson[$json]][] = $className;
78+
79+
return $className;
80+
}
81+
6582
$suffix = 'a';
6683
while (array_key_exists($className, $this->unknownSchemas)) {
6784
$className = Utils::fixKeyword($fallbackName . strtoupper($suffix++));
@@ -88,4 +105,11 @@ public function unknownSchemas(): iterable
88105
$this->unknownSchemas = [];
89106
yield from $unknownSchemas;
90107
}
108+
109+
public function aliasesForClassName(string $classname): iterable
110+
{
111+
if (array_key_exists($classname, $this->aliasses)) {
112+
yield from array_unique($this->aliasses[$classname]);
113+
}
114+
}
91115
}

0 commit comments

Comments
 (0)