forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-openapi.test.ts
64 lines (57 loc) · 1.98 KB
/
get-openapi.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
import { expectDiagnostics } from "@typespec/compiler/testing";
import { ok, strictEqual } from "assert";
import { it } from "vitest";
import { getOpenAPI3 } from "../src/openapi.js";
import { createOpenAPITestHost } from "./test-host.js";
it("can get openapi as an object", async () => {
const host = await createOpenAPITestHost();
host.addTypeSpecFile(
"./main.tsp",
`import "@typespec/http";
import "@typespec/rest";
import "@typespec/openapi";
import "@typespec/openapi3";
using TypeSpec.Rest;
using TypeSpec.Http;
using TypeSpec.OpenAPI;
@service
namespace Foo;
@get op get(): Item;
model Item { x: true }
model Bar { }; // unreachable
`
);
await host.compile("main.tsp");
const output = await getOpenAPI3(host.program, { "omit-unreachable-types": false });
const documentRecord = output[0];
ok(!documentRecord.versioned, "should not be versioned");
strictEqual(documentRecord.document.components!.schemas!["Item"].type, "object");
});
it("has diagnostics", async () => {
const host = await createOpenAPITestHost();
host.addTypeSpecFile(
"./main.tsp",
`import "@typespec/http";
import "@typespec/rest";
import "@typespec/openapi";
import "@typespec/openapi3";
using TypeSpec.Rest;
using TypeSpec.Http;
using TypeSpec.OpenAPI;
@service
namespace Foo;
op read(): {@minValue(455) @maxValue(495) @statusCode _: int32, content: string};
`
);
await host.compile("main.tsp");
const output = await getOpenAPI3(host.program, { "omit-unreachable-types": false });
const documentRecord = output[0];
ok(!documentRecord.versioned, "should not be versioned");
expectDiagnostics(documentRecord.diagnostics, [
{
code: "@typespec/openapi3/unsupported-status-code-range",
message:
"Status code range '455 to '495' is not supported. OpenAPI 3.0 can only represent range 1XX, 2XX, 3XX, 4XX and 5XX. Example: `@minValue(400) @maxValue(499)` for 4XX.",
},
]);
});