From 2691d8a8976a3c3fd2a242eabf86539ed9161acd Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 18 Sep 2018 16:21:05 +0800 Subject: [PATCH 1/2] Add a new jsonMarsal method This tries to fix the issue that the angle brackets "<" and ">" are escaped to "\u003c" and "\u003e" with json.Marshal. --- provider_client.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/provider_client.go b/provider_client.go index bba8c89c8..6b0216e78 100644 --- a/provider_client.go +++ b/provider_client.go @@ -159,6 +159,14 @@ type RequestOpts struct { var applicationJSON = "application/json" +func jsonMarshal(t interface{}) ([]byte, error) { + buffer := &bytes.Buffer{} + enc := json.NewEncoder(buffer) + enc.SetEscapeHTML(false) + err := enc.Encode(t) + return buffer.Bytes(), err +} + // Request performs an HTTP request using the ProviderClient's current HTTPClient. An authentication // header will automatically be provided. func (client *ProviderClient) Request(method, url string, options *RequestOpts) (*http.Response, error) { @@ -172,7 +180,7 @@ func (client *ProviderClient) Request(method, url string, options *RequestOpts) panic("Please provide only one of JSONBody or RawBody to golangsdk.Request().") } - rendered, err := json.Marshal(options.JSONBody) + rendered, err := jsonMarshal(options.JSONBody) if err != nil { return nil, err } From 95054321496dfa9b1dba9b8cc2380779f8174795 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 18 Sep 2018 17:33:13 +0800 Subject: [PATCH 2/2] Fix test issue --- .../extensions/volumeactions/testing/fixtures.go | 6 +++++- results.go | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/openstack/blockstorage/extensions/volumeactions/testing/fixtures.go b/openstack/blockstorage/extensions/volumeactions/testing/fixtures.go index c81d6ffd6..d22e7da71 100644 --- a/openstack/blockstorage/extensions/volumeactions/testing/fixtures.go +++ b/openstack/blockstorage/extensions/volumeactions/testing/fixtures.go @@ -281,7 +281,11 @@ func MockForceDeleteResponse(t *testing.T) { th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22/action", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "POST") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) - th.TestBody(t, r, `{"os-force_delete":""}`) + th.TestJSONRequest(t, r, ` +{ + "os-force_delete": "" +} + `) w.WriteHeader(http.StatusAccepted) }) } diff --git a/results.go b/results.go index 1c4b9cc64..07b1cc19d 100644 --- a/results.go +++ b/results.go @@ -53,7 +53,7 @@ func (r Result) ExtractInto(to interface{}) error { return json.NewDecoder(reader).Decode(to) } - b, err := json.Marshal(r.Body) + b, err := jsonMarshal(r.Body) if err != nil { return err } @@ -73,7 +73,7 @@ func (r Result) extractIntoPtr(to interface{}, label string) error { return err } - b, err := json.Marshal(m[label]) + b, err := jsonMarshal(m[label]) if err != nil { return err } @@ -101,7 +101,7 @@ func (r Result) extractIntoPtr(to interface{}, label string) error { // https://play.golang.org/p/NHo3ywlPZli newType := reflect.New(typeOfV).Elem() - b, err := json.Marshal(v) + b, err := jsonMarshal(v) if err != nil { return err } @@ -260,7 +260,7 @@ func (r HeaderResult) ExtractInto(to interface{}) error { } } - b, err := json.Marshal(tmpHeaderMap) + b, err := jsonMarshal(tmpHeaderMap) if err != nil { return err }