diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5faddfa..839dd2c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,8 +39,8 @@ jobs: for k in totals: totals[k]+=int(r.get(k,'0')) except Exception: pass - exp_tests=4426 - exp_skipped=1715 + exp_tests=4436 + exp_skipped=1692 if totals['tests']!=exp_tests or totals['skipped']!=exp_skipped: print(f"Unexpected test totals: {totals} != expected tests={exp_tests}, skipped={exp_skipped}") sys.exit(1) diff --git a/json-java21/src/main/java/jdk/sandbox/internal/util/json/StableValue.java b/json-java21/src/main/java/jdk/sandbox/internal/util/json/StableValue.java index 19906ec..1fafaa3 100644 --- a/json-java21/src/main/java/jdk/sandbox/internal/util/json/StableValue.java +++ b/json-java21/src/main/java/jdk/sandbox/internal/util/json/StableValue.java @@ -63,6 +63,21 @@ public T get() { } return result; } + + @Override + public String toString() { + return get().toString(); + } + + @Override + public int hashCode() { + return get().hashCode(); + } + + @Override + public boolean equals(Object obj) { + return get().equals(obj); + } }; } } \ No newline at end of file diff --git a/json-java21/src/test/java/jdk/sandbox/java/util/json/EscapedKeyBugTest.java b/json-java21/src/test/java/jdk/sandbox/java/util/json/EscapedKeyBugTest.java new file mode 100644 index 0000000..49dec75 --- /dev/null +++ b/json-java21/src/test/java/jdk/sandbox/java/util/json/EscapedKeyBugTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2025, Simon Massey. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @enablePreview + * @summary Test escaped characters in JSON object keys + * @run junit EscapedKeyBugTest + */ + +package jdk.sandbox.java.util.json; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.util.logging.Logger; + +public class EscapedKeyBugTest { + private static final Logger LOGGER = Logger.getLogger(EscapedKeyBugTest.class.getName()); + + @Test + public void testEscapedCharactersInKeys() { + LOGGER.info("Executing testEscapedCharactersInKeys"); + // Should parse successfully with escaped characters in keys + String json = """ + {"foo\\nbar":1,"foo\\tbar":2} + """; + + JsonValue result = Json.parse(json); + JsonObject obj = (JsonObject) result; + + // Verify both keys are parsed correctly + assertEquals(1L, ((JsonNumber) obj.members().get("foo\nbar")).toNumber()); + assertEquals(2L, ((JsonNumber) obj.members().get("foo\tbar")).toNumber()); + } + + @Test + public void testEscapedQuoteInKey() { + LOGGER.info("Executing testEscapedQuoteInKey"); + // Test escaped quotes in keys + String json = """ + {"foo\\"bar":1} + """; + + JsonValue result = Json.parse(json); + JsonObject obj = (JsonObject) result; + + // Verify key with escaped quote is parsed correctly + assertEquals(1L, ((JsonNumber) obj.members().get("foo\"bar")).toNumber()); + } + + @Test + public void testEscapedBackslashInKey() { + LOGGER.info("Executing testEscapedBackslashInKey"); + // Test escaped backslashes in keys + String json = """ + {"foo\\\\bar":1} + """; + + JsonValue result = Json.parse(json); + JsonObject obj = (JsonObject) result; + + // Verify key with escaped backslash is parsed correctly + assertEquals(1L, ((JsonNumber) obj.members().get("foo\\bar")).toNumber()); + } + + @Test + public void testMultipleEscapedCharactersInKey() { + LOGGER.info("Executing testMultipleEscapedCharactersInKey"); + // Test multiple escaped characters in one key + String json = """ + {"foo\\n\\t\\"bar":1} + """; + + JsonValue result = Json.parse(json); + JsonObject obj = (JsonObject) result; + + // Verify key with multiple escaped characters is parsed correctly + assertEquals(1L, ((JsonNumber) obj.members().get("foo\n\t\"bar")).toNumber()); + } +}