-
Notifications
You must be signed in to change notification settings - Fork 937
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically adds missing tags to the `OpenApi` spec
- Loading branch information
1 parent
ce3b9be
commit 7ea7c73
Showing
13 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Processors; | ||
|
||
use OpenApi\Analysis; | ||
use OpenApi\Annotations as OA; | ||
use OpenApi\Generator; | ||
|
||
/** | ||
* Ensures that all tags used on operations also exist in the global <code>tags</code> list. | ||
*/ | ||
class AugmentTags implements ProcessorInterface | ||
{ | ||
public function __invoke(Analysis $analysis) | ||
{ | ||
/** @var OA\Operation[] $operations */ | ||
$operations = $analysis->getAnnotationsOfType(OA\Operation::class); | ||
|
||
$usedTags = []; | ||
foreach ($operations as $operation) { | ||
if (!Generator::isDefault($operation->tags)) { | ||
$usedTags = array_merge($usedTags, $operation->tags); | ||
} | ||
} | ||
|
||
if ($usedTags) { | ||
$declaredTags = []; | ||
if (!Generator::isDefault($analysis->openapi->tags)) { | ||
foreach ($analysis->openapi->tags as $tag) { | ||
$declaredTags[] = $tag->name; | ||
} | ||
} | ||
foreach ($usedTags as $tag) { | ||
if (!in_array($tag, $declaredTags)) { | ||
$analysis->openapi->merge([new OA\Tag(['name' => $tag, 'description' => $tag])]); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,7 @@ paths: | |
responses: | ||
'200': | ||
description: OK | ||
tags: | ||
- | ||
name: endpoints | ||
description: endpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,7 @@ paths: | |
responses: | ||
'200': | ||
description: OK | ||
tags: | ||
- | ||
name: endpoints | ||
description: endpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Tests\Fixtures\Scratch; | ||
|
||
use OpenApi\Attributes as OAT; | ||
|
||
#[OAT\Info( | ||
title: 'Tags', | ||
description: 'Tag Scratch', | ||
version: '1.0', | ||
contact: new OAT\Contact(name: 'contact', email: '[email protected]') | ||
) | ||
] | ||
#[OAT\Get( | ||
path: '/endpoint', | ||
description: 'Sandbox endpoint', | ||
tags: ['sandbox'], | ||
responses: [ | ||
new OAT\Response( | ||
response: 200, | ||
description: 'All good' | ||
), | ||
] | ||
)] | ||
class TagsEndpoint | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: Tags | ||
description: 'Tag Scratch' | ||
contact: | ||
name: contact | ||
email: [email protected] | ||
version: '1.0' | ||
paths: | ||
/endpoint: | ||
get: | ||
tags: | ||
- sandbox | ||
description: 'Sandbox endpoint' | ||
operationId: f1c7f5a59b9708596c2bc955017002e6 | ||
responses: | ||
'200': | ||
description: 'All good' | ||
tags: | ||
- | ||
name: sandbox | ||
description: sandbox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: Tags | ||
description: 'Tag Scratch' | ||
contact: | ||
name: contact | ||
email: [email protected] | ||
version: '1.0' | ||
paths: | ||
/endpoint: | ||
get: | ||
tags: | ||
- sandbox | ||
description: 'Sandbox endpoint' | ||
operationId: f1c7f5a59b9708596c2bc955017002e6 | ||
responses: | ||
'200': | ||
description: 'All good' | ||
tags: | ||
- | ||
name: sandbox | ||
description: sandbox |