Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OpenApiPatch.test.ts

400 lines15.8 KB

1import * as OpenApiPatch from "@effect/openapi-generator/OpenApiPatch"
2import * as NodeServices from "@effect/platform-node/NodeServices"
3import { assert, describe, it } from "@effect/vitest"
4import * as Effect from "effect/Effect"
5import * as Exit from "effect/Exit"
6import * as Path from "effect/Path"
7​
8const testLayer = NodeServices.layer
9​
10describe("OpenApiPatch", () => {
11 describe("parsePatchInput", () => {
12 describe("inline JSON", () => {
13 it.effect("parses valid inline JSON patch", () =>
14 Effect.gen(function*() {
15 const result = yield* OpenApiPatch.parsePatchInput(
16 "[{\"op\":\"add\",\"path\":\"/foo\",\"value\":\"bar\"}]"
17 )
18 assert.deepStrictEqual(result, [{ op: "add", path: "/foo", value: "bar" }])
19 }).pipe(Effect.provide(testLayer)))
20​
21 it.effect("parses inline JSON with multiple operations", () =>
22 Effect.gen(function*() {
23 const result = yield* OpenApiPatch.parsePatchInput(
24 "[{\"op\":\"add\",\"path\":\"/a\",\"value\":1},{\"op\":\"remove\",\"path\":\"/b\"},{\"op\":\"replace\",\"path\":\"/c\",\"value\":true}]"
25 )
26 assert.strictEqual(result.length, 3)
27 assert.strictEqual(result[0].op, "add")
28 assert.strictEqual(result[1].op, "remove")
29 assert.strictEqual(result[2].op, "replace")
30 }).pipe(Effect.provide(testLayer)))
31​
32 it.effect("fails on invalid JSON syntax", () =>
33 Effect.gen(function*() {
34 const exit = yield* Effect.exit(
35 OpenApiPatch.parsePatchInput("[{\"op\":\"add\" \"path\":\"/foo\"}]")
36 )
37 assert.isTrue(Exit.isFailure(exit))
38 }).pipe(Effect.provide(testLayer)))
39​
40 it.effect("fails on unsupported operation", () =>
41 Effect.gen(function*() {
42 const exit = yield* Effect.exit(
43 OpenApiPatch.parsePatchInput("[{\"op\":\"copy\",\"from\":\"/a\",\"path\":\"/b\"}]")
44 )
45 assert.isTrue(Exit.isFailure(exit))
46 }).pipe(Effect.provide(testLayer)))
47​
48 it.effect("fails on missing path field", () =>
49 Effect.gen(function*() {
50 const exit = yield* Effect.exit(
51 OpenApiPatch.parsePatchInput("[{\"op\":\"add\",\"value\":\"test\"}]")
52 )
53 assert.isTrue(Exit.isFailure(exit))
54 }).pipe(Effect.provide(testLayer)))
55​
56 it.effect("fails on missing value for add operation", () =>
57 Effect.gen(function*() {
58 const exit = yield* Effect.exit(
59 OpenApiPatch.parsePatchInput("[{\"op\":\"add\",\"path\":\"/foo\"}]")
60 )
61 assert.isTrue(Exit.isFailure(exit))
62 }).pipe(Effect.provide(testLayer)))
63​
64 it.effect("allows missing value for remove operation", () =>
65 Effect.gen(function*() {
66 const result = yield* OpenApiPatch.parsePatchInput(
67 "[{\"op\":\"remove\",\"path\":\"/foo\"}]"
68 )
69 assert.deepStrictEqual(result, [{ op: "remove", path: "/foo" }])
70 }).pipe(Effect.provide(testLayer)))
71 })
72​
73 describe("file paths", () => {
74 it.effect("parses valid JSON file", () =>
75 Effect.gen(function*() {
76 const pathService = yield* Path.Path
77 const filePath = pathService.join(
78 import.meta.dirname,
79 "fixtures/patches/valid-add.json"
80 )
81 const result = yield* OpenApiPatch.parsePatchInput(filePath)
82 assert.strictEqual(result.length, 1)
83 assert.strictEqual(result[0].op, "add")
84 assert.strictEqual(result[0].path, "/info/x-custom")
85 }).pipe(Effect.provide(testLayer)))
86​
87 it.effect("parses valid YAML file", () =>
88 Effect.gen(function*() {
89 const pathService = yield* Path.Path
90 const filePath = pathService.join(
91 import.meta.dirname,
92 "fixtures/patches/valid-patch.yaml"
93 )
94 const result = yield* OpenApiPatch.parsePatchInput(filePath)
95 assert.deepStrictEqual(result, [
96 { op: "replace", path: "/info/title", value: "YAML Patched Title" },
97 { op: "add", path: "/info/x-yaml-patch", value: true }
98 ])
99 }).pipe(Effect.provide(testLayer)))
100​
101 it.effect("parses multiple operations from JSON file", () =>
102 Effect.gen(function*() {
103 const pathService = yield* Path.Path
104 const filePath = pathService.join(
105 import.meta.dirname,
106 "fixtures/patches/valid-multiple.json"
107 )
108 const result = yield* OpenApiPatch.parsePatchInput(filePath)
109 assert.strictEqual(result.length, 3)
110 }).pipe(Effect.provide(testLayer)))
111​
112 it.effect("fails on file with unsupported operation", () =>
113 Effect.gen(function*() {
114 const pathService = yield* Path.Path
115 const filePath = pathService.join(
116 import.meta.dirname,
117 "fixtures/patches/invalid-op.json"
118 )
119 const exit = yield* Effect.exit(OpenApiPatch.parsePatchInput(filePath))
120 assert.isTrue(Exit.isFailure(exit))
121 }).pipe(Effect.provide(testLayer)))
122​
123 it.effect("fails on file with missing path", () =>
124 Effect.gen(function*() {
125 const pathService = yield* Path.Path
126 const filePath = pathService.join(
127 import.meta.dirname,
128 "fixtures/patches/missing-path.json"
129 )
130 const exit = yield* Effect.exit(OpenApiPatch.parsePatchInput(filePath))
131 assert.isTrue(Exit.isFailure(exit))
132 }).pipe(Effect.provide(testLayer)))
133​
134 it.effect("falls back to inline JSON when file does not exist", () =>
135 Effect.gen(function*() {
136 const result = yield* OpenApiPatch.parsePatchInput(
137 "[{\"op\":\"add\",\"path\":\"/x\",\"value\":1}]"
138 )
139 assert.deepStrictEqual(result, [{ op: "add", path: "/x", value: 1 }])
140 }).pipe(Effect.provide(testLayer)))
141 })
142 })
143​
144 describe("applyPatches", () => {
145 it.effect("moves a schema to a new name, removing the source (RFC 6902)", () =>
146 Effect.gen(function*() {
147 const document = { components: { schemas: { Skill: { type: "object" } } } }
148 const patch = yield* OpenApiPatch.parsePatchInput(
149 "[{\"op\":\"move\",\"from\":\"/components/schemas/Skill\",\"path\":\"/components/schemas/SkillResource\"}]"
150 )
151 const result = yield* OpenApiPatch.applyPatches([{ source: "inline", patch }], document)
152 assert.deepStrictEqual(result, {
153 components: { schemas: { SkillResource: { type: "object" } } }
154 })
155 }).pipe(Effect.provide(testLayer)))
156​
157 it.effect("treats a move from the root to the root as a no-op (RFC 6902)", () =>
158 Effect.gen(function*() {
159 const patch = yield* OpenApiPatch.parsePatchInput("[{\"op\":\"move\",\"from\":\"\",\"path\":\"\"}]")
160 const result = yield* OpenApiPatch.applyPatches([{ source: "inline", patch }], { a: 1 })
161 assert.deepStrictEqual(result, { a: 1 })
162 }).pipe(Effect.provide(testLayer)))
163​
164 it.effect("applies single add patch", () =>
165 Effect.gen(function*() {
166 const document = { info: { title: "Test" } }
167 const patches = [{
168 source: "test",
169 patch: [{ op: "add" as const, path: "/info/version", value: "1.0.0" }]
170 }]
171 const result = yield* OpenApiPatch.applyPatches(patches, document)
172 assert.deepStrictEqual(result, { info: { title: "Test", version: "1.0.0" } })
173 }))
174​
175 it.effect("applies single remove patch", () =>
176 Effect.gen(function*() {
177 const document = { info: { title: "Test", deprecated: true } }
178 const patches = [{
179 source: "test",
180 patch: [{ op: "remove" as const, path: "/info/deprecated" }]
181 }]
182 const result = yield* OpenApiPatch.applyPatches(patches, document)
183 assert.deepStrictEqual(result, { info: { title: "Test" } })
184 }))
185​
186 it.effect("applies single replace patch", () =>
187 Effect.gen(function*() {
188 const document = { info: { title: "Old Title" } }
189 const patches = [{
190 source: "test",
191 patch: [{ op: "replace" as const, path: "/info/title", value: "New Title" }]
192 }]
193 const result = yield* OpenApiPatch.applyPatches(patches, document)
194 assert.deepStrictEqual(result, { info: { title: "New Title" } })
195 }))
196​
197 it.effect("applies multiple patches in sequence", () =>
198 Effect.gen(function*() {
199 const document = { info: { title: "Original", version: "0.0.1" } }
200 const patches = [
201 {
202 source: "patch1",
203 patch: [{ op: "replace" as const, path: "/info/title", value: "Step 1" }]
204 },
205 {
206 source: "patch2",
207 patch: [{ op: "replace" as const, path: "/info/version", value: "1.0.0" }]
208 },
209 {
210 source: "patch3",
211 patch: [{ op: "add" as const, path: "/info/x-patched", value: true }]
212 }
213 ]
214 const result = yield* OpenApiPatch.applyPatches(patches, document)
215 assert.deepStrictEqual(result, {
216 info: { title: "Step 1", version: "1.0.0", "x-patched": true }
217 })
218 }))
219​
220 it.effect("applies multiple operations within a single patch", () =>
221 Effect.gen(function*() {
222 const document = { info: { title: "Test" }, paths: {} }
223 const patches = [{
224 source: "test",
225 patch: [
226 { op: "replace" as const, path: "/info/title", value: "Updated" },
227 { op: "add" as const, path: "/info/version", value: "2.0.0" }
228 ]
229 }]
230 const result = yield* OpenApiPatch.applyPatches(patches, document)
231 assert.deepStrictEqual(result, {
232 info: { title: "Updated", version: "2.0.0" },
233 paths: {}
234 })
235 }))
236​
237 it.effect("fails when path does not exist for replace", () =>
238 Effect.gen(function*() {
239 const document = { info: { title: "Test" } }
240 const patches = [{
241 source: "test",
242 patch: [{ op: "replace" as const, path: "/info/nonexistent", value: "x" }]
243 }]
244 const exit = yield* Effect.exit(OpenApiPatch.applyPatches(patches, document))
245 assert.isTrue(Exit.isFailure(exit))
246 }))
247​
248 it.effect("fails when path does not exist for remove", () =>
249 Effect.gen(function*() {
250 const document = { info: { title: "Test" } }
251 const patches = [{
252 source: "test",
253 patch: [{ op: "remove" as const, path: "/info/nonexistent" }]
254 }]
255 const exit = yield* Effect.exit(OpenApiPatch.applyPatches(patches, document))
256 assert.isTrue(Exit.isFailure(exit))
257 }))
258​
259 it.effect("accumulates multiple errors", () =>
260 Effect.gen(function*() {
261 const document = { info: { title: "Test" } }
262 const patches = [{
263 source: "test.json",
264 patch: [
265 { op: "replace" as const, path: "/info/nonexistent1", value: "x" },
266 { op: "remove" as const, path: "/info/nonexistent2" },
267 { op: "replace" as const, path: "/info/nonexistent3", value: "y" }
268 ]
269 }]
270 const exit = yield* Effect.exit(OpenApiPatch.applyPatches(patches, document))
271 assert.isTrue(Exit.isFailure(exit))
272 if (Exit.isFailure(exit)) {
273 const failure = exit.cause.reasons[0]
274 if (failure._tag === "Fail") {
275 assert.strictEqual(failure.error._tag, "JsonPatchAggregateError")
276 assert.strictEqual(failure.error.errors.length, 3)
277 assert.include(failure.error.message, "3 patch operations failed")
278 assert.include(failure.error.message, "/info/nonexistent1")
279 assert.include(failure.error.message, "/info/nonexistent2")
280 assert.include(failure.error.message, "/info/nonexistent3")
281 }
282 }
283 }))
284​
285 it.effect("accumulates errors across multiple patches", () =>
286 Effect.gen(function*() {
287 const document = { info: { title: "Test" } }
288 const patches = [
289 {
290 source: "patch1.json",
291 patch: [{ op: "remove" as const, path: "/info/missing1" }]
292 },
293 {
294 source: "patch2.json",
295 patch: [{ op: "remove" as const, path: "/info/missing2" }]
296 }
297 ]
298 const exit = yield* Effect.exit(OpenApiPatch.applyPatches(patches, document))
299 assert.isTrue(Exit.isFailure(exit))
300 if (Exit.isFailure(exit)) {
301 const failure = exit.cause.reasons[0]
302 if (failure._tag === "Fail") {
303 assert.strictEqual(failure.error.errors.length, 2)
304 assert.include(failure.error.message, "patch1.json")
305 assert.include(failure.error.message, "patch2.json")
306 }
307 }
308 }))
309​
310 it.effect("preserves unmodified parts of document", () =>
311 Effect.gen(function*() {
312 const document = {
313 info: { title: "Test", description: "Unchanged" },
314 paths: { "/users": { get: {} } },
315 components: { schemas: {} }
316 }
317 const patches = [{
318 source: "test",
319 patch: [{ op: "replace" as const, path: "/info/title", value: "Changed" }]
320 }]
321 const result = yield* OpenApiPatch.applyPatches(patches, document)
322 assert.strictEqual((result as { info: { description: string } }).info.description, "Unchanged")
323 assert.deepStrictEqual((result as { paths: object }).paths, { "/users": { get: {} } })
324 assert.deepStrictEqual((result as { components: object }).components, { schemas: {} })
325 }))
326​
327 it.effect("returns original document when no patches provided", () =>
328 Effect.gen(function*() {
329 const document = { info: { title: "Test" } }
330 const result = yield* OpenApiPatch.applyPatches([], document)
331 assert.deepStrictEqual(result, document)
332 }))
333 })
334​
335 describe("error messages", () => {
336 it.effect("JsonPatchParseError has descriptive message", () =>
337 Effect.gen(function*() {
338 const error = new OpenApiPatch.JsonPatchParseError({
339 source: "./fix.json",
340 reason: "Unexpected token"
341 })
342 assert.strictEqual(
343 error.message,
344 "Failed to parse patch from ./fix.json: Unexpected token"
345 )
346 }))
347​
348 it.effect("JsonPatchValidationError has descriptive message", () =>
349 Effect.gen(function*() {
350 const error = new OpenApiPatch.JsonPatchValidationError({
351 source: "inline",
352 reason: "Missing 'path' field"
353 })
354 assert.strictEqual(
355 error.message,
356 "Invalid JSON Patch from inline: Missing 'path' field"
357 )
358 }))
359​
360 it.effect("JsonPatchApplicationError has descriptive message", () =>
361 Effect.gen(function*() {
362 const error = new OpenApiPatch.JsonPatchApplicationError({
363 source: "./fix.json",
364 operationIndex: 2,
365 operation: "remove",
366 path: "/info/x",
367 reason: "Property does not exist"
368 })
369 assert.strictEqual(
370 error.message,
371 "Failed to apply patch from ./fix.json: operation 2 (remove at /info/x): Property does not exist"
372 )
373 }))
374​
375 it.effect("JsonPatchAggregateError has descriptive message", () =>
376 Effect.gen(function*() {
377 const error = new OpenApiPatch.JsonPatchAggregateError({
378 errors: [
379 new OpenApiPatch.JsonPatchApplicationError({
380 source: "./fix.json",
381 operationIndex: 0,
382 operation: "replace",
383 path: "/info/x",
384 reason: "Property does not exist"
385 }),
386 new OpenApiPatch.JsonPatchApplicationError({
387 source: "./other.json",
388 operationIndex: 1,
389 operation: "remove",
390 path: "/paths/~1users",
391 reason: "Path not found"
392 })
393 ]
394 })
395 assert.include(error.message, "2 patch operations failed")
396 assert.include(error.message, "1. [./fix.json] op 0 (replace at /info/x)")
397 assert.include(error.message, "2. [./other.json] op 1 (remove at /paths/~1users)")
398 }))
399 })
400})