fix(docparser): keep JSON numbers and <, >, & as written in JSON uploads (#4436)

jsonToMarkdown decoded the upload with json.Unmarshal, which turns every
number into a float64. An id above 2^53 therefore changed digits:
12345678901234567890 came out as 12345678901234567000, and
9007199254740993 as 9007199254740992. 1.50 became 1.5 and 1e5 became
100000. The decoder now uses UseNumber, and each number is written back
exactly as it was in the file.

The chunks were encoded with json.Marshal, which escapes <, > and & for
embedding in HTML, so "a < b && c > d" was indexed as
"a \u003c b \u0026\u0026 c \u003e d". They are now encoded with HTML
escaping off, in the size measurement as well, so a chunk is measured as
it is written.
main
Lukas 2026-10-17 16:31:39 +02:00 committed by GitHub
parent 1fc00cdc08
commit e2a58ea3cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 70 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package docparser
import (
"bytes"
"encoding/json"
"errors"
"fmt"
@ -52,8 +53,14 @@ func jsonToMarkdown(data []byte) (string, error) {
}
data = trimBOM(data)
// UseNumber keeps each number as written. Decoded into a float64, an id
// above 2^53 changed digits: 12345678901234567890 came out as
// 12345678901234567000. ValidateJSONContent above has already rejected
// anything after the value.
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()
var parsed interface{}
if err := json.Unmarshal(data, &parsed); err != nil {
if err := decoder.Decode(&parsed); err != nil {
return "", fmt.Errorf("failed to parse JSON: %w", err)
}
@ -206,7 +213,7 @@ func listToDictPreprocess(data interface{}) interface{} {
// jsonSize returns the serialized JSON byte length of a value.
func jsonSize(v interface{}) int {
b, err := json.Marshal(v)
b, err := marshalJSON(v, "")
if err != nil {
return 0
}
@ -215,13 +222,28 @@ func jsonSize(v interface{}) int {
// formatValue formats a JSON value with indentation.
func formatValue(v interface{}) string {
b, err := json.MarshalIndent(v, "", " ")
b, err := marshalJSON(v, " ")
if err != nil {
b, _ = json.Marshal(v)
b, _ = marshalJSON(v, "")
}
return string(b)
}
// marshalJSON encodes v like json.Marshal, indented when indent is set, but
// leaves <, > and & as they are. json.Marshal writes them as \u003c, \u003e
// and \u0026, which is meant for embedding in HTML and only garbles the text
// a chunk is read and searched as.
func marshalJSON(v interface{}, indent string) ([]byte, error) {
var buf bytes.Buffer
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", indent)
if err := encoder.Encode(v); err != nil {
return nil, err
}
return bytes.TrimSuffix(buf.Bytes(), []byte("\n")), nil
}
// wrapCodeBlock wraps content in a fenced JSON code block.
func wrapCodeBlock(content string) string {
return "```json\n" + content + "\n```"

View File

@ -386,6 +386,50 @@ func TestJsonToMarkdown_SpecialCharsInStrings(t *testing.T) {
})
}
func TestJsonToMarkdown_KeepsNumbersAsWritten(t *testing.T) {
// Decoded into a float64, an id above 2^53 changed digits.
input := `{"order_id": 12345678901234567890, "big": 9007199254740993, "price": 1.50, "ratio": 1e5}`
result, err := jsonToMarkdown([]byte(input))
visualReport(t, "Numbers keep the digits they were written with", input, result, err, []checkResult{
check("no error", err == nil),
check("all blocks are valid JSON", allBlocksValidJSON(result)),
check("order_id keeps all 20 digits", strings.Contains(result, `"order_id": 12345678901234567890`)),
check("2^53+1 is not rounded to 2^53", strings.Contains(result, `"big": 9007199254740993`)),
check("1.50 keeps its trailing zero", strings.Contains(result, `"price": 1.50`)),
check("1e5 keeps its exponent", strings.Contains(result, `"ratio": 1e5`)),
})
}
func TestJsonToMarkdown_KeepsNumbersAsWrittenAcrossChunks(t *testing.T) {
// Large enough to take the recursive split path.
items := make([]string, 0, 60)
for i := 0; i < 60; i++ {
item := fmt.Sprintf(`"item_%02d": {"id": 1234567890123456789%d, "label": "entry %d"}`, i, i%10, i)
items = append(items, item)
}
input := "{" + strings.Join(items, ", ") + "}"
result, err := jsonToMarkdown([]byte(input))
visualReport(t, "Numbers keep their digits in a split document", input, result, err, []checkResult{
check("no error", err == nil),
check("split into several blocks", len(splitBlocks(result)) > 1),
check("first id kept", strings.Contains(result, `"id": 12345678901234567890`)),
check("last id kept", strings.Contains(result, `"id": 12345678901234567899`)),
})
}
func TestJsonToMarkdown_KeepsAngleBracketsAndAmpersands(t *testing.T) {
// json.Marshal writes <, > and & as \u003c, \u003e and \u0026.
input := `{"note": "a < b && c > d", "html": "<b>bold</b>"}`
result, err := jsonToMarkdown([]byte(input))
visualReport(t, "<, > and & stay readable", input, result, err, []checkResult{
check("no error", err == nil),
check("all blocks are valid JSON", allBlocksValidJSON(result)),
check("comparison text kept", strings.Contains(result, `"note": "a < b && c > d"`)),
check("markup kept", strings.Contains(result, `"html": "<b>bold</b>"`)),
check("no \\u003c escapes", !strings.Contains(result, `\u003c`)),
})
}
func TestJsonToMarkdown_NullValues(t *testing.T) {
input := `{"name": "test", "value": null, "list": [null, 1, null]}`
result, err := jsonToMarkdown([]byte(input))