From 12ca4f5f90e43d2e6459683e2a6aa75c23a1ae1e Mon Sep 17 00:00:00 2001 From: WildEgo Date: Sun, 3 Mar 2024 18:23:16 +0000 Subject: [PATCH] add: Tags as a primary key https://swagger.io/docs/specification/grouping-operations-with-tags/ --- src/Support/Generator/OpenApi.php | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Support/Generator/OpenApi.php b/src/Support/Generator/OpenApi.php index 5a7bf8e2..2228cc89 100644 --- a/src/Support/Generator/OpenApi.php +++ b/src/Support/Generator/OpenApi.php @@ -16,6 +16,9 @@ class OpenApi /** @var Path[] */ public array $paths = []; + /** @var string[] */ + public array $tags = []; + private ?Security $defaultSecurity = null; public function __construct(string $version) @@ -72,6 +75,23 @@ public function addPath(Path $path) return $this; } + /** + * @param string[] $tags + */ + public function tags(array $tags) + { + $this->tags = $tags; + + return $this; + } + + public function addTag(string $tag) + { + $this->tags[] = $tag; + + return $this; + } + public function addServer(Server $server) { $this->servers[] = $server; @@ -104,6 +124,12 @@ public function toArray() $result['security'] = [$this->defaultSecurity->toArray()]; } + $tags = []; + + if (count($this->tags)) { + $tags = $this->tags; + } + if (count($this->paths)) { $paths = []; @@ -115,6 +141,18 @@ public function toArray() } $result['paths'] = $paths; + + $tags = array_merge( + $tags, + collect($paths)->pluck('*.tags')->flatten()->unique()->toArray(), + ); + } + + if (count($tags = array_filter($tags))) { + $keys = array_keys($tags); + $result['tags'] = array_map(fn ($tag) => [ + 'name' => $tag, + ], $tags, $keys); } if (count($serializedComponents = $this->components->toArray())) {