Skip to content

Commit 9fb8279

Browse files
authored
Merge pull request #133 from php-api-clients/allow-duplicated-schemas
Allow duplicated schemas
2 parents 5649b2a + 1fa7ef1 commit 9fb8279

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

bin/openapi-client-generator.source

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use Symfony\Component\Yaml\Yaml;
1717
*/
1818
exit((function (string $configuration): int {
1919
$yaml = Yaml::parseFile($configuration);
20-
(new Generator($yaml['spec']))->generate($yaml['namespace'] . '\\', dirname($configuration) . DIRECTORY_SEPARATOR . $yaml['destination'], $yaml['voter'] ?? []);
20+
(new Generator($yaml['spec']))->generate($yaml['namespace'] . '\\', dirname($configuration) . DIRECTORY_SEPARATOR . $yaml['destination'], $yaml['schemas'] ?? [], $yaml['voter'] ?? []);
2121

2222
return 0;
2323
})($configuration));

openapi-client.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
spec: file:///home/wyrihaximus/Projects/PHPAPIClients/openapi-client-generator/api.github.com.yaml
33
namespace: ApiClients\Client\Github
44
destination: generated
5+
schemas:
6+
allowDuplication: true
57
voter:
68
listOperation:
79
- ApiClients\Tools\OpenApiClientGenerator\Voter\ListOperation\PageAndPerPageInQuery

src/Generator.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public function __construct(string $specUrl)
3030
$this->spec = Reader::readFromYamlFile($specUrl);
3131
}
3232

33-
public function generate(string $namespace, string $destinationPath, array $voters)
33+
public function generate(string $namespace, string $destinationPath, array $schemas, array $voters)
3434
{
3535
$existingFiles = iterator_to_array(Files::listExistingFiles($destinationPath . DIRECTORY_SEPARATOR));
3636
$namespace = Utils::cleanUpNamespace($namespace);
3737
$codePrinter = new Standard();
3838

39-
foreach ($this->all($namespace, $destinationPath . DIRECTORY_SEPARATOR, $voters) as $file) {
39+
foreach ($this->all($namespace, $destinationPath . DIRECTORY_SEPARATOR, $schemas, $voters) as $file) {
4040
$fileName = $destinationPath . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($file->fqcn, strlen($namespace))) . '.php';
4141
if ($file->contents instanceof Node\Stmt\Namespace_) {
4242
array_unshift($file->contents->stmts, ...[
@@ -100,14 +100,15 @@ public function generate(string $namespace, string $destinationPath, array $vote
100100
}
101101

102102
/**
103-
* @param string $namespace
104-
* @param string $destinationPath
105103
* @return iterable<File>
106104
*/
107-
private function all(string $namespace, string $rootPath, array $voters): iterable
105+
private function all(string $namespace, string $rootPath, array $schemas, array $voters): iterable
108106
{
109-
$schemas = [];
110107
$schemaRegistry = new SchemaRegistry();
108+
if (array_key_exists('allowDuplication', $schemas)) {
109+
$schemaRegistry->setAllowDuplicatedSchemas($schemas['allowDuplication']);
110+
}
111+
$schemas = [];
111112
$throwableSchemaRegistry = new ThrowableSchema();
112113
if (count($this->spec->components->schemas ?? []) > 0) {
113114
foreach ($this->spec->components->schemas as $name => $schema) {

src/Registry/Schema.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ final class Schema
2626
*/
2727
private array $unknownSchemasJson = [];
2828

29+
private bool $allowDuplicatedSchemas = false;
30+
31+
public function setAllowDuplicatedSchemas(bool $allow): void
32+
{
33+
$this->allowDuplicatedSchemas = $allow;
34+
}
35+
2936
public function addClassName(string $className, openAPISchema $schema): void
3037
{
3138
if ($schema->type === 'array') {
@@ -47,10 +54,10 @@ public function get(openAPISchema $schema, string $fallbackName): string
4754
}
4855

4956
$json = json_encode($schema->getSerializableData());
50-
if (array_key_exists($json, $this->json)) {
57+
if (!$this->allowDuplicatedSchemas && array_key_exists($json, $this->json)) {
5158
return $this->json[$json];
5259
}
53-
if (array_key_exists($json, $this->unknownSchemasJson)) {
60+
if (!$this->allowDuplicatedSchemas && array_key_exists($json, $this->unknownSchemasJson)) {
5461
return $this->unknownSchemasJson[$json];
5562
}
5663

0 commit comments

Comments
 (0)