Skip to content

Commit

Permalink
Error when JSON/YAML parsing would set property default in a Dynamic
Browse files Browse the repository at this point in the history
Resolves apple#561
  • Loading branch information
HT154 committed Oct 24, 2024
1 parent 86d870b commit e8ee462
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public void endObject(@Nullable EconomicMap<Object, ObjectMember> members) {

@Override
public void startObjectValue(@Nullable EconomicMap<Object, ObjectMember> members, String name) {
if (!useMapping && "default".equals(name)) {
// https://github.com/apple/pkl/issues/561
throw new ParseException(
"Cannot parse object key `default` into a `Dynamic` value", getLocation());
}
currPath.push(Identifier.get(name));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,12 @@ private void addMembers(MappingNode node, VmObject object) {
var memberName =
convertedKey instanceof String string && !useMapping ? Identifier.get(string) : null;

// https://github.com/apple/pkl/issues/561
if (memberName != null && "default".equals(convertedKey)) {
throw new YamlEngineException(
"Cannot parse object key `default` into a Dynamic value. Node: " + node);
}

var member =
new ObjectMember(
sourceSection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ res16 = parser.parse("""

// invalid syntax
res17 = test.catch(() -> parser.parse("!@#$%"))

res18 = test.catch(() -> parser.parse("""
{"default": null}
"""))

res19 = (parser) { useMapping = true }.parse("""
{"default": null}
""")
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,11 @@ res15 = parser.parseAll("""
hobby: surfing
...
""")

res16 = test.catch(() -> parser.parse("""
{"default": null}
"""))

res17 = (parser) { useMapping = true }.parse("""
default: null
""")
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ res16 {
}
}
res17 = "Error parsing JSON document."
res18 = "Error parsing JSON document."
res19 {
["default"] = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ res15 = List(new {
}, new {
hobby = "surfing"
})
res16 = "Error parsing YAML document."
res17 {
["default"] = null
}

0 comments on commit e8ee462

Please sign in to comment.