forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnullable-properties.test.ts
74 lines (68 loc) · 1.93 KB
/
nullable-properties.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { deepStrictEqual, ok } from "assert";
import { describe, expect, it } from "vitest";
import { OpenAPI3SchemaProperty } from "../src/types.js";
import { oapiForModel, openApiFor } from "./test-host.js";
describe("openapi3: nullable properties", () => {
it("makes nullable schema when union with null", async () => {
const res = await openApiFor(
`
model Thing {
id: string;
properties: Thing | null;
}
op doStuff(): Thing;
`
);
deepStrictEqual(res.components.schemas.Thing.properties.properties, {
type: "object",
allOf: [{ $ref: "#/components/schemas/Thing" }],
nullable: true,
});
});
it("handles a nullable enum", async () => {
const res = await oapiForModel(
"X",
`
enum A {
a: 1
}
model X {
prop: A | null
}
`
);
deepStrictEqual(res.schemas.X.properties.prop.oneOf, [
{
$ref: "#/components/schemas/A",
},
]);
ok(res.schemas.X.properties.prop.nullable);
});
describe("when used in circular references", () => {
async function expectInCircularReference(ref: string, value: OpenAPI3SchemaProperty) {
const res = await openApiFor(
`
model Test {
children: ${ref} | null;
}
op test(filters: ${ref}): {}[];
`
);
expect(res.components.schemas.Test.properties.children).toEqual(value);
}
it("keep nullable array inline", async () => {
await expectInCircularReference("Test[]", {
type: "array",
items: { $ref: "#/components/schemas/Test" },
nullable: true,
});
});
it("keep nullable Record<T> inline", async () => {
await expectInCircularReference("Record<Test>", {
type: "object",
additionalProperties: { $ref: "#/components/schemas/Test" },
nullable: true,
});
});
});
});