-
Notifications
You must be signed in to change notification settings - Fork 18k
text/template: JSEscape generates invalid JSON #37634
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
Comments
However
|
|
We were just lucky not to hit those characters in our use case which is writing a JSON document with values that are PEM-formatted certificates ( |
Interesting issue. I confirm this is intended behavior as JSEscape was only designed to be used to escape JS and not JSON. The fact that you were lucky not to hit escaping with b64 was just a coincidence and was not intended. That said I undestand your pain and I would like to ask for a little bit more information before I try to address this:
|
Change https://golang.org/cl/226097 mentions this issue: |
Note that JSEscape still generates invalid JSON:
I'm guessing maybe @sding3 is wanting to template JSON for the same reason I do, for generating Slack messages? |
@lpar , we were using it for escaping PEM encoded certificates. We ended up writing up our own custom template function to do JSON escapae. Sounds like the standard library could benefit from a |
Huh, CL 226097 looks like it's replacing |
I have a couple of questions: if you are templating JSON, why not use And if we find a legit case for supporting this: do we want to work on making sure that JSEscape will always yield valid JSON strings or do we prefer to add a new API to perform |
In our case, the idea is to let users build messages using Slack block kit and then insert placeholders for the data in the JSON generated by Slack. Building objects from text/template generated data and then using encoding/json to serialize would basically involve duplicating the block kit API. |
So I guess parsing the JSON generated by Slack into a map and then replacing the placeholders would not work for you? How do you make sure users don't corrupt the JSON data structure when changing your template? (While we discuss this I'll take a look into extending the change in https://golang.org/cl/226097 to also cover all the JS escaping functions so that we only use the JSON subset when emitting escaped data. This might take a while) |
The Kubernetes ecosystem is place where go templating seems to come up a lot, often with the intent of producing YAML or JSON (or sometimes even YAML/JSON inside of YAML 😱). AFAICT, templated values are often incorrectly wrapped in quotation marks, which works until it doesn't. For example, see the sealed-secrets documentation has this example: ArgoCD (another tool in the Kubernetes ecosystem) provides Sprig, which has a |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Ran this code:
What did you expect to see?
Expected to see
"="
, which is the behavior ingo1.13.8
What did you see instead?
Insead see
"\x3D"
.The text was updated successfully, but these errors were encountered: