From 2fd69cb0a542e0d88918e7862a46d19bc5c56d21 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 2 Sep 2016 09:31:47 -0700 Subject: [PATCH] json: interpolations have to be escaped At some point we ignored the " in interpolations. We do this for HCL and it is correct but this is invalid JSON syntax and for JSON we've always had the stance that we have to escape them. --- decoder_test.go | 8 ++++++++ json/parser/parser_test.go | 8 ++++++++ json/parser/test-fixtures/bad_input_tf_8110.json | 7 +++++++ json/parser/test-fixtures/good_input_tf_8110.json | 7 +++++++ json/scanner/scanner.go | 2 +- json/scanner/scanner_test.go | 1 - test-fixtures/interpolate.json | 3 +++ 7 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 json/parser/test-fixtures/bad_input_tf_8110.json create mode 100644 json/parser/test-fixtures/good_input_tf_8110.json create mode 100644 test-fixtures/interpolate.json diff --git a/decoder_test.go b/decoder_test.go index b423033..ef39f6b 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -342,6 +342,14 @@ func TestDecode_interface(t *testing.T) { }, }, }, + + { + "interpolate.json", + false, + map[string]interface{}{ + "default": `${replace("europe-west", "-", " ")}`, + }, + }, } for _, tc := range cases { diff --git a/json/parser/parser_test.go b/json/parser/parser_test.go index 16593f2..48bba84 100644 --- a/json/parser/parser_test.go +++ b/json/parser/parser_test.go @@ -315,6 +315,14 @@ func TestParse(t *testing.T) { "bad_input_128.json", true, }, + { + "bad_input_tf_8110.json", + true, + }, + { + "good_input_tf_8110.json", + false, + }, } const fixtureDir = "./test-fixtures" diff --git a/json/parser/test-fixtures/bad_input_tf_8110.json b/json/parser/test-fixtures/bad_input_tf_8110.json new file mode 100644 index 0000000..a043858 --- /dev/null +++ b/json/parser/test-fixtures/bad_input_tf_8110.json @@ -0,0 +1,7 @@ +{ + "variable": { + "poc": { + "default": "${replace("europe-west", "-", " ")}" + } + } +} diff --git a/json/parser/test-fixtures/good_input_tf_8110.json b/json/parser/test-fixtures/good_input_tf_8110.json new file mode 100644 index 0000000..f21aa09 --- /dev/null +++ b/json/parser/test-fixtures/good_input_tf_8110.json @@ -0,0 +1,7 @@ +{ + "variable": { + "poc": { + "default": "${replace(\"europe-west\", \"-\", \" \")}" + } + } +} diff --git a/json/scanner/scanner.go b/json/scanner/scanner.go index 477f71f..dd5c72b 100644 --- a/json/scanner/scanner.go +++ b/json/scanner/scanner.go @@ -296,7 +296,7 @@ func (s *Scanner) scanString() { return } - if ch == '"' && braces == 0 { + if ch == '"' { break } diff --git a/json/scanner/scanner_test.go b/json/scanner/scanner_test.go index fe2d755..3033a57 100644 --- a/json/scanner/scanner_test.go +++ b/json/scanner/scanner_test.go @@ -32,7 +32,6 @@ var tokenLists = map[string][]tokenPair{ {token.STRING, `" "`}, {token.STRING, `"a"`}, {token.STRING, `"本"`}, - {token.STRING, `"${file("foo")}"`}, {token.STRING, `"${file(\"foo\")}"`}, {token.STRING, `"\a"`}, {token.STRING, `"\b"`}, diff --git a/test-fixtures/interpolate.json b/test-fixtures/interpolate.json new file mode 100644 index 0000000..cad0151 --- /dev/null +++ b/test-fixtures/interpolate.json @@ -0,0 +1,3 @@ +{ + "default": "${replace(\"europe-west\", \"-\", \" \")}" +}