From db490a2c0fc92e79531d34d60e4c14bf4b62a5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20R=C3=BCtter?= Date: Wed, 11 Nov 2026 08:44:17 +0000 Subject: [PATCH 1/2] Let to_json output null for null values Co-Authored-By: Claude Opus 5.5 --- .../Antlers/Language/Runtime/ModifierManager.php | 2 ++ tests/Antlers/Runtime/CoreModifiersTest.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/View/Antlers/Language/Runtime/ModifierManager.php b/src/View/Antlers/Language/Runtime/ModifierManager.php index 3d3b9bd5178..414b5260490 100644 --- a/src/View/Antlers/Language/Runtime/ModifierManager.php +++ b/src/View/Antlers/Language/Runtime/ModifierManager.php @@ -27,6 +27,8 @@ class ModifierManager */ protected static $nullAwareModifiers = [ 'bool_string', + 'json', + 'to_json', ]; public static function isModifier(ParameterNode $node) diff --git a/tests/Antlers/Runtime/CoreModifiersTest.php b/tests/Antlers/Runtime/CoreModifiersTest.php index 851c57f4705..6672b9e774a 100644 --- a/tests/Antlers/Runtime/CoreModifiersTest.php +++ b/tests/Antlers/Runtime/CoreModifiersTest.php @@ -623,6 +623,20 @@ public function test_bool_string_outputs_false_for_null() $this->assertSame('FALSE', $this->renderString('{{ empty_field | bool_string | upper }}', $data, true)); $this->assertSame('true', $this->renderString('{{ filled_field | bool_string }}', $data, true)); } + + public function test_to_json_outputs_null_for_null() + { + $data = [ + 'bare' => null, + 'empty_field' => new Value(null, 'empty_field', new Text), + 'filled_field' => new Value('yes', 'filled_field', new Text), + ]; + + $this->assertSame('null', $this->renderString('{{ bare | to_json }}', $data, true)); + $this->assertSame('null', $this->renderString('{{ empty_field | to_json }}', $data, true)); + $this->assertSame('null', $this->renderString('{{ empty_field | json }}', $data, true)); + $this->assertSame('"yes"', $this->renderString('{{ filled_field | to_json }}', $data, true)); + } } class SimpleEntryObject implements Arrayable From 15ff97e396e0890cb80990303a225e310bc3f187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20R=C3=BCtter?= Date: Wed, 11 Nov 2026 08:46:55 +0000 Subject: [PATCH 2/2] Cover more to_json null cases --- tests/Antlers/Runtime/CoreModifiersTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Antlers/Runtime/CoreModifiersTest.php b/tests/Antlers/Runtime/CoreModifiersTest.php index 6672b9e774a..0316e62a45a 100644 --- a/tests/Antlers/Runtime/CoreModifiersTest.php +++ b/tests/Antlers/Runtime/CoreModifiersTest.php @@ -635,6 +635,9 @@ public function test_to_json_outputs_null_for_null() $this->assertSame('null', $this->renderString('{{ bare | to_json }}', $data, true)); $this->assertSame('null', $this->renderString('{{ empty_field | to_json }}', $data, true)); $this->assertSame('null', $this->renderString('{{ empty_field | json }}', $data, true)); + $this->assertSame('null', $this->renderString('{{ missing | to_json }}', $data, true)); + $this->assertSame('null', $this->renderString("{{ empty_field | to_json('pretty') }}", $data, true)); + $this->assertSame('NULL', $this->renderString('{{ empty_field | to_json | upper }}', $data, true)); $this->assertSame('"yes"', $this->renderString('{{ filled_field | to_json }}', $data, true)); } }