-
Notifications
You must be signed in to change notification settings - Fork 34
Preserve large-integer precision when decoding tool arguments #528
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,10 @@ | |
| package jsonformat | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/google/jsonschema-go/jsonschema" | ||
| ) | ||
|
|
@@ -69,9 +71,18 @@ func (f *Format) Normalize(v any) error { | |
| func applySchema(data json.RawMessage, resolved *jsonschema.Resolved) (json.RawMessage, error) { | ||
| var v any | ||
| if len(data) > 0 { | ||
| if err := json.Unmarshal(data, &v); err != nil { | ||
| // Decode with UseNumber so integers beyond 2^53 are not silently | ||
| // truncated by being decoded into float64 and re-marshalled. | ||
| dec := json.NewDecoder(bytes.NewReader(data)) | ||
| dec.UseNumber() | ||
| if err := dec.Decode(&v); err != nil { | ||
|
Comment on lines
+74
to
+78
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| return nil, fmt.Errorf("unmarshaling arguments: %w", err) | ||
| } | ||
| // Unlike json.Unmarshal, json.Decoder tolerates trailing data after the | ||
| // first value; reject it so validation is not looser than before. | ||
| if _, err := dec.Token(); err != io.EOF { | ||
| return nil, fmt.Errorf("unmarshaling arguments: unexpected trailing data after JSON value") | ||
| } | ||
| } | ||
| if err := resolved.ApplyDefaults(&v); err != nil { | ||
| return nil, fmt.Errorf("applying schema defaults: %w", err) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — added the io import and a strict trailing-data check (dec.Token() must return io.EOF). Note: validate() can't also use UseNumber — json.Number is a string type the jsonschema validator rejects as a non-integer, which broke every test; left as a comment in the PR.