From 87c566c833bd4b1ed124d48c91e24d4b73c69035 Mon Sep 17 00:00:00 2001 From: Victor Rubezhny Date: Thu, 25 Jun 2026 00:31:36 +0200 Subject: [PATCH] Upgrade js-yaml from 4.x to 5.1.0 Signed-off-by: Victor Rubezhny --- lib/parsers/yaml.ts | 15 +++++++++------ package.json | 3 +-- pnpm-lock.yaml | 18 +++++------------- test/specs/bundle-null-value/bundled.ts | 2 +- test/specs/invalid/invalid.spec.ts | 8 ++++---- test/specs/invalid/invalid.yaml | 2 +- .../nested/working-dir.spec.ts | 10 +++++----- 7 files changed, 26 insertions(+), 32 deletions(-) diff --git a/lib/parsers/yaml.ts b/lib/parsers/yaml.ts index 9a179a29..c98997b5 100644 --- a/lib/parsers/yaml.ts +++ b/lib/parsers/yaml.ts @@ -1,8 +1,6 @@ +import { JSON_SCHEMA, load } from "js-yaml"; +import type { FileInfo, Plugin } from "../types/index.js"; import { ParserError } from "../util/errors.js"; -import yaml from "js-yaml"; -import { JSON_SCHEMA } from "js-yaml"; -import type { FileInfo } from "../types/index.js"; -import type { Plugin } from "../types/index.js"; export default { /** @@ -39,12 +37,17 @@ export default { } if (typeof data === "string") { + // Handle empty strings - js-yaml 5.x throws an error for empty input + if (data.trim() === "") { + return undefined; + } + try { - return yaml.load(data, { schema: JSON_SCHEMA }); + return load(data, { schema: JSON_SCHEMA }); } catch { try { // fallback to non JSON_SCHEMA - return yaml.load(data); + return load(data); } catch (e: any) { throw new ParserError(e?.message || "Parser Error", file.url); } diff --git a/package.json b/package.json index 74625bbf..bef94740 100644 --- a/package.json +++ b/package.json @@ -72,13 +72,12 @@ "@types/json-schema": "^7.0.15" }, "dependencies": { - "js-yaml": "^4.2.0" + "js-yaml": "^5.1.0" }, "devDependencies": { "@eslint/compat": "^2.1.0", "@eslint/js": "^10.0.1", "@types/eslint": "^9.6.1", - "@types/js-yaml": "^4.0.9", "@types/json-schema": "^7.0.15", "@types/node": "^25.9.3", "@typescript-eslint/eslint-plugin": "^8.61.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d17ace42..f039792b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: js-yaml: - specifier: ^4.2.0 - version: 4.2.0 + specifier: ^5.1.0 + version: 5.1.0 devDependencies: '@eslint/compat': specifier: ^2.1.0 @@ -21,9 +21,6 @@ importers: '@types/eslint': specifier: ^9.6.1 version: 9.6.1 - '@types/js-yaml': - specifier: ^4.0.9 - version: 4.0.9 '@types/json-schema': specifier: ^7.0.15 version: 7.0.15 @@ -394,9 +391,6 @@ packages: '@types/estree@1.0.9': resolution: {integrity: sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==} - '@types/js-yaml@4.0.9': - resolution: {integrity: sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg==} - '@types/json-schema@7.0.15': resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==} @@ -1087,8 +1081,8 @@ packages: js-tokens@10.0.0: resolution: {integrity: sha512-lM/UBzQmfJRo9ABXbPWemivdCW8V2G8FHaHdypQaIy523snUjog0W71ayWXTjiR+ixeMyVHN2XcpnTd/liPg/Q==} - js-yaml@4.2.0: - resolution: {integrity: sha512-ePWsvanv0DWuDRsW8dnt+R4jQ31SCRCQ7hhNcPXZPsoBZiemuZNYGf7adZdqX2D86j6rvKp3RpCxVTSb8WQlOw==} + js-yaml@5.1.0: + resolution: {integrity: sha512-s8VA5jkR8f22S3NAXmhKPFqGUduqZGlsufabVOgN14iTdw/RXcym7bKkbwjxLK9Yw2lEvvmJjFp119+KPeo8Kg==} hasBin: true jsdom@29.1.1: @@ -1954,8 +1948,6 @@ snapshots: '@types/estree@1.0.9': {} - '@types/js-yaml@4.0.9': {} - '@types/json-schema@7.0.15': {} '@types/json5@0.0.29': {} @@ -2798,7 +2790,7 @@ snapshots: js-tokens@10.0.0: {} - js-yaml@4.2.0: + js-yaml@5.1.0: dependencies: argparse: 2.0.1 diff --git a/test/specs/bundle-null-value/bundled.ts b/test/specs/bundle-null-value/bundled.ts index 39cc14a7..52496e6e 100644 --- a/test/specs/bundle-null-value/bundled.ts +++ b/test/specs/bundle-null-value/bundled.ts @@ -3,7 +3,7 @@ const bundledSchema = { Pet: { example: { breed: "dog", - name: null, + name: "", }, properties: { breed: { diff --git a/test/specs/invalid/invalid.spec.ts b/test/specs/invalid/invalid.spec.ts index ec64ded6..ea873203 100644 --- a/test/specs/invalid/invalid.spec.ts +++ b/test/specs/invalid/invalid.spec.ts @@ -128,7 +128,7 @@ describe("Invalid syntax", () => { name: ParserError.name, message: (message: any) => message.includes( - "invalid.yaml: incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line (1:1)", + "invalid.yaml: unexpected end of the stream within a flow collection (2:1)", ), path: [], source: (message: any) => message.endsWith("test/specs/invalid/invalid.yaml"), @@ -269,7 +269,7 @@ describe("Invalid syntax", () => { // Because the JSON and YAML parsers were disabled, the invalid YAML file got parsed as plain text expect(schema).to.deep.equal({ - foo: ":\n", + foo: "{ invalid yaml: [\n", }); }); @@ -319,7 +319,7 @@ describe("Invalid syntax", () => { name: ParserError.name, message: (message: any) => message.includes( - "invalid.yaml: incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line (1:1)", + "invalid.yaml: unexpected end of the stream within a flow collection (2:1)", ), path: ["foo"], // source: message => message.endsWith("/test/") || message.startsWith("http://localhost"), @@ -397,7 +397,7 @@ describe("Invalid syntax", () => { { foo: { $ref: path.rel("test/specs/invalid/invalid.yaml") } }, { continueOnError: true, parse: { yaml: false, json: false } }, ); - expect(result).to.deep.equal({ foo: ":\n" }); + expect(result).to.deep.equal({ foo: "{ invalid yaml: [\n" }); }); }); }); diff --git a/test/specs/invalid/invalid.yaml b/test/specs/invalid/invalid.yaml index 397db75f..63aa2a2d 100644 --- a/test/specs/invalid/invalid.yaml +++ b/test/specs/invalid/invalid.yaml @@ -1 +1 @@ -: +{ invalid yaml: [ diff --git a/test/specs/working-directory/nested/working-dir.spec.ts b/test/specs/working-directory/nested/working-dir.spec.ts index e696e785..5039379c 100644 --- a/test/specs/working-directory/nested/working-dir.spec.ts +++ b/test/specs/working-directory/nested/working-dir.spec.ts @@ -1,10 +1,10 @@ +import fsp from "fs/promises"; +import { load } from "js-yaml"; import { describe, expect, it } from "vitest"; import $RefParser from "../../../../lib"; -import path from "../../../utils/path"; -import yaml from "js-yaml"; -import fsp from "fs/promises"; -import helper from "../../../utils/helper"; import { JSONParserError } from "../../../../lib/util/errors"; +import helper from "../../../utils/helper"; +import path from "../../../utils/path"; describe("Working directory", () => { it("should parse using the relative directory of the resolved file", async () => { @@ -16,7 +16,7 @@ describe("Working directory", () => { const parser = new $RefParser(); const schema = path.rel("test/specs/working-directory/api/example.yaml"); const contents = await fsp.readFile(schema, "utf-8"); - const parsed = await yaml.load(contents); + const parsed = await load(contents); try { await parser.dereference(parsed); helper.shouldNotGetCalled();