Skip to content

Commit 3ec51d9

Browse files
committed
feat: path/method description decorator
1 parent f961714 commit 3ec51d9

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

decorators.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,44 @@ return (
161161
};
162162
}
163163

164+
/**
165+
* A constant key used to store or retrieve description metadata.
166+
* This key is typically used in decorators to annotate
167+
* classes or methods with specific description for documentation.
168+
*/
169+
export const DESCRIPTION_KEY = 'description';
170+
171+
/**
172+
* A decorator function to add an openAPI description to a class or a method.
173+
*
174+
* @param description - The description.
175+
*
176+
* @example
177+
* ```typescript
178+
* class ExampleClass {
179+
* @Description('My very cool description')
180+
* exampleMethod() {
181+
* // method implementation
182+
* }
183+
* }
184+
* ```
185+
*/
186+
export function Description(description: string) : DecoratorFunction {
187+
return (
188+
target: Object,
189+
propertyKey?: string | symbol,
190+
descriptor?: PropertyDescriptor,
191+
) => {
192+
if (propertyKey) {
193+
MetadataHelper.setMetadata(DESCRIPTION_KEY, description, target, propertyKey);
194+
} else {
195+
MetadataHelper.setMetadata(DESCRIPTION_KEY, description, target);
196+
}
197+
};
198+
}
199+
200+
201+
164202
/**
165203
* A constant key used to store or retrieve api-security metadata.
166204
*/

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@danet/swagger",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"exports": {
55
".":"./mod.ts",
66
"./decorators": "./decorators.ts"

example/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ApiBasicAuth, ApiBearerAuth, ApiCookieAuth, ApiOAuth2,
33
ApiProperty, ApiSecurity,
44
BodyType,
5+
Description,
56
Optional,
67
QueryType,
78
ReturnedType,
@@ -181,6 +182,7 @@ class MyController {
181182
@Tag('second')
182183
@Controller('second-endpoint')
183184
class SecondController {
185+
@Description("Get Second Description")
184186
@ApiSecurity('basic')
185187
@Get()
186188
getSecond() {

method-definer.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Swagger } from './swagger.ts';
22
import { Constructor } from './mod.ts';
3-
import { API_PROPERTY, API_SECURITY, OPTIONAL_KEY, RETURNED_TYPE_KEY, TAGS_KEY } from './decorators.ts';
3+
import { API_PROPERTY, API_SECURITY, DESCRIPTION_KEY, OPTIONAL_KEY, RETURNED_TYPE_KEY, TAGS_KEY } from './decorators.ts';
44
import { RequestBodyBuilder, ResponseBuilder } from './builder.ts';
55
import DataType = Swagger.DataType;
66
import DataFormat = Swagger.DataFormat;
@@ -70,6 +70,7 @@ export class MethodDefiner {
7070
this.addResponse(actualPath);
7171
this.addRequestBody(actualPath);
7272
this.addSecurity(actualPath);
73+
this.addDescription(actualPath);
7374
schemas = {
7475
...schemas,
7576
...this.schemas,
@@ -117,6 +118,17 @@ export class MethodDefiner {
117118
}
118119
}
119120

121+
private addDescription(actualPath: Operation) {
122+
const methodDescription = MetadataHelper.getMetadata<string>(
123+
DESCRIPTION_KEY,
124+
this.Controller.prototype,
125+
this.methodName,
126+
);
127+
if (methodDescription) {
128+
actualPath.description = methodDescription;
129+
}
130+
}
131+
120132
private addUrlParams(actualPath: Operation) {
121133
if (this.containsUrlParams && !actualPath.parameters) {
122134
actualPath.parameters = [];

spec/generate-schemas.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const expectedSpec = {
8484
'/second-endpoint': {
8585
'get': {
8686
'operationId': 'getSecond',
87+
'description': "Get Second Description",
8788
'responses': { '200': { 'description': '' } },
8889
'tags': ['second'],
8990
'security': [{

0 commit comments

Comments
 (0)