From d22db35ddca7e0fa49dba53adb41bc63ced6b19e Mon Sep 17 00:00:00 2001 From: Naoki Ikeguchi Date: Sun, 12 Jan 2025 20:54:37 +0900 Subject: [PATCH] test(openapi3): Add tests for `Map` to be additionalProperties Signed-off-by: Naoki Ikeguchi --- .../test/additional-properties.test.ts | 177 +++++++++++++----- 1 file changed, 130 insertions(+), 47 deletions(-) diff --git a/packages/openapi3/test/additional-properties.test.ts b/packages/openapi3/test/additional-properties.test.ts index 0fd065f5d9..341aa91585 100644 --- a/packages/openapi3/test/additional-properties.test.ts +++ b/packages/openapi3/test/additional-properties.test.ts @@ -3,78 +3,161 @@ import { describe, it } from "vitest"; import { oapiForModel } from "./test-host.js"; describe("openapi3: Additional properties", () => { - describe("extends Record", () => { - it("doesn't set additionalProperties on model itself", async () => { - const res = await oapiForModel("Pet", `model Pet extends Record {};`); - deepStrictEqual(res.schemas.Pet.additionalProperties, undefined); + describe("Record", () => { + describe("extends Record", () => { + it("doesn't set additionalProperties on model itself", async () => { + const res = await oapiForModel("Pet", `model Pet extends Record {};`); + deepStrictEqual(res.schemas.Pet.additionalProperties, undefined); + }); + + it("links to an allOf of the Record schema", async () => { + const res = await oapiForModel("Pet", `model Pet extends Record {};`); + deepStrictEqual(res.schemas.Pet.allOf, [{ type: "object", additionalProperties: {} }]); + }); + + it("include model properties", async () => { + const res = await oapiForModel( + "Pet", + `model Pet extends Record { name: string };`, + ); + deepStrictEqual(res.schemas.Pet.properties, { + name: { type: "string" }, + }); + }); }); - it("links to an allOf of the Record schema", async () => { - const res = await oapiForModel("Pet", `model Pet extends Record {};`); - deepStrictEqual(res.schemas.Pet.allOf, [{ type: "object", additionalProperties: {} }]); + describe("is Record", () => { + it("set additionalProperties on model itself", async () => { + const res = await oapiForModel("Pet", `model Pet is Record {};`); + deepStrictEqual(res.schemas.Pet.additionalProperties, {}); + }); + + it("set additional properties type", async () => { + const res = await oapiForModel("Pet", `model Pet is Record {};`); + deepStrictEqual(res.schemas.Pet.additionalProperties, { + type: "string", + }); + }); + + it("include model properties", async () => { + const res = await oapiForModel("Pet", `model Pet is Record { name: string };`); + deepStrictEqual(res.schemas.Pet.properties, { + name: { type: "string" }, + }); + }); }); - it("include model properties", async () => { - const res = await oapiForModel("Pet", `model Pet extends Record { name: string };`); - deepStrictEqual(res.schemas.Pet.properties, { - name: { type: "string" }, + describe("referencing Record", () => { + it("add additionalProperties inline for property of type Record", async () => { + const res = await oapiForModel( + "Pet", + ` + model Pet { details: Record }; + `, + ); + + ok(res.isRef); + ok(res.schemas.Pet, "expected definition named Pet"); + deepStrictEqual(res.schemas.Pet.properties.details, { + type: "object", + additionalProperties: {}, + }); + }); + }); + + it("set additionalProperties if model extends Record with leaf type", async () => { + const res = await oapiForModel( + "Pet", + ` + @doc("value") + scalar Value; + model Pet is Record {}; + `, + ); + + ok(res.isRef); + ok(res.schemas.Pet, "expected definition named Pet"); + deepStrictEqual(res.schemas.Pet.additionalProperties, { + $ref: "#/components/schemas/Value", }); }); }); - describe("is Record", () => { - it("set additionalProperties on model itself", async () => { - const res = await oapiForModel("Pet", `model Pet is Record {};`); - deepStrictEqual(res.schemas.Pet.additionalProperties, {}); + describe("Map", () => { + describe("extends Map", () => { + it("doesn't set additionalProperties on model itself", async () => { + const res = await oapiForModel("Pet", `model Pet extends Map {};`); + deepStrictEqual(res.schemas.Pet.additionalProperties, undefined); + }); + + it("links to an allOf of the Map schema", async () => { + const res = await oapiForModel("Pet", `model Pet extends Map {};`); + deepStrictEqual(res.schemas.Pet.allOf, [{ type: "object", additionalProperties: {} }]); + }); + + it("include model properties", async () => { + const res = await oapiForModel( + "Pet", + `model Pet extends Map { name: string };`, + ); + deepStrictEqual(res.schemas.Pet.properties, { + name: { type: "string" }, + }); + }); }); - it("set additional properties type", async () => { - const res = await oapiForModel("Pet", `model Pet is Record {};`); - deepStrictEqual(res.schemas.Pet.additionalProperties, { - type: "string", + describe("is Map", () => { + it("set additionalProperties on model itself", async () => { + const res = await oapiForModel("Pet", `model Pet is Map {};`); + deepStrictEqual(res.schemas.Pet.additionalProperties, {}); + }); + + it("set additional properties type", async () => { + const res = await oapiForModel("Pet", `model Pet is Map {};`); + deepStrictEqual(res.schemas.Pet.additionalProperties, { + type: "string", + }); + }); + + it("include model properties", async () => { + const res = await oapiForModel( + "Pet", + `model Pet is Map { name: string };`, + ); + deepStrictEqual(res.schemas.Pet.properties, { + name: { type: "string" }, + }); }); }); - it("include model properties", async () => { - const res = await oapiForModel("Pet", `model Pet is Record { name: string };`); - deepStrictEqual(res.schemas.Pet.properties, { - name: { type: "string" }, + describe("referencing Map", () => { + it("add additionalProperties inline for property of type Map", async () => { + const res = await oapiForModel("Pet", `model Pet { details: Map };`); + + ok(res.isRef); + ok(res.schemas.Pet, "expected definition named Pet"); + deepStrictEqual(res.schemas.Pet.properties.details, { + type: "object", + additionalProperties: {}, + }); }); }); - }); - describe("referencing Record", () => { - it("add additionalProperties inline for property of type Record", async () => { + it("set additionalProperties if model extends Map with leaf type", async () => { const res = await oapiForModel( "Pet", ` - model Pet { details: Record }; + @doc("value") + scalar Value; + model Pet is Map {}; `, ); ok(res.isRef); ok(res.schemas.Pet, "expected definition named Pet"); - deepStrictEqual(res.schemas.Pet.properties.details, { - type: "object", - additionalProperties: {}, + deepStrictEqual(res.schemas.Pet.additionalProperties, { + $ref: "#/components/schemas/Value", }); }); }); - - it("set additionalProperties if model extends Record with leaf type", async () => { - const res = await oapiForModel( - "Pet", - ` - @doc("value") - scalar Value; - model Pet is Record {}; - `, - ); - - ok(res.isRef); - ok(res.schemas.Pet, "expected definition named Pet"); - deepStrictEqual(res.schemas.Pet.additionalProperties, { - $ref: "#/components/schemas/Value", - }); - }); });