Skip to content

Commit

Permalink
[release-17.0] json: Fix quoting JSON keys (#14066) (#14068)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Florent Poinsard <florent.poinsard@outlook.fr>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Florent Poinsard <florent.poinsard@outlook.fr>
  • Loading branch information
vitess-bot[bot] and frouioui authored Sep 22, 2023
1 parent 3b20a40 commit 7a916f0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions go/mysql/json/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (v *Value) marshalSQLInternal(top bool, dst []byte) []byte {
if i != 0 {
dst = append(dst, ", "...)
}
dst = append(dst, "_utf8mb4'"...)
dst = append(dst, vv.k...)
dst = append(dst, "', "...)
dst = append(dst, "_utf8mb4"...)
dst = append(dst, sqltypes.EncodeStringSQL(vv.k)...)
dst = append(dst, ", "...)
dst = vv.v.marshalSQLInternal(false, dst)
}
dst = append(dst, ')')
Expand Down
57 changes: 57 additions & 0 deletions go/mysql/json/marshal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2023 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package json

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestMarshalSQLTo(t *testing.T) {
testcases := []struct {
input string
expected string
}{
{
input: "null",
expected: "CAST(_utf8mb4'null' as JSON)",
},
{
input: `{}`,
expected: `JSON_OBJECT()`,
},
{
input: `{"a": 1}`,
expected: `JSON_OBJECT(_utf8mb4'a', 1)`,
},
{
input: `{"key with ' in it": []}`,
expected: `JSON_OBJECT(_utf8mb4'key with \' in it', JSON_ARRAY())`,
},
}
for _, tc := range testcases {
t.Run(tc.input, func(t *testing.T) {
var p Parser

v, err := p.Parse(tc.input)
require.NoError(t, err)
buf := v.MarshalSQLTo(nil)
require.Equal(t, tc.expected, string(buf))
})
}
}

0 comments on commit 7a916f0

Please sign in to comment.