Skip to content

Commit

Permalink
Avoid duplicate schemas, fix name generation
Browse files Browse the repository at this point in the history
Duplicate schema names would generate User_2 -> User_2_3 -> User_2_3_4 etc, this fixes that and also detects duplicate schemas and uses the existing one if found
  • Loading branch information
wslaghekke authored Sep 12, 2024
1 parent a915082 commit 31d76ca
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Service/DocumentationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,32 @@ private function addSchema(string $name, EntityMapping $mapping, array &$compone
}
}

$newSchema = new OASv3\Schema(['properties' => $properties]);

// try to find a name that has not been used yet
if (isset($components[$name])) {
$appendix = 2;
/** @var OASv3\Schema $existingSchema */
$existingSchema = $components[$name];

if ($existingSchema->toJson() === $newSchema->toJson()) {
// schema already exists, return existing name
return $name;
}

$appendix = 2;
while (true) {
$name = $name.'_'.$appendix;
$newName = $name.'_'.$appendix;

if (!isset($components[$name])) {
if (!isset($components[$newName])) {
$name = $newName;
break;
}

$appendix++;
}
}

$components[$name] = new OASv3\Schema(['properties' => $properties]);
$components[$name] = $newSchema;

return $name;
}
Expand Down

0 comments on commit 31d76ca

Please sign in to comment.