Skip to content

Commit

Permalink
test(openapi3): Add tests for Map<K, V> to be additionalProperties
Browse files Browse the repository at this point in the history
Signed-off-by: Naoki Ikeguchi <[email protected]>
  • Loading branch information
siketyan committed Jan 12, 2025
1 parent 7abf3ee commit d22db35
Showing 1 changed file with 130 additions and 47 deletions.
177 changes: 130 additions & 47 deletions packages/openapi3/test/additional-properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,161 @@ import { describe, it } from "vitest";
import { oapiForModel } from "./test-host.js";

describe("openapi3: Additional properties", () => {
describe("extends Record<T>", () => {
it("doesn't set additionalProperties on model itself", async () => {
const res = await oapiForModel("Pet", `model Pet extends Record<unknown> {};`);
deepStrictEqual(res.schemas.Pet.additionalProperties, undefined);
describe("Record<T>", () => {
describe("extends Record<T>", () => {
it("doesn't set additionalProperties on model itself", async () => {
const res = await oapiForModel("Pet", `model Pet extends Record<unknown> {};`);
deepStrictEqual(res.schemas.Pet.additionalProperties, undefined);
});

it("links to an allOf of the Record<unknown> schema", async () => {
const res = await oapiForModel("Pet", `model Pet extends Record<unknown> {};`);
deepStrictEqual(res.schemas.Pet.allOf, [{ type: "object", additionalProperties: {} }]);
});

it("include model properties", async () => {
const res = await oapiForModel(
"Pet",
`model Pet extends Record<unknown> { name: string };`,
);
deepStrictEqual(res.schemas.Pet.properties, {
name: { type: "string" },
});
});
});

it("links to an allOf of the Record<unknown> schema", async () => {
const res = await oapiForModel("Pet", `model Pet extends Record<unknown> {};`);
deepStrictEqual(res.schemas.Pet.allOf, [{ type: "object", additionalProperties: {} }]);
describe("is Record<T>", () => {
it("set additionalProperties on model itself", async () => {
const res = await oapiForModel("Pet", `model Pet is Record<unknown> {};`);
deepStrictEqual(res.schemas.Pet.additionalProperties, {});
});

it("set additional properties type", async () => {
const res = await oapiForModel("Pet", `model Pet is Record<string> {};`);
deepStrictEqual(res.schemas.Pet.additionalProperties, {
type: "string",
});
});

it("include model properties", async () => {
const res = await oapiForModel("Pet", `model Pet is Record<unknown> { name: string };`);
deepStrictEqual(res.schemas.Pet.properties, {
name: { type: "string" },
});
});
});

it("include model properties", async () => {
const res = await oapiForModel("Pet", `model Pet extends Record<unknown> { name: string };`);
deepStrictEqual(res.schemas.Pet.properties, {
name: { type: "string" },
describe("referencing Record<T>", () => {
it("add additionalProperties inline for property of type Record<unknown>", async () => {
const res = await oapiForModel(
"Pet",
`
model Pet { details: Record<unknown> };
`,
);

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<Value> {};
`,
);

ok(res.isRef);
ok(res.schemas.Pet, "expected definition named Pet");
deepStrictEqual(res.schemas.Pet.additionalProperties, {
$ref: "#/components/schemas/Value",
});
});
});

describe("is Record<T>", () => {
it("set additionalProperties on model itself", async () => {
const res = await oapiForModel("Pet", `model Pet is Record<unknown> {};`);
deepStrictEqual(res.schemas.Pet.additionalProperties, {});
describe("Map<K, V>", () => {
describe("extends Map<K, V>", () => {
it("doesn't set additionalProperties on model itself", async () => {
const res = await oapiForModel("Pet", `model Pet extends Map<string, unknown> {};`);
deepStrictEqual(res.schemas.Pet.additionalProperties, undefined);
});

it("links to an allOf of the Map<string, unknown> schema", async () => {
const res = await oapiForModel("Pet", `model Pet extends Map<string, unknown> {};`);
deepStrictEqual(res.schemas.Pet.allOf, [{ type: "object", additionalProperties: {} }]);
});

it("include model properties", async () => {
const res = await oapiForModel(
"Pet",
`model Pet extends Map<string, unknown> { 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<string> {};`);
deepStrictEqual(res.schemas.Pet.additionalProperties, {
type: "string",
describe("is Map<K, V>", () => {
it("set additionalProperties on model itself", async () => {
const res = await oapiForModel("Pet", `model Pet is Map<string, unknown> {};`);
deepStrictEqual(res.schemas.Pet.additionalProperties, {});
});

it("set additional properties type", async () => {
const res = await oapiForModel("Pet", `model Pet is Map<string, string> {};`);
deepStrictEqual(res.schemas.Pet.additionalProperties, {
type: "string",
});
});

it("include model properties", async () => {
const res = await oapiForModel(
"Pet",
`model Pet is Map<string, unknown> { name: string };`,
);
deepStrictEqual(res.schemas.Pet.properties, {
name: { type: "string" },
});
});
});

it("include model properties", async () => {
const res = await oapiForModel("Pet", `model Pet is Record<unknown> { name: string };`);
deepStrictEqual(res.schemas.Pet.properties, {
name: { type: "string" },
describe("referencing Map<K, V>", () => {
it("add additionalProperties inline for property of type Map<string, unknown>", async () => {
const res = await oapiForModel("Pet", `model Pet { details: Map<string, unknown> };`);

ok(res.isRef);
ok(res.schemas.Pet, "expected definition named Pet");
deepStrictEqual(res.schemas.Pet.properties.details, {
type: "object",
additionalProperties: {},
});
});
});
});

describe("referencing Record<T>", () => {
it("add additionalProperties inline for property of type Record<unknown>", async () => {
it("set additionalProperties if model extends Map with leaf type", async () => {
const res = await oapiForModel(
"Pet",
`
model Pet { details: Record<unknown> };
@doc("value")
scalar Value;
model Pet is Map<string, Value> {};
`,
);

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<Value> {};
`,
);

ok(res.isRef);
ok(res.schemas.Pet, "expected definition named Pet");
deepStrictEqual(res.schemas.Pet.additionalProperties, {
$ref: "#/components/schemas/Value",
});
});
});

0 comments on commit d22db35

Please sign in to comment.