Skip to content

Conversation

andras-markos
Copy link

@andras-markos andras-markos commented Aug 21, 2025

Supersedes #58; this PR contains only the fix.

This fixes var resolution so a default is applied only when the path is missing, not when the value is explicitly null.

Why

Per the JsonLogic docs, the second var argument is a default “for values that might be missing.” Treating null as missing deviates from that intent and leads to surprising results.

What changed

  • Introduced an internal MISSING sentinel to track “not found” during path traversal.
  • Map access now uses containsKey to differentiate an absent key from a present key with null.
  • Array access returns MISSING for out-of-bounds indices.
  • evaluate(JsonLogicVariable, …) applies the default only when the traversal yields MISSING; explicit null is returned as null.

Examples

  • {"var": ["user.age", 42]} with {"user":{"age": null}}null (no default)
  • {"var": ["items.2", "missing"]} with {"items":[10,20]}"missing"
  • {"var": ""} returns the entire data object (same instance)

Tests

Added coverage to VariableTests:

  • Missing nested key falls back to default.
  • Present leaf null returns null (no default).
  • Intermediate null or non-traversable types return null.
  • Array index in-bounds returns the element; out-of-bounds uses default.
  • Top-level numeric index over lists works.
  • Empty var key returns the original data object.

Compatibility

Behavior change for callers who relied on “default-for-null.” This aligns with the JsonLogic spec and other interpreters.

@andras-markos andras-markos changed the title Fix: treat null differently from missing Fix: Distinguish missing vs null in var resolution (use default only when missing) Aug 21, 2025
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.

1 participant