Skip to content

Add type checking for variadic function arguments - #102

Open
AdamMagued wants to merge 1 commit into
jmespath:masterfrom
AdamMagued:fix-variadic-arg-type-checking
Open

AdamMagued wants to merge 1 commit into
jmespath:masterfrom
AdamMagued:fix-variadic-arg-type-checking

Conversation

@AdamMagued

Copy link
Copy Markdown

Summary

Add argument type validation for variadic functions in functionEntry.resolveArgs.

Problem & Root Cause

In functionEntry.resolveArgs (functions.go), non-variadic functions validate both argument count and individual argument types via argSpec.typeCheck. However, when a function entry is marked variadic: true, resolveArgs only checked len(arguments) < len(e.arguments) and bypassed argSpec.typeCheck entirely.

As a result, passing non-object values (such as nil, strings, numbers, booleans, or arrays) into functions like merge() resulted in unhandled runtime panics during downcast type assertions inside function handlers (e.g. mapped := m.(map[string]interface{}) in jpfMerge in functions.go:519), rather than cleanly returning an evaluation error.

Fix

In functionEntry.resolveArgs, iterate over all supplied arguments for variadic functions and validate each argument using argSpec.typeCheck:

  • Fixed prefix arguments (i < len(e.arguments)-1) are validated against e.arguments[i].
  • Variadic arguments (i >= len(e.arguments)-1) are validated against e.arguments[len(e.arguments)-1].

If any argument fails type checking, an evaluation error is returned instead of panicking.

Verification

  • Added TestVariadicFunctionTypeChecking covering merge() with invalid types (nil, string, number, array, boolean), merge() with missing/nil fields as reported in merge in go-jmespath / jp panics when parameters are not JSON objects #72, and not_null() with variadic arguments.
  • Ran all existing unit and compliance tests (go test -v ./...), 100% passing.
  • Checked code formatting (gofmt -l .) and static analysis (go vet ./...).

Fixes #72

In functionEntry.resolveArgs, variadic functions only validated that
len(arguments) >= len(e.arguments) without performing type checking on
either fixed or variadic arguments. When passing arguments of unexpected
types (such as nil, strings, or numbers) to functions like merge(),
downstream type assertions in handlers (e.g. jpfMerge) panicked at runtime
instead of returning an evaluation error.

Iterate over arguments in resolveArgs for variadic functions, checking
each argument against its corresponding argSpec or the trailing variadic
argSpec.

Fixes jmespath#72
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 this pull request may close these issues.

merge in go-jmespath / jp panics when parameters are not JSON objects

1 participant