Skip to content

Commit a6c2836

Browse files
authored
Routes can be marked as deprecated (#994)
1 parent 7d1866b commit a6c2836

File tree

14 files changed

+118
-2
lines changed

14 files changed

+118
-2
lines changed

camel/Extraction/Metadata.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ class Metadata extends BaseDTO
1313
public ?string $title;
1414
public ?string $description;
1515
public bool $authenticated = false;
16+
public bool $deprecated = false;
1617
}

camel/Output/OutputEndpointData.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ public function isAuthed(): bool
313313
return $this->metadata->authenticated;
314314
}
315315

316+
public function isDeprecated(): bool
317+
{
318+
return $this->metadata->deprecated;
319+
}
320+
316321
public function hasJsonBody(): bool
317322
{
318323
if ($this->hasFiles() || empty($this->nestedBodyParameters))

resources/css/theme-default.style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,10 @@ html {
10371037
background-color: green;
10381038
}
10391039

1040+
.badge.badge-darkgoldenrod {
1041+
background-color: darkgoldenrod;
1042+
}
1043+
10401044
.badge.badge-darkgreen {
10411045
background-color: darkgreen;
10421046
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@if($deprecated)@component('scribe::components.badges.base', ['colour' => "darkgoldenrod", 'text' => 'deprecated'])
2+
@endcomponent
3+
@endif

resources/views/themes/default/endpoint.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<p>
99
@component('scribe::components.badges.auth', ['authenticated' => $endpoint->isAuthed()])
1010
@endcomponent
11+
@component('scribe::components.badges.deprecated', ['deprecated' => $endpoint->isDeprecated()])
12+
@endcomponent
1113
</p>
1214

1315
{!! Parsedown::instance()->text($endpoint->metadata->description ?: '') !!}

resources/views/themes/elements/endpoint.blade.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class="sl-overflow-x-hidden sl-truncate sl-text-muted">{{ rtrim($baseUrl, '/') }
3737
>requires authentication
3838
</div>
3939
@endif
40+
@if($endpoint->metadata->deprecated)
41+
<div class="sl-font-prose sl-font-semibold sl-px-1.5 sl-py-0.5 sl-text-on-primary sl-rounded-lg"
42+
style="background-color: darkgoldenrod"
43+
>deprecated
44+
</div>
45+
@endif
4046
</div>
4147
</div>
4248

src/Attributes/Deprecated.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Knuckles\Scribe\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
8+
class Deprecated
9+
{
10+
public function __construct(
11+
public ?bool $deprecated = true,
12+
)
13+
{
14+
}
15+
16+
public function toArray()
17+
{
18+
return ["deprecated" => $this->deprecated];
19+
}
20+
}

src/Extracting/Strategies/Metadata/GetFromDocBlocks.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function getMetadataFromDocBlock(DocBlock $methodDocBlock, DocBlock $clas
2929
'subgroupDescription' => $this->getEndpointSubGroupDescription($methodDocBlock, $classDocBlock),
3030
'title' => $title ?: $methodDocBlock->getShortDescription(),
3131
'description' => $methodDocBlock->getLongDescription()->getContents(),
32+
'deprecated' => $this->getDeprecatedStatusFromDocBlock($methodDocBlock, $classDocBlock),
3233
];
3334
if (!is_null($authStatus = $this->getAuthStatusFromDocBlock($methodDocBlock, $classDocBlock))) {
3435
$metadata['authenticated'] = $authStatus;
@@ -53,6 +54,17 @@ protected function getAuthStatusFromDocBlock(DocBlock $methodDocBlock, ?DocBlock
5354
: null;
5455
}
5556

57+
protected function getDeprecatedStatusFromDocBlock(DocBlock $methodDocBlock, ?DocBlock $classDocBlock = null): bool
58+
{
59+
foreach ($methodDocBlock->getTags() as $tag) {
60+
if (strtolower($tag->getName()) === 'deprecated') {
61+
return true;
62+
}
63+
}
64+
65+
return $classDocBlock && $this->getDeprecatedStatusFromDocBlock($classDocBlock);
66+
}
67+
5668
/**
5769
* @return array The endpoint's group name, the group description, and the endpoint title
5870
*/

src/Extracting/Strategies/Metadata/GetFromMetadataAttributes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Knuckles\Camel\Extraction\ExtractedEndpointData;
66
use Knuckles\Scribe\Attributes\Authenticated;
7+
use Knuckles\Scribe\Attributes\Deprecated;
78
use Knuckles\Scribe\Attributes\Endpoint;
89
use Knuckles\Scribe\Attributes\Group;
910
use Knuckles\Scribe\Attributes\Subgroup;
@@ -24,6 +25,7 @@ class GetFromMetadataAttributes extends PhpAttributeStrategy
2425
Endpoint::class,
2526
Authenticated::class,
2627
Unauthenticated::class,
28+
Deprecated::class,
2729
];
2830

2931
protected function extractFromAttributes(

src/Writing/OpenApiSpecGenerators/BaseGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public function pathItem(array $pathItem, array $groupedEndpoints, OutputEndpoin
5252
})['name']],
5353
];
5454

55+
if ($endpoint->metadata->deprecated) {
56+
$spec['deprecated'] = true;
57+
}
58+
5559
if (count($endpoint->bodyParameters)) {
5660
$spec['requestBody'] = $this->generateEndpointRequestBodySpec($endpoint);
5761
}
@@ -549,7 +553,7 @@ public function generateSchemaForResponseValue(mixed $value, OutputEndpointData
549553
$schema['items']['properties'] = collect($sample)->mapWithKeys(function ($v, $k) use ($endpoint, $path) {
550554
return [$k => $this->generateSchemaForResponseValue($v, $endpoint, "$path.$k")];
551555
})->toArray();
552-
556+
553557
$required = $this->filterRequiredResponseFields($endpoint, array_keys($schema['items']['properties']),
554558
$path);
555559
if ($required) {

0 commit comments

Comments
 (0)