Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better path sorting for openapi UIs #6583

Merged
merged 6 commits into from
Sep 7, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/OpenApi/Model/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,33 @@ final class Paths
public function addPath(string $path, PathItem $pathItem): void
{
$this->paths[$path] = $pathItem;
}

private function comparePathsByKey($keyA, $keyB): int
{
$a = $this->paths[$keyA];
$b = $this->paths[$keyB];

ksort($this->paths);
$tagsA = [
...($a->getGet()?->getTags() ?? []),
...($a->getPost()?->getTags() ?? []),
...($a->getPut()?->getTags() ?? []),
...($a->getDelete()?->getTags() ?? []),
];
sort($tagsA);

$tagsB = [
...($b->getGet()?->getTags() ?? []),
...($b->getPost()?->getTags() ?? []),
...($b->getPut()?->getTags() ?? []),
...($b->getDelete()?->getTags() ?? []),
];
sort($tagsB);

return match (true) {
current($tagsA) === current($tagsB) => $keyA <=> $keyB,
default => current($tagsA) <=> current($tagsB),
};
}

public function getPath(string $path): ?PathItem
Expand All @@ -31,6 +56,9 @@ public function getPath(string $path): ?PathItem

public function getPaths(): array
{
// sort paths by tags, then by path for each tag
uksort($this->paths, $this->comparePathsByKey(...));

mrossard marked this conversation as resolved.
Show resolved Hide resolved
return $this->paths;
}
}
Loading