Skip to content

Commit

Permalink
Hide EscapeHTML behind an option instead of default behaviour. (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus authored Aug 17, 2021
1 parent 816a0f6 commit f01913a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
23 changes: 18 additions & 5 deletions ygot/struct_validation_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ type EmitJSONConfig struct {
// Indent is the string used for indentation within the JSON output. The
// default value is three spaces.
Indent string
// EscapeHTML determines whether certain characters will be escaped
// in the marshalled JSON for safety in HTML embedding. See
// https://pkg.go.dev/encoding/json#Encoder.SetEscapeHTML.
EscapeHTML bool
// SkipValidation specifies whether the GoStruct supplied to EmitJSON should
// be validated before emitting its content. Validation is skipped when it
// is set to true.
Expand Down Expand Up @@ -418,17 +422,26 @@ func EmitJSON(s ValidatedGoStruct, opts *EmitJSONConfig) (string, error) {
return "", err
}

sb := &strings.Builder{}
enc := json.NewEncoder(sb)
indent := indentString
if opts != nil && opts.Indent != "" {
indent = opts.Indent
enc.SetEscapeHTML(false)
if opts != nil {
enc.SetEscapeHTML(opts.EscapeHTML)

if opts.Indent != "" {
indent = opts.Indent
}
}
enc.SetIndent("", indent)

j, err := json.MarshalIndent(v, "", indent)
if err != nil {
if err := enc.Encode(v); err != nil {
return "", fmt.Errorf("JSON marshalling error: %v", err)
}

return string(j), nil
// Exclude the last newline character:
// https://pkg.go.dev/encoding/json#Encoder.Encode
return sb.String()[:sb.Len()-1], nil
}

// makeJSON renders the GoStruct s to map[string]interface{} according to the
Expand Down
14 changes: 13 additions & 1 deletion ygot/struct_validation_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,23 @@ func TestEmitJSON(t *testing.T) {
name: "simple schema JSON output",
inStruct: &mapStructTestOne{
Child: &mapStructTestOneChild{
FieldOne: String("hello"),
FieldOne: String("abc -> def"),
FieldTwo: Uint32(42),
},
},
wantJSONPath: filepath.Join(TestRoot, "testdata/emitjson_1.json-txt"),
}, {
name: "simple schema JSON output with safe HTML",
inStruct: &mapStructTestOne{
Child: &mapStructTestOneChild{
FieldOne: String("abc -> def"),
FieldTwo: Uint32(42),
},
},
inConfig: &EmitJSONConfig{
EscapeHTML: true,
},
wantJSONPath: filepath.Join(TestRoot, "testdata/emitjson_1_html_safe.json-txt"),
}, {
name: "schema with a list JSON output",
inStruct: &mapStructTestFour{
Expand Down
2 changes: 1 addition & 1 deletion ygot/testdata/emitjson_1.json-txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"child": {
"config": {
"field-one": "hello",
"field-one": "abc -> def",
"field-two": 42
}
}
Expand Down
8 changes: 8 additions & 0 deletions ygot/testdata/emitjson_1_html_safe.json-txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"child": {
"config": {
"field-one": "abc -\u003e def",
"field-two": 42
}
}
}

0 comments on commit f01913a

Please sign in to comment.