|
| 1 | +import assert from "assert"; |
| 2 | +import { expect, it } from "vitest"; |
| 3 | +import { OpenAPI3Response } from "../../src/types.js"; |
| 4 | +import { expectDecorators } from "./utils/expect.js"; |
| 5 | +import { tspForOpenAPI3 } from "./utils/tsp-for-openapi3.js"; |
| 6 | + |
| 7 | +const response: OpenAPI3Response = { |
| 8 | + description: "test response", |
| 9 | + content: { |
| 10 | + "application/json": { |
| 11 | + schema: { |
| 12 | + type: "object", |
| 13 | + properties: { |
| 14 | + message: { type: "string" }, |
| 15 | + }, |
| 16 | + }, |
| 17 | + }, |
| 18 | + }, |
| 19 | +}; |
| 20 | + |
| 21 | +it("generates operations with no params", async () => { |
| 22 | + const serviceNamespace = await tspForOpenAPI3({ |
| 23 | + paths: { |
| 24 | + "/": { |
| 25 | + get: { |
| 26 | + operationId: "rootGet", |
| 27 | + parameters: [], |
| 28 | + responses: { |
| 29 | + "200": response, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + const operations = serviceNamespace.operations; |
| 37 | + |
| 38 | + expect(operations.size).toBe(1); |
| 39 | + |
| 40 | + /* @route("/") @get op rootGet(): rootGet200ApplicationJsonResponse; */ |
| 41 | + const rootGet = operations.get("rootGet"); |
| 42 | + assert(rootGet, "rootGet operation not found"); |
| 43 | + |
| 44 | + /* @get @route("/") */ |
| 45 | + expectDecorators(rootGet.decorators, [{ name: "get" }, { name: "route", args: ["/"] }]); |
| 46 | + // verify no operation parameters |
| 47 | + expect(rootGet.parameters.properties.size).toBe(0); |
| 48 | + assert(rootGet.returnType.kind === "Model", "Expected model return type"); |
| 49 | + expect(rootGet.returnType.name).toBe("rootGet200ApplicationJsonResponse"); |
| 50 | +}); |
| 51 | + |
| 52 | +it("generates operations without common params", async () => { |
| 53 | + const serviceNamespace = await tspForOpenAPI3({ |
| 54 | + paths: { |
| 55 | + "/{id}": { |
| 56 | + get: { |
| 57 | + operationId: "idGet", |
| 58 | + parameters: [{ name: "id", in: "path", required: true, schema: { type: "string" } }], |
| 59 | + responses: { |
| 60 | + "200": response, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + const operations = serviceNamespace.operations; |
| 68 | + |
| 69 | + expect(operations.size).toBe(1); |
| 70 | + |
| 71 | + /* @route("/{id}") @get op idGet(@path id: string): idGet200ApplicationJsonResponse; */ |
| 72 | + const idGet = operations.get("idGet"); |
| 73 | + assert(idGet, "idGet operation not found"); |
| 74 | + |
| 75 | + /* @get @route("/{id}") */ |
| 76 | + expectDecorators(idGet.decorators, [{ name: "get" }, { name: "route", args: ["/{id}"] }]); |
| 77 | + |
| 78 | + expect(idGet.parameters.properties.size).toBe(1); |
| 79 | + const idParam = idGet.parameters.properties.get("id")!; |
| 80 | + expect(idParam).toMatchObject({ |
| 81 | + optional: false, |
| 82 | + type: { kind: "Scalar", name: "string" }, |
| 83 | + }); |
| 84 | + expectDecorators(idParam.decorators, [{ name: "path" }]); |
| 85 | + |
| 86 | + assert(idGet.returnType.kind === "Model", "Expected model return type"); |
| 87 | + expect(idGet.returnType.name).toBe("idGet200ApplicationJsonResponse"); |
| 88 | +}); |
| 89 | + |
| 90 | +it("generates operations with common params", async () => { |
| 91 | + const serviceNamespace = await tspForOpenAPI3({ |
| 92 | + paths: { |
| 93 | + "/{id}": { |
| 94 | + parameters: [{ name: "id", in: "path", required: true, schema: { type: "string" } }], |
| 95 | + get: { |
| 96 | + operationId: "idGet", |
| 97 | + parameters: [], |
| 98 | + responses: { |
| 99 | + "200": response, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + const operations = serviceNamespace.operations; |
| 107 | + |
| 108 | + expect(operations.size).toBe(1); |
| 109 | + |
| 110 | + /* @route("/{id}") @get op idGet(@path id: string): idGet200ApplicationJsonResponse; */ |
| 111 | + const idGet = operations.get("idGet"); |
| 112 | + assert(idGet, "idGet operation not found"); |
| 113 | + |
| 114 | + /* @get @route("/{id}") */ |
| 115 | + expectDecorators(idGet.decorators, [{ name: "get" }, { name: "route", args: ["/{id}"] }]); |
| 116 | + |
| 117 | + expect(idGet.parameters.properties.size).toBe(1); |
| 118 | + const idParam = idGet.parameters.properties.get("id")!; |
| 119 | + expect(idParam).toMatchObject({ |
| 120 | + optional: false, |
| 121 | + type: { kind: "Scalar", name: "string" }, |
| 122 | + }); |
| 123 | + expectDecorators(idParam.decorators, [{ name: "path" }]); |
| 124 | + |
| 125 | + assert(idGet.returnType.kind === "Model", "Expected model return type"); |
| 126 | + expect(idGet.returnType.name).toBe("idGet200ApplicationJsonResponse"); |
| 127 | +}); |
| 128 | + |
| 129 | +it("generates operations with common and specific params", async () => { |
| 130 | + const serviceNamespace = await tspForOpenAPI3({ |
| 131 | + paths: { |
| 132 | + "/{id}": { |
| 133 | + parameters: [{ name: "id", in: "path", required: true, schema: { type: "string" } }], |
| 134 | + get: { |
| 135 | + operationId: "idGet", |
| 136 | + parameters: [{ name: "foo", in: "query", schema: { type: "string" } }], |
| 137 | + responses: { |
| 138 | + "200": response, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }); |
| 144 | + |
| 145 | + const operations = serviceNamespace.operations; |
| 146 | + |
| 147 | + expect(operations.size).toBe(1); |
| 148 | + |
| 149 | + /* @route("/{id}") @get op idGet(@path id: string, @query foo?: string): idGet200ApplicationJsonResponse; */ |
| 150 | + const idGet = operations.get("idGet"); |
| 151 | + assert(idGet, "idGet operation not found"); |
| 152 | + |
| 153 | + /* @get @route("/{id}") */ |
| 154 | + expectDecorators(idGet.decorators, [{ name: "get" }, { name: "route", args: ["/{id}"] }]); |
| 155 | + |
| 156 | + /* (@path id: string, @query foo?: string) */ |
| 157 | + expect(idGet.parameters.properties.size).toBe(2); |
| 158 | + const idParam = idGet.parameters.properties.get("id")!; |
| 159 | + expect(idParam).toMatchObject({ |
| 160 | + optional: false, |
| 161 | + type: { kind: "Scalar", name: "string" }, |
| 162 | + }); |
| 163 | + expectDecorators(idParam.decorators, { name: "path" }); |
| 164 | + |
| 165 | + const fooParam = idGet.parameters.properties.get("foo")!; |
| 166 | + expect(fooParam).toMatchObject({ |
| 167 | + optional: true, |
| 168 | + type: { kind: "Scalar", name: "string" }, |
| 169 | + }); |
| 170 | + expectDecorators(fooParam.decorators, { name: "query" }); |
| 171 | + |
| 172 | + assert(idGet.returnType.kind === "Model", "Expected model return type"); |
| 173 | + expect(idGet.returnType.name).toBe("idGet200ApplicationJsonResponse"); |
| 174 | +}); |
| 175 | + |
| 176 | +it("supports overriding common params with operation params", async () => { |
| 177 | + const serviceNamespace = await tspForOpenAPI3({ |
| 178 | + paths: { |
| 179 | + "/{id}": { |
| 180 | + parameters: [ |
| 181 | + { name: "id", in: "path", required: true, schema: { type: "string" } }, |
| 182 | + { name: "x-header", in: "header", required: false, schema: { type: "string" } }, |
| 183 | + ], |
| 184 | + get: { |
| 185 | + operationId: "idGet", |
| 186 | + parameters: [ |
| 187 | + { name: "foo", in: "query", schema: { type: "string" } }, |
| 188 | + { name: "x-header", in: "header", required: true, schema: { type: "string" } }, |
| 189 | + ], |
| 190 | + responses: { |
| 191 | + "200": response, |
| 192 | + }, |
| 193 | + }, |
| 194 | + put: { |
| 195 | + operationId: "idPut", |
| 196 | + parameters: [], |
| 197 | + responses: { |
| 198 | + "200": response, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + }); |
| 204 | + |
| 205 | + const operations = serviceNamespace.operations; |
| 206 | + |
| 207 | + expect(operations.size).toBe(2); |
| 208 | + |
| 209 | + // `idGet` overrides the common `x-header` parameter with it's own, making it required |
| 210 | + /* @route("/{id}") @get op idGet(@path id: string, @query foo?: string, @header `x-header`: string): idGet200ApplicationJsonResponse; */ |
| 211 | + const idGet = operations.get("idGet"); |
| 212 | + assert(idGet, "idGet operation not found"); |
| 213 | + |
| 214 | + /* @get @route("/{id}") */ |
| 215 | + expectDecorators(idGet.decorators, [{ name: "get" }, { name: "route", args: ["/{id}"] }]); |
| 216 | + |
| 217 | + /* (@path id: string, @query foo?: string, @header `x-header`: string) */ |
| 218 | + expect(idGet.parameters.properties.size).toBe(3); |
| 219 | + const idParam = idGet.parameters.properties.get("id")!; |
| 220 | + expect(idParam).toMatchObject({ |
| 221 | + optional: false, |
| 222 | + type: { kind: "Scalar", name: "string" }, |
| 223 | + }); |
| 224 | + expectDecorators(idParam.decorators, { name: "path" }); |
| 225 | + |
| 226 | + const fooParam = idGet.parameters.properties.get("foo")!; |
| 227 | + expect(fooParam).toMatchObject({ |
| 228 | + optional: true, |
| 229 | + type: { kind: "Scalar", name: "string" }, |
| 230 | + }); |
| 231 | + expectDecorators(fooParam.decorators, { name: "query" }); |
| 232 | + |
| 233 | + const xHeaderParam = idGet.parameters.properties.get("x-header")!; |
| 234 | + expect(xHeaderParam).toMatchObject({ |
| 235 | + optional: false, |
| 236 | + type: { kind: "Scalar", name: "string" }, |
| 237 | + }); |
| 238 | + expectDecorators(xHeaderParam.decorators, { name: "header" }); |
| 239 | + |
| 240 | + assert(idGet.returnType.kind === "Model", "Expected model return type"); |
| 241 | + expect(idGet.returnType.name).toBe("idGet200ApplicationJsonResponse"); |
| 242 | + |
| 243 | + // `idPut` uses the common `x-header` parameter, which is marked optional |
| 244 | + /* @route("/{id}") @put op idPut(@path id: string, @header `x-header`: string): idPut200ApplicationJsonResponse; */ |
| 245 | + const idPut = operations.get("idPut"); |
| 246 | + assert(idPut, "idPut operation not found"); |
| 247 | + |
| 248 | + /* @put @route("/{id}") */ |
| 249 | + expectDecorators(idPut.decorators, [{ name: "put" }, { name: "route", args: ["/{id}"] }]); |
| 250 | + |
| 251 | + /* (@path id: string, @header `x-header`?: string) */ |
| 252 | + expect(idPut.parameters.properties.size).toBe(2); |
| 253 | + const idPutParam = idPut.parameters.properties.get("id")!; |
| 254 | + expect(idPutParam).toMatchObject({ |
| 255 | + optional: false, |
| 256 | + type: { kind: "Scalar", name: "string" }, |
| 257 | + }); |
| 258 | + expectDecorators(idPutParam.decorators, [{ name: "path" }]); |
| 259 | + |
| 260 | + const xHeaderSharedParam = idPut.parameters.properties.get("x-header")!; |
| 261 | + expect(xHeaderSharedParam).toMatchObject({ |
| 262 | + optional: true, |
| 263 | + type: { kind: "Scalar", name: "string" }, |
| 264 | + }); |
| 265 | + expectDecorators(xHeaderSharedParam.decorators, { name: "header" }); |
| 266 | + |
| 267 | + assert(idPut.returnType.kind === "Model", "Expected model return type"); |
| 268 | + expect(idPut.returnType.name).toBe("idPut200ApplicationJsonResponse"); |
| 269 | +}); |
0 commit comments