From 23e55f72cb777ece66291b4773e427bafdcfa7e5 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 28 Jun 2024 15:05:37 +0900 Subject: [PATCH] fix(yaml): fix merge (<<) type handling in `parse()` (#5185) --- yaml/_loader/loader.ts | 2 +- yaml/parse_test.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/yaml/_loader/loader.ts b/yaml/_loader/loader.ts index c7260698c5ec..f0ffbd394d9c 100644 --- a/yaml/_loader/loader.ts +++ b/yaml/_loader/loader.ts @@ -300,7 +300,7 @@ function mergeMappings( ); } - for (const key in Object.keys(source)) { + for (const key of Object.keys(source)) { if (!hasOwn(destination, key)) { Object.defineProperty(destination, key, { value: source[key], diff --git a/yaml/parse_test.ts b/yaml/parse_test.ts index adba7bd72688..33ce1e8ff676 100644 --- a/yaml/parse_test.ts +++ b/yaml/parse_test.ts @@ -448,3 +448,20 @@ Deno.test("parse() throws with invalid strings", () => { 'expected valid JSON character at line 1, column 3:\n "\b"\n ^', ); }); + +Deno.test("parse() handles merge (<<) types", () => { + assertEquals( + parse(`<<: { a: 1, b: 2 } +c: 3`), + { a: 1, b: 2, c: 3 }, + ); + + assertThrows( + () => + // number can't be used as merge value + parse(`<<: 1 +c: 3`), + YamlError, + "cannot merge mappings; the provided source object is unacceptable at line 1, column 6:\n <<: 1\n ^", + ); +});