microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e966efd30e552f4e84d5ae7224515199738ddd8e

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/openapi3/test/test-primitive-types.ts

85lines · modecode

1import { deepStrictEqual, ok } from "assert";
2import { oapiForModel } from "./test-host.js";
3
4describe("openapi3: primitives", () => {
5 it("defines models extended from primitives", async () => {
6 const res = await oapiForModel(
7 "Pet",
8 `
9 model shortString is string {}
10 model Pet { name: shortString };
11 `
12 );
13
14 ok(res.isRef);
15 ok(res.schemas.shortString, "expected definition named shortString");
16 ok(res.schemas.Pet, "expected definition named Pet");
17 deepStrictEqual(res.schemas.shortString, {
18 type: "string",
19 });
20 });
21
22 it("apply description on extended primitive", async () => {
23 const res = await oapiForModel(
24 "shortString",
25 `
26 @doc("My custom description")
27 model shortString is string {}
28 `
29 );
30
31 ok(res.isRef);
32 ok(res.schemas.shortString, "expected definition named shortString");
33 deepStrictEqual(res.schemas.shortString, {
34 type: "string",
35 description: "My custom description",
36 });
37 });
38
39 it("defines models extended from primitives with attrs", async () => {
40 const res = await oapiForModel(
41 "Pet",
42 `
43 @maxLength(10) @minLength(10)
44 model shortString is string {}
45 model Pet { name: shortString };
46 `
47 );
48
49 ok(res.isRef);
50 ok(res.schemas.shortString, "expected definition named shortString");
51 ok(res.schemas.Pet, "expected definition named Pet");
52 deepStrictEqual(res.schemas.shortString, {
53 type: "string",
54 minLength: 10,
55 maxLength: 10,
56 });
57 });
58
59 it("defines models extended from primitives with new attrs", async () => {
60 const res = await oapiForModel(
61 "Pet",
62 `
63 @maxLength(10)
64 model shortString is string {}
65 @minLength(1)
66 model shortButNotEmptyString is shortString {};
67 model Pet { name: shortButNotEmptyString, breed: shortString };
68 `
69 );
70 ok(res.isRef);
71 ok(res.schemas.shortString, "expected definition named shortString");
72 ok(res.schemas.shortButNotEmptyString, "expected definition named shortButNotEmptyString");
73 ok(res.schemas.Pet, "expected definition named Pet");
74
75 deepStrictEqual(res.schemas.shortString, {
76 type: "string",
77 maxLength: 10,
78 });
79 deepStrictEqual(res.schemas.shortButNotEmptyString, {
80 type: "string",
81 minLength: 1,
82 maxLength: 10,
83 });
84 });
85});
86