microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/openapi3/test/array.test.ts
79lines · modecode
| 1 | import { deepStrictEqual, ok } from "assert"; |
| 2 | import { oapiForModel } from "./test-host.js"; |
| 3 | |
| 4 | describe("openapi3: Array", () => { |
| 5 | it("defines array inline", async () => { |
| 6 | const res = await oapiForModel( |
| 7 | "Pet", |
| 8 | ` |
| 9 | model Pet { names: string[] }; |
| 10 | ` |
| 11 | ); |
| 12 | |
| 13 | ok(res.isRef); |
| 14 | ok(res.schemas.Pet, "expected definition named Pet"); |
| 15 | deepStrictEqual(res.schemas.Pet.properties.names, { |
| 16 | type: "array", |
| 17 | items: { type: "string" }, |
| 18 | "x-cadl-name": "string[]", |
| 19 | }); |
| 20 | }); |
| 21 | |
| 22 | it("define a named array using model is", async () => { |
| 23 | const res = await oapiForModel( |
| 24 | "Pet", |
| 25 | ` |
| 26 | model PetNames is string[] {} |
| 27 | model Pet { names: PetNames }; |
| 28 | ` |
| 29 | ); |
| 30 | |
| 31 | ok(res.isRef); |
| 32 | ok(res.schemas.PetNames, "expected definition named myArray"); |
| 33 | ok(res.schemas.Pet, "expected definition named Pet"); |
| 34 | deepStrictEqual(res.schemas.PetNames, { |
| 35 | type: "array", |
| 36 | items: { type: "string" }, |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | it("can specify minItems using @minItems decorator", async () => { |
| 41 | const res = await oapiForModel( |
| 42 | "Pet", |
| 43 | ` |
| 44 | model Pet { |
| 45 | @minItems(1) |
| 46 | names: string[] |
| 47 | }; |
| 48 | ` |
| 49 | ); |
| 50 | |
| 51 | ok(res.schemas.Pet, "expected definition named Pet"); |
| 52 | deepStrictEqual(res.schemas.Pet.properties.names, { |
| 53 | type: "array", |
| 54 | minItems: 1, |
| 55 | items: { type: "string" }, |
| 56 | "x-cadl-name": "string[]", |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | it("can specify maxItems using @maxItems decorator", async () => { |
| 61 | const res = await oapiForModel( |
| 62 | "Pet", |
| 63 | ` |
| 64 | model Pet { |
| 65 | @maxItems(3) |
| 66 | names: string[] |
| 67 | }; |
| 68 | ` |
| 69 | ); |
| 70 | |
| 71 | ok(res.schemas.Pet, "expected definition named Pet"); |
| 72 | deepStrictEqual(res.schemas.Pet.properties.names, { |
| 73 | type: "array", |
| 74 | maxItems: 3, |
| 75 | items: { type: "string" }, |
| 76 | "x-cadl-name": "string[]", |
| 77 | }); |
| 78 | }); |
| 79 | }); |
| 80 | |