From 297f72b01669bd4a7ed1feed86822088a3265368 Mon Sep 17 00:00:00 2001 From: Sam Chung Date: Mon, 24 Apr 2023 12:14:11 +1000 Subject: [PATCH] Handle Loose References to Zod Objects (#8) --- src/rules/require-openapi/rule.test.ts | 1 + src/rules/require-openapi/rule.ts | 6 +++- src/rules/require-openapi/tests/reference.ts | 33 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/rules/require-openapi/tests/reference.ts diff --git a/src/rules/require-openapi/rule.test.ts b/src/rules/require-openapi/rule.test.ts index 7b01135..6a6c175 100644 --- a/src/rules/require-openapi/rule.test.ts +++ b/src/rules/require-openapi/rule.test.ts @@ -21,6 +21,7 @@ ruleTester.run(ruleName, rule, { test('object-shape'), test('literal-no-openapi'), test('optional-no-openapi'), + test('reference'), ], invalid: [ { diff --git a/src/rules/require-openapi/rule.ts b/src/rules/require-openapi/rule.ts index d18662a..78b57fb 100644 --- a/src/rules/require-openapi/rule.ts +++ b/src/rules/require-openapi/rule.ts @@ -36,6 +36,10 @@ export const rule = createRule({ } }, Property(node) { + if (node.value.type === 'Identifier') { + return; + } + const type = getType(node, context); if (!type?.isZodType || type.name === 'ZodLiteral') { @@ -44,7 +48,7 @@ export const rule = createRule({ const openApiCallExpression = findOpenApiCallExpression(node); - if (!openApiCallExpression && !getInferredComment(node, context)) { + if (!openApiCallExpression) { return context.report({ messageId: 'open-api-required', node: node.key, diff --git a/src/rules/require-openapi/tests/reference.ts b/src/rules/require-openapi/tests/reference.ts new file mode 100644 index 0000000..1407351 --- /dev/null +++ b/src/rules/require-openapi/tests/reference.ts @@ -0,0 +1,33 @@ +import { z } from 'zod'; +import { ZodOpenApiOperationObject, extendZodWithOpenApi } from 'zod-openapi'; +extendZodWithOpenApi(z); + +const QuerySchema = z + .object({ + a: z.string().openapi({ description: 'a', example: 'a' }), + }) + .openapi({ description: 'Query schema' }); + +const GetJobResponseSchema = z + .object({ + a: z.string().openapi({ description: 'a', example: 'a' }), + }) + .openapi({ description: 'Response schema' }); + +export const getJobOperation: ZodOpenApiOperationObject = { + operationId: 'getJob', + summary: 'Get Job', + requestParams: { + query: QuerySchema, + }, + responses: { + '200': { + description: 'Successful operation', + content: { + 'application/json': { + schema: GetJobResponseSchema, + }, + }, + }, + }, +};