Skip to content

fix: ObjExtend should work with non-object lhs - #1120

Draft
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/objextend-nonobject-lhs
Draft

fix: ObjExtend should work with non-object lhs#1120
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/objextend-nonobject-lhs

Conversation

@He-Pin

@He-Pin He-Pin commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix ObjExtend (lhs { body } syntax) to correctly handle non-object lhs values per jsonnet specification.

Problem

Per jsonnet spec, lhs { body } is equivalent to lhs + { body }. When lhs is not an object (e.g., string), sjsonnet was throwing a type error instead of falling back to add semantics.

Before:

"hello" { x: 1 }
// Error: Expected object, found string

After:

"hello" { x: 1 }
// Output: "hello{\"x\": 1}"

Solution

Modified visitObjExtend in Evaluator.scala to:

  1. Check if base is Val.Obj - if so, use existing object extend logic
  2. If base is not an object, evaluate body as standalone object (super = null)
  3. Apply add semantics: string + object = string concat with stringified object

This matches:

  • go-jsonnet: Desugars ApplyBrace to Binary + at parse time
  • jrsonnet: Recent fix (commit b5fcc2639d) falls back to evaluate_add_op

Changes

  • sjsonnet/src/sjsonnet/Evaluator.scala: Handle non-object lhs in visitObjExtend
  • sjsonnet/test/resources/new_test_suite/objextend_nonobject_lhs.jsonnet: Test case
  • sjsonnet/test/resources/new_test_suite/objextend_nonobject_lhs.jsonnet.golden: Expected output

Test Plan

  • Added test case for string + object concatenation
  • CI tests pass
  • Verify no regression in existing object extend behavior

@He-Pin
He-Pin marked this pull request as draft September 3, 2026 09:13
@He-Pin
He-Pin force-pushed the fix/objextend-nonobject-lhs branch from 2900488 to bf763b2 Compare September 3, 2026 09:19
Per jsonnet spec, `lhs { body }` is equivalent to `lhs + { body }`.
When lhs is not an object (e.g., string), the expression should fall
back to add semantics instead of throwing a type error.

This matches go-jsonnet behavior (which desugars ApplyBrace to Binary+)
and jrsonnet's recent fix (commit b5fcc2639d).

Changes:
- Evaluator.scala: Handle non-object lhs in visitObjExtend by evaluating
  body as standalone object and applying add semantics
- Add test case for string + object concatenation

Example:
  "hello" { x: 1 }  =>  "hello{\"x\": 1}"
@He-Pin
He-Pin force-pushed the fix/objextend-nonobject-lhs branch from bf763b2 to 8284ca9 Compare September 3, 2026 09:25
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