Skip to content

Conditional operator returns the condition itself when the condition is an object or an array (follow-up to #127) #450

Description

@paddock3d

Version: observed on 2.6.4 and reproduced by the HCL Universal Orchestrator team on the 3.x line (Jackson 3 build).

Summary

When the condition of cond ? a : b evaluates to a non-empty object or a non-empty array, JSONata4Java returns the condition value instead of evaluating a branch. #127 fixed the case where the condition is a string field (key ? 1 : 2); the object and array cases still misbehave. The reference implementation (jsonata-js 2.x) applies the $boolean() casting rules: a non-empty object or array is true, an empty array is false.

Reproduction (input {})

expression jsonata-js JSONata4Java
{'k': 1} ? 'YES' : 'NO' "YES" {"k":1}
[1] ? 'YES' : 'NO' "YES" [1]
$match('HCL1', /^HCL[0-9]+$/) ? 'YES' : 'NO' "YES" {"match":"HCL1","index":0,"groups":[]}
$match('x', /^HCL[0-9]+$/) ? 'YES' : 'NO' "NO" []
'abc' ? 'YES' : 'NO' "YES" "YES" (correct)
$boolean($match('HCL1', /^HCL[0-9]+$/)) ? 'YES' : 'NO' "YES" "YES" (correct)
$type('HCL1') = 'string' and $match('HCL1', /^HCL[0-9]+$/) ? 'YES' : 'NO' "YES" "YES" (correct)

Expected

Per https://docs.jsonata.org/programming#conditional-logic the condition is cast with the $boolean rules, so the first four rows should return "YES", "YES", "YES", "NO".

Impact

$match(x, /re/) ? a : b is the natural way to write an allow-list; on JSONata4Java it silently yields the match object (or an empty array) in place of the intended branch value, with no error.

Workaround

Wrap the condition in $boolean(), use $contains(x, /re/), or combine with a boolean via and.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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