Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: extend schemas with openapi #17

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions mocks/schemas.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
import { z } from "zod";

const BodySchema = z.object({ name: z.string() });
const QuerySchema = z.object({ age: z.number().optional() });
extendZodWithOpenApi(z);

const BaseSchema = z.object({ name: z.string() });

const BodySchema = BaseSchema.openapi("Body", { title: "User", description: "Required user information" });
const QuerySchema = BaseSchema.extend({ age: z.number().optional() }).openapi({
title: "User details",
description: "Optional user information",
});
const ParamsSchema = z.object({ id: z.string() });
const ResponseSchema = z.object({ success: z.boolean() });

export { BodySchema, ParamsSchema, QuerySchema, ResponseSchema, UnusedSchema };
export { BodySchema, ParamsSchema, QuerySchema, ResponseSchema };
9 changes: 9 additions & 0 deletions src/openAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ describe("buildOpenAPIDocument", () => {
type: "object",
properties: { name: { type: "string" } },
required: ["name"],
title: "User",
description: "Required user information",
});
expect(document.components!.schemas!.QuerySchema).to.deep.equal({
type: "object",
properties: { name: { type: "string" }, age: { type: "number" } },
required: ["name"],
title: "User details",
description: "Optional user information",
});
});

Expand Down
10 changes: 5 additions & 5 deletions src/openAPIRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("openAPIRoute", () => {
},
);

const req = mockRequest({ name: "John" }, { age: 30 }, { id: "123" });
const req = mockRequest({ name: "John" }, { age: 30, name: "John" }, { id: "123" });
const res = mockResponse();
const next = mockNext();

Expand All @@ -75,7 +75,7 @@ describe("openAPIRoute", () => {
res.json({ success: true });
});

const req = mockRequest({ name: 123 }, { age: 30 }, { id: "123" });
const req = mockRequest({ name: 123 }, { age: 30, name: "John" }, { id: "123" });
const res = mockResponse();
const next = mockNext();

Expand All @@ -91,7 +91,7 @@ describe("openAPIRoute", () => {
res.json({ success: true });
});

const req = mockRequest({ name: "John" }, { age: "thirty" }, { id: "123" });
const req = mockRequest({ name: "John" }, { age: "thirty", name: "John" }, { id: "123" });
const res = mockResponse();
const next = mockNext();

Expand All @@ -107,7 +107,7 @@ describe("openAPIRoute", () => {
res.json({ success: true });
});

const req = mockRequest({ name: "John" }, { age: 30 }, { id: 123 });
const req = mockRequest({ name: "John" }, { age: 30, name: "John" }, { id: 123 });
const res = mockResponse();
const next = mockNext();

Expand All @@ -124,7 +124,7 @@ describe("openAPIRoute", () => {
res.json({ success: "true" });
});

const req = mockRequest({ name: "John" }, { age: 30 }, { id: "123" });
const req = mockRequest({ name: "John" }, { age: 30, name: "John" }, { id: "123" });
const res = mockResponse();
const next = mockNext();

Expand Down
Loading