Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshalling variables causes some characters to be unicode escaped #671

Open
bramca opened this issue May 23, 2023 · 0 comments
Open

Marshalling variables causes some characters to be unicode escaped #671

bramca opened this issue May 23, 2023 · 0 comments

Comments

@bramca
Copy link
Contributor

bramca commented May 23, 2023

example script:

- name: parse_network_tag_data
  steps:
    - script: echo '{{.tag_data}}' | jq '.[].id' | perl -pe 'chomp;s/"(.+?)"/&id=\1/g;' | sed 's/^&//'

This results in the following variables:

{ 
  "step": {
    "script": "echo '[{\"createdAt\":\"2023-05-22T14:35:24.367570311+02:00\",\"id\":\"1d423a9c-2a6a-4bc9-afcf-b53f19403c9b\",\"key\":\"Test-Tag-0\",\"updatedAt\":\"2023-05-22T14:35:24.367570311+02:00\",\"value\":\"Test-Value-0\"}, {\"createdAt\":\"2023-05-22T14:35:24.367570311+02:00\",\"id\":\"e18e6f91-cd75-4bb9-8733-65eea005f11f\",\"key\":\"Test-Tag-1\",\"updatedAt\":\"2023-05-22T14:35:24.367570311+02:00\",\"value\":\"Test-Value-1\"}]' | jq '.[].id' | perl -pe 'chomp;s/\"(.+?)\"/\u0026id=\\1/g;' | sed 's/^\u0026//'"
   },
   "result": {
    "systemout": "id=1d423a9c-2a6a-4bc9-afcf-b53f19403c9b\u0026id=e18e6f91-cd75-4bb9-8733-65eea005f11f",
    "code": "0",
    "timeseconds": 0.154433031
   }
}

The '&' character is unicode escaped by '\u0026' in the result. This is caused by the json.Marshal (https://cs.opensource.google/go/go/+/master:src/encoding/json/encode.go;l=161)

This breaks following test steps that need this '&' as the result is used in a URL query.

Suggestion:
The Marshalling of variables into json could be done another way in Go to fix this escaping.

func JSONMarshal(t interface{}) ([]byte, error) {
    buffer := &bytes.Buffer{}
    encoder := json.NewEncoder(buffer)
    encoder.SetEscapeHTML(false)
    err := encoder.Encode(t)
    return buffer.Bytes(), err
}

Let me know if you think this is a good fix? Or should I consider passing the variable another way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant