Skip to content

Commit

Permalink
test: coverage for v6 features
Browse files Browse the repository at this point in the history
Upstream introduces changes in v7 that having these tests in place will
expose how they break.
  • Loading branch information
vacas5 committed Dec 4, 2024
1 parent 344702f commit e6b4173
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
7 changes: 5 additions & 2 deletions mocks/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { z } from "zod";

extendZodWithOpenApi(z);

const BaseSchema = z.object({ name: z.string() });
const BaseSchema = z.object({
name: z.string(),
address: z.tuple([z.number().int(), z.string(), z.enum(["street", "avenue", "boulevard"])]).optional(),
});

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() });
const ResponseSchema = z.object({ success: z.boolean(), value: z.bigint().optional() });

export { BodySchema, ParamsSchema, QuerySchema, ResponseSchema };
77 changes: 73 additions & 4 deletions src/openAPI.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, spy, use } from "chai";
import chaiSpies from "chai-spies";
import { Router } from "express";
import type { PathItemObject } from "openapi3-ts/oas30";
import type { PathItemObject, SchemaObject } from "openapi3-ts/oas30";
import * as schemas from "../mocks/schemas";
import { buildOpenAPIDocument } from "./openAPI";
import { openAPIRoute } from "./openAPIRoute";
Expand Down Expand Up @@ -60,19 +60,62 @@ describe("buildOpenAPIDocument", () => {
const schemaPaths: string[] = ["../mocks/schemas"];
const errors = { 401: "Unauthorized", 403: "Forbidden" };

const document = buildOpenAPIDocument({ config, routers, schemaPaths, errors, openApiVersion });
const document = buildOpenAPIDocument({ config, routers, schemaPaths, errors, openApiVersion: "3.1.0" });

expect(document.components!.schemas).to.have.property("BodySchema");
expect(document.components!.schemas!.BodySchema).to.deep.equal({
type: "object",
properties: { name: { type: "string" } },
properties: {
address: {
items: {
anyOf: [
{
type: "integer",
},
{
type: "string",
},
{
enum: ["street", "avenue", "boulevard"],
type: "string",
},
],
},
maxItems: 3,
minItems: 3,
type: "array",
},
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" } },
properties: {
address: {
items: {
anyOf: [
{
type: "integer",
},
{
type: "string",
},
{
enum: ["street", "avenue", "boulevard"],
type: "string",
},
],
},
maxItems: 3,
minItems: 3,
type: "array",
},
name: { type: "string" },
age: { type: "number" },
},
required: ["name"],
title: "User details",
description: "Optional user information",
Expand Down Expand Up @@ -201,4 +244,30 @@ describe("buildOpenAPIDocument", () => {

expect(requestBodySchema.$ref.includes("BodySchema")).to.be.true;
});

it("should allow for bigints in schemas", () => {
const config = { info: { title: "Test API", version: "1.0.0" } };
const router = Router();
router.get(
"/test",
openAPIRoute(
{
tag: "Test",
summary: "Test route",
query: schemas.QuerySchema,
response: schemas.ResponseSchema,
},
(req, res) => res.json({ success: true, value: BigInt(1234) }),
),
);
const routers: Router[] = [router];
const schemaPaths: string[] = ["../mocks/schemas"];
const errors = { 401: "Unauthorized", 403: "Forbidden" };

const document = buildOpenAPIDocument({ config, routers, schemaPaths, errors, openApiVersion });
const responseSchema = document.components!.schemas!.ResponseSchema as SchemaObject;
const value = responseSchema.properties!.value as SchemaObject;

expect(value.type).to.equal("integer");
});
});

0 comments on commit e6b4173

Please sign in to comment.