forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverloads.test.ts
128 lines (113 loc) · 4.42 KB
/
overloads.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { deepStrictEqual, ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { OpenAPI3Document } from "../src/types.js";
import { openApiFor } from "./test-host.js";
describe("openapi3: overloads", () => {
describe("overloads use same endpoint", () => {
let res: OpenAPI3Document;
beforeEach(async () => {
res = await openApiFor(
`
@route("/upload")
op upload(data: string | bytes, @header contentType: "text/plain" | "application/octet-stream"): void;
@overload(upload)
op uploadString(data: string, @header contentType: "text/plain" ): void;
@overload(upload)
op uploadBytes(data: bytes, @header contentType: "application/octet-stream"): void;
`
);
});
it("should create a single path using the overload base name", async () => {
strictEqual(Object.keys(res.paths).length, 1);
const operation = res.paths["/upload"].post;
ok(operation);
strictEqual(operation.operationId, "upload");
deepStrictEqual(Object.keys(operation.requestBody.content), [
"text/plain",
"application/octet-stream",
]);
});
});
describe("overloads all use different endpoint", () => {
let res: OpenAPI3Document;
beforeEach(async () => {
res = await openApiFor(
`
@route("/upload")
op upload(data: string | bytes, @header contentType: "text/plain" | "application/octet-stream"): void;
@overload(upload)
@route("/uploadString")
op uploadString(data: string, @header contentType: "text/plain" ): void;
@overload(upload)
@route("/uploadBytes")
op uploadBytes(data: bytes, @header contentType: "application/octet-stream"): void;
`
);
});
it("should create an endpoint for each overload", () => {
const stringOperation = res.paths["/uploadString"].post;
const bytesOperation = res.paths["/uploadBytes"].post;
ok(stringOperation);
ok(bytesOperation);
strictEqual(stringOperation.operationId, "uploadString");
strictEqual(bytesOperation.operationId, "uploadBytes");
deepStrictEqual(Object.keys(stringOperation.requestBody.content), ["text/plain"]);
deepStrictEqual(Object.keys(bytesOperation.requestBody.content), [
"application/octet-stream",
]);
});
it("should still create endpoint for operation overload base", async () => {
const baseOperation = res.paths["/upload"].post;
ok(baseOperation);
strictEqual(baseOperation.operationId, "upload");
deepStrictEqual(Object.keys(baseOperation.requestBody.content), [
"text/plain",
"application/octet-stream",
]);
});
});
describe("some overload all use different endpoint", () => {
let res: OpenAPI3Document;
beforeEach(async () => {
res = await openApiFor(
`
@route("/upload")
op upload(data: string | bytes, @header contentType: "text/plain" | "application/octet-stream"): void;
@overload(upload)
@route("/uploadString")
op uploadString(data: string, @header contentType: "text/plain" ): void;
@overload(upload)
op uploadBytes(data: bytes, @header contentType: "application/octet-stream"): void;
`
);
});
it("should only create an endpoint for overloads with a different route", () => {
strictEqual(Object.keys(res.paths).length, 2);
ok(res.paths["/upload"].post);
ok(res.paths["/uploadString"].post);
});
});
it("overloads work inside an interface", async () => {
const _ = await openApiFor(`
interface Foo {
op doStringOrInt(a?: string, b?: int32, c?: Record<string>): { @TypeSpec.Http.header("content-type") contenType: "application/text", @TypeSpec.Http.body data: string }| int32;
@overload(Foo.doStringOrInt)
op doString(a: string): { @TypeSpec.Http.header("content-type") contenType: "application/text", @TypeSpec.Http.body data: string };
@overload(Foo.doStringOrInt)
op doInt(b: int32): int32;
}
`);
});
it("can overload a boolean property with true or false", async () => {
const _ = await openApiFor(`
@test
op someThing(param: boolean): string | int32;
@test
@overload(someThing)
op someStringThing(param: true): string;
@test
@overload(someThing)
op someNumberThing(param: false): int32;
`);
});
});