Skip to content

Commit

Permalink
feat: handle ReturnedSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Nov 7, 2024
1 parent 4add3ec commit 4c64c85
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 23 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
"experimentalDecorators": true
},
"tasks": {
"test": "deno test --unstable spec --allow-env --allow-read --allow-net",
Expand Down
8 changes: 7 additions & 1 deletion example/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@danet/core';
import { Module } from '@danet/core';
import { DanetApplication } from '@danet/core';
import { Query as ZodQuery, Body as ZodBody } from '@danet/zod';
import { Query as ZodQuery, Body as ZodBody, ReturnedSchema } from '@danet/zod';
import { z } from 'zod';
import { extendZodWithOpenApi } from 'zod-openapi';

Expand Down Expand Up @@ -136,6 +136,12 @@ class ZodController {
posZodSomething(@ZodBody(ZodTodo) todo: ZodTodo): number {
return 1;
}

@ReturnedSchema(ZodCat, true)
@Get()
getZodSomething() {
return [new Cat()]
}
}

@Controller('my-endpoint')
Expand Down
69 changes: 48 additions & 21 deletions method-definer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Parameter = Swagger.Parameter;
import { zodQuerySchemaKey, zodBodySchemaKey } from '@danet/zod';
import { ZodSchema } from 'npm:zod';
import { generateSchema, type OpenApiZodAny } from 'zod-openapi';
import { RETURNED_SCHEMA_KEY } from '../danet-zod/decorators.ts';

const primitiveTypes = [
'string',
Expand Down Expand Up @@ -251,14 +252,14 @@ export class MethodDefiner {
}

private addResponse(actualPath: Operation) {
const returnedValue = MetadataHelper.getMetadata(
let returnedValue = MetadataHelper.getMetadata<{
returnedType: Constructor;
isArray: boolean | undefined;
}>(
RETURNED_TYPE_KEY,
this.Controller.prototype,
this.methodName,
) as {
returnedType: Constructor;
isArray: boolean | undefined;
};
);
if (returnedValue) {
if (isPrimitive(returnedValue.returnedType.name.toLowerCase())) {
if (returnedValue.isArray) {
Expand All @@ -268,27 +269,53 @@ export class MethodDefiner {
type: returnedValue.returnedType.name.toLowerCase() as DataType,
},
}).setDescription('').get();
} else {
actualPath.responses[200] = new ResponseBuilder().jsonContent({
type: returnedValue.returnedType.name.toLowerCase() as DataType,
}).setDescription('').get();
return;
}
} else {
if (returnedValue.isArray) {
actualPath.responses[200] = new ResponseBuilder().jsonContent({
type: 'array',
items: {
'$ref': `#/components/schemas/${returnedValue.returnedType.name}`,
},
actualPath.responses[200] = new ResponseBuilder().jsonContent({
type: returnedValue.returnedType.name.toLowerCase() as DataType,
}).setDescription('').get();
} else {
actualPath.responses[200] = new ResponseBuilder().jsonContent({
return;
}
if (returnedValue.isArray) {
actualPath.responses[200] = new ResponseBuilder().jsonContent({
type: 'array',
items: {
'$ref': `#/components/schemas/${returnedValue.returnedType.name}`,
}).setDescription('').get();
}
this.generateTypeSchema(returnedValue.returnedType);
},
}).setDescription('').get();
} else {
actualPath.responses[200] = new ResponseBuilder().jsonContent({
'$ref': `#/components/schemas/${returnedValue.returnedType.name}`,
}).setDescription('').get();
}
this.generateTypeSchema(returnedValue.returnedType);
return;
}
const returnedSchema = MetadataHelper.getMetadata<{
returnedSchema: OpenApiZodAny;
isArray: boolean | undefined;
}>(
RETURNED_SCHEMA_KEY,
this.Controller.prototype,
this.methodName
);
console.log('returnedSchema', returnedSchema);
if (returnedSchema) {
const openApiSchema = this.generateZodSchema(returnedSchema.returnedSchema);
if (returnedSchema.isArray) {
actualPath.responses[200] = new ResponseBuilder().jsonContent({
type: 'array',
items: {
'$ref': `#/components/schemas/${openApiSchema.title}`,
},
}).setDescription('').get();
} else {
actualPath.responses[200] = new ResponseBuilder().jsonContent({
'$ref': `#/components/schemas/${openApiSchema.title}`,
}).setDescription('').get();
}
}

return null;
}

Expand Down
18 changes: 18 additions & 0 deletions spec/generate-schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ const expectedSpec = {
'description': '',
},
},
'get': {
'operationId': 'getZodSomething',
'responses': {
'200': {
'description': '',
'content': {
'application/json': {
'schema': {
'type': 'array',
'items': {
'$ref': '#/components/schemas/ZodCat',
},
},
},
},
},
},
},
},
'/my-endpoint': {
'get': {
Expand Down

0 comments on commit 4c64c85

Please sign in to comment.