Skip to content

Commit

Permalink
feat: 2.3.0 handle return type from design:returntype
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Nov 14, 2024
1 parent 63a7619 commit f961714
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@danet/swagger",
"version": "2.2.0",
"version": "2.3.0",
"exports": {
".":"./mod.ts",
"./decorators": "./decorators.ts"
Expand Down
4 changes: 2 additions & 2 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions example/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class HelloController {
@Controller('zod')
class ZodController {
@Post()
posZodSomething(@ZodBody(ZodTodo) todo: ZodTodo): number {
posZodSomething(@ZodBody(ZodTodo) todo: ZodTodo) {
return 1;
}

Expand All @@ -156,10 +156,9 @@ class MyController {
}

@ApiOAuth2(['my-permission:all'])
@ReturnedType(Todo)
@Post()
postSomething(@Body() todo: Todo): number {
return 1;
postSomething(@Body() todo: Todo): Todo {
return new Todo();
}

@ApiCookieAuth()
Expand All @@ -171,7 +170,7 @@ class MyController {

@BodyType(Todo)
@Put('somethingagain')
putSomething(): Todo {
putSomething() {
return new Todo();
}
}
Expand Down
15 changes: 14 additions & 1 deletion method-definer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ export class MethodDefiner {
this.Controller.prototype,
this.methodName,
);
if (!returnedValue) {
const returnedType = MetadataHelper.getMetadata<Constructor>(
'design:returntype',
this.Controller.prototype,
this.methodName,
);
if (returnedType) {
returnedValue = {
returnedType,
isArray: false,
};
}
}
if (returnedValue) {
if (isPrimitive(returnedValue.returnedType.name.toLowerCase())) {
if (returnedValue.isArray) {
Expand Down Expand Up @@ -298,7 +311,6 @@ export class MethodDefiner {
this.Controller.prototype,
this.methodName
);
console.log('returnedSchema', returnedSchema);
if (returnedSchema) {
const openApiSchema = this.generateZodSchema(returnedSchema.returnedSchema);
if (returnedSchema.isArray) {
Expand All @@ -313,6 +325,7 @@ export class MethodDefiner {
'$ref': `#/components/schemas/${openApiSchema.title}`,
}).setDescription('').get();
}
return;
}

return null;
Expand Down

0 comments on commit f961714

Please sign in to comment.