From 83aa83eaeabce38f375b6dc66fecdd32d3680fea Mon Sep 17 00:00:00 2001 From: Sharon Nam Date: Wed, 21 Jun 2023 10:34:01 -0700 Subject: [PATCH] Replace NormalizeJsonString with local helper func --- internal/service/events/rule.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/internal/service/events/rule.go b/internal/service/events/rule.go index 6a6c4370662..df25ef6b686 100644 --- a/internal/service/events/rule.go +++ b/internal/service/events/rule.go @@ -1,7 +1,9 @@ package events import ( + "bytes" "context" + "encoding/json" "fmt" "log" "time" @@ -65,7 +67,7 @@ func ResourceRule() *schema.Resource { ValidateFunc: validateEventPatternValue(), AtLeastOneOf: []string{"schedule_expression", "event_pattern"}, StateFunc: func(v interface{}) string { - json, _ := structure.NormalizeJsonString(v.(string)) + json, _ := jsonMarshal(v.(string)) return json }, }, @@ -109,6 +111,30 @@ func ResourceRule() *schema.Resource { } } +func jsonMarshal(jsonString interface{}) (string, error) { + var j interface{} + + if jsonString == nil || jsonString.(string) == "" { + return "", nil + } + + s := jsonString.(string) + + err := json.Unmarshal([]byte(s), &j) + if err != nil { + return s, err + } + + b, _ := json.Marshal(j) + + if bytes.Contains(b, []byte("\\u003c")) || bytes.Contains(b, []byte("\\u003e")) || bytes.Contains(b, []byte("\\u0026")) { + b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1) + b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1) + b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1) + } + return string(b[:]), nil +} + func resourceRuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EventsConn(ctx)