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

> character is not decoded properly when applying json patches #198

Closed
nvelat opened this issue Jan 16, 2024 · 3 comments · Fixed by #202
Closed

> character is not decoded properly when applying json patches #198

nvelat opened this issue Jan 16, 2024 · 3 comments · Fixed by #202

Comments

@nvelat
Copy link

nvelat commented Jan 16, 2024

The > character is decoded as unicode rather than text after applying a json patch. Here's an example:

package main

import (
	"fmt"

	jsonpatch "github.com/evanphx/json-patch/v5"
)

func main() {
	original := []byte(`{
  "version": "0.1.0",
  "engines": {
    "node": ">= 10.16.3"
  }
}`)
	patchJSON := []byte(`[{"op": "replace", "path": "/version", "value": "0.2.0"}]`)
	patch, err := jsonpatch.DecodePatch(patchJSON)
	if err != nil {
		panic(err)
	}
	modified, err := patch.ApplyIndent(original, "  ")
	if err != nil {
		panic(err)
	}

	fmt.Printf("Original document:\n%s\n", original)
	fmt.Printf("Modified document:\n%s\n", modified)
}

Output:

Original document:
{
  "version": "0.1.0",
  "engines": {
    "node": ">= 10.16.3"
  }
}
Modified document:
{
  "version": "0.2.0",
  "engines": {
    "node": "\u003e= 10.16.3"
  }
}
@evanphx
Copy link
Owner

evanphx commented Jan 16, 2024

The reason for this is that encoding/json escapes for HTML inclusion by default. I'm not sure I want to change that at this stage of the game, it could subtle break anyone that uses the output directly.

@nvelat
Copy link
Author

nvelat commented Jan 16, 2024

Hmm, is it possible to get a new ApplyOption that allows us to SetEscapeHTML(false) on the json encoder? That's how you would normally work around this in the encoding/json library

@evanphx
Copy link
Owner

evanphx commented Jan 28, 2024

5.9.0 includes a new EscapeHTML value on ApplyOptions you can set to control this behavior.

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

Successfully merging a pull request may close this issue.

2 participants