jsonnet
jsonnet copied to clipboard
`std.parseYaml()` - new test case fails...
std.parseYaml() exists in the cpp version in the master branch since commit da1490f6. It was introduced by the merge of #899 and the main issue about its introduction is (I think) #460. A release hasn't been made with it yet.
std.parseYaml() chokes on this (I think) valid YAML,:
f1: |
a
b
f2: "a\nb"
Running it through std.parseYaml() gives an error:
Something went wrong during jsonnet_evaluate_snippet, please report this: [json.exception.parse_error.101] parse error at line 2, column 0: syntax error while parsing value - invalid string: control character U+000A (LF) must be escaped to \u000A or \n; last read: '"a<U+000A>'
[1] 91637 abort ./jsonnet parseYaml.jsonnet
Where it should be fine and equal { f1: 'a\nb', f2: 'a\nb' }.
This is a patch for the test_suite to test for it. (Easy to add as a PR - let me know if you want a PR for this...)
diff --git a/test_suite/stdlib.jsonnet b/test_suite/stdlib.jsonnet
index 669c1f9..257c9d5 100644
--- a/test_suite/stdlib.jsonnet
+++ b/test_suite/stdlib.jsonnet
@@ -1467,6 +1467,16 @@ std.assertEqual(
|||
), [1, 2, 3]
) &&
+std.assertEqual(
+ std.parseYaml(
+ |||
+ f1: |
+ a
+ b
+ f2: "a\nb"
+ |||
+ ), { f1: 'a\nb', f2: 'a\nb' }
+) &&
std.assertEqual(std.asciiUpper('!@#$%&*()asdfghFGHJKL09876 '), '!@#$%&*()ASDFGHFGHJKL09876 ') &&
std.assertEqual(std.asciiLower('!@#$%&*()asdfghFGHJKL09876 '), '!@#$%&*()asdfghfghjkl09876 ') &&
Thanks for reporting.
Yeah, if you could raise a PR that would be very convenient.
FWIW go-jsonnet seems to work fine on this example:
❯ cat junk/test.jsonnet
local yaml = |||
f1: |
a
b
f2: "a\nb"
|||;
std.parseYaml(yaml)
❯ ./jsonnet junk/test.jsonnet
{
"f1": "a\nb\n",
"f2": "a\nb"
}
FYI: The test case in the PR is different from the one above, because f1 actually equals a\nb\n.
I assume this is a bug in the underlying yaml parsing library?
I.e., rapidyaml does not support text blocks?
I am not sure whether this deserves a separate issue. String values parseable as integers, floats, booleans, null are wrongly converted to those types, e.g.:
/tmp/jsonnet-0.18.0 $ ./jsonnet -e 'std.parseYaml("[\" null\", \"null\", \" 1\", \"1\", \" 1.1\", \"1.1\", \" false\", \"false\"]")'
[
" null",
null,
" 1",
1,
" 1.1",
1.1000000000000001,
" false",
false
]
This issue appears to be caused by the old version (v0.1.0, released of Nov 03, 2020) of rapidyaml. Version of jsonnet which gets installed via Gentoo Linux package manager (which takes a usual stance of unbundling everything possible) built with recent rapidyaml v0.4.1 correctly parses all of those strings as strings.
I'm upgrading Rapid YAML in #1134 - that should fix this case. Then the contributed test case #943 should pass and be mergeable.
The bundled RapidYAML has been upgraded, and your test case is now passing and has been merged. Thanks!