Skip to content

types.Query truncates int64 during marshaling #1021

@MaratBR

Description

@MaratBR

types.Query truncates int64 in its custom MarshalJSON implementation.

Probably happens because here you marshal Query into JSON and then turn it into map[string]any. json package probably convert int64 into float and this causes loss of precision.

As a result this query fails for me because tags value changes.

{
    "from": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "tags": [
                            4475034517144338534
                        ]
                    }
                }
            ]
        }
    },
    "size": 20
}

4475034517144338534 gets truncated to 4475034517144338400

demo of the issue The following is analogous to what happens in `MarshalJSON` of `types.Query`
package main

import "encoding/json"

type Issue1021 struct {
	Value int64
}

func main() {
	v := Issue1021{
		Value: 4475034517144338534,
	}
	jsonV, _ := json.Marshal(v)
	println(string(jsonV))
	var tmp map[string]any
	json.Unmarshal(jsonV, &tmp)
	jsonV2, _ := json.Marshal(tmp)
	println(string(jsonV2))
}

Output:

{"Value":4475034517144338534}
{"Value":4475034517144338400}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions