Skip to content

Commit

Permalink
Disable HTMLEscape in reflect JSON encoder (#704)
Browse files Browse the repository at this point in the history
Fixes #700.

Zap's custom JSON encoder does not escape HTML, so make encoding
by the reflect-based JSON encoder work the same way.
  • Loading branch information
prashantv authored Apr 29, 2019
1 parent badef73 commit 1c5a13c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions zapcore/json_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (enc *jsonEncoder) resetReflectBuf() {
if enc.reflectBuf == nil {
enc.reflectBuf = bufferpool.Get()
enc.reflectEnc = json.NewEncoder(enc.reflectBuf)

// For consistency with our custom JSON encoder.
enc.reflectEnc.SetEscapeHTML(false)
} else {
enc.reflectBuf.Reset()
}
Expand Down
4 changes: 2 additions & 2 deletions zapcore/json_encoder_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ func TestJSONEncoderObjectFields(t *testing.T) {
},
{
desc: "reflect (success)",
expected: `"k":{"loggable":"yes"}`,
expected: `"k":{"escape":"<&>","loggable":"yes"}`,
f: func(e Encoder) {
assert.NoError(t, e.AddReflected("k", map[string]string{"loggable": "yes"}), "Unexpected error JSON-serializing a map.")
assert.NoError(t, e.AddReflected("k", map[string]string{"escape": "<&>", "loggable": "yes"}), "Unexpected error JSON-serializing a map.")
},
},
{
Expand Down

2 comments on commit 1c5a13c

@TimmyWangDouDou
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root@MPSYUN-W1-000057:/usr/mpsyun# go get -u go.uber.org/zap

go.uber.org/zap/zapcore

/root/src/go.uber.org/zap/zapcore/json_encoder.go:142: enc.reflectEnc.SetEscapeHTML undefined (type *json.Encoder has no field or method SetEscapeHTML)

it happened,after your commit.How to solve this?

@prashantv
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now call json.SetEscapeHTML which was introduced in Go 1.7.

Please upgrade to a newer version of Go (the latest release is 1.12).

Note that zap only supports the two most recent minor versions of Go.

Please sign in to comment.