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

Modified error handling in UnmarshalString to marshal non-string input into a JSON string #3530

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion graphql/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func UnmarshalString(v any) (string, error) {
case nil:
return "", nil
default:
return "", fmt.Errorf("%T is not a string", v)
payload, err := json.Marshal(v)
if err != nil {
return "", fmt.Errorf("failed to marshal value of type %T: %w", v, err)
}

return string(payload), nil
}
}
2 changes: 2 additions & 0 deletions graphql/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
assert.Equal(t, "true", mustUnmarshalString(t, true))
assert.Equal(t, "false", mustUnmarshalString(t, false))
assert.Equal(t, "", mustUnmarshalString(t, nil))
assert.Equal(t, `{"hello":"world"}`, mustUnmarshalString(t, map[string]interface{}{"hello": "world"}))

Check failure on line 34 in graphql/string_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

use-any: since Go 1.18 'interface{}' can be replaced by 'any' (revive)

Check failure on line 34 in graphql/string_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.23)

use-any: since Go 1.18 'interface{}' can be replaced by 'any' (revive)
assert.Equal(t, `{"hello":123}`, mustUnmarshalString(t, map[string]interface{}{"hello": 123}))

Check failure on line 35 in graphql/string_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

use-any: since Go 1.18 'interface{}' can be replaced by 'any' (revive)

Check failure on line 35 in graphql/string_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.23)

use-any: since Go 1.18 'interface{}' can be replaced by 'any' (revive)
})
}

Expand Down
Loading