diff --git a/mocks/schemas.js b/mocks/schemas.js index 2b3eb68..6caca9f 100644 --- a/mocks/schemas.js +++ b/mocks/schemas.js @@ -3,7 +3,10 @@ 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({ @@ -11,6 +14,6 @@ const QuerySchema = BaseSchema.extend({ age: z.number().optional() }).openapi({ 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 }; diff --git a/src/openAPI.test.ts b/src/openAPI.test.ts index 75174c6..e9524db 100644 --- a/src/openAPI.test.ts +++ b/src/openAPI.test.ts @@ -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"; @@ -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", @@ -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"); + }); });