revolution icon indicating copy to clipboard operation
revolution copied to clipboard

modTemplateVar::parseBinding - third parameter is invalid JSON

Open sdrenth opened this issue 3 years ago • 1 comments

Bug report

Summary

I have a template variable which contains the input option values as:

@SELECT CONCAT(`pagetitle`, ' (', `id`, ')') AS `name`,`id` FROM `[[+PREFIX]]site_content` WHERE `published` = 1 AND `deleted` = 0 AND `context_key` = '[[+context_key]]'

When I open a resource that contains this template variable, the following error is logged in the MODX error log:

[2022-06-02 16:15:22] (ERROR @/core/src/Revolution/modTemplateVar.php : 1016) modTemplateVar::parseBinding - third parameter is invalid JSON :@SELECT CONCAT(`pagetitle`, ' (', `id`, ')') AS `name`,`id` FROM `[[+PREFIX]]site_content` WHERE `published` = 1 AND `deleted` = 0 AND `context_key` = '[[+context_key]]'

Looking at the code it expects to contain a JSON string but it's not. I think the parseBinding method requires some additional logic maybe?

public function parseBinding($binding_string)
    {
        $match = [];
        $match2 = [];
        $binding_string = trim($binding_string);
        $regexp = '/@(' . implode('|', $this->bindings) . ')\s*(.*)/is'; /* Split binding on whitespace */

        if (preg_match($regexp, $binding_string, $match)) {
            /* We can't return the match array directly because the first element is the whole string */

            $regexp2 = '/(\S+)\s+(.+)/is'; /* Split binding on second whitespace to get properties */

            $properties = [];
            if (preg_match($regexp2, $match[2] , $match2)) {
                if (isset($match2[2])) {
                    $props = json_decode($match2[2],true);
                    $valid = json_last_error() === JSON_ERROR_NONE;
                    if ($valid && is_array($props)){
                        $properties = $props;
                        $match[2] = $match2[1];
                    } else {
                        $this->xpdo->log(modX::LOG_LEVEL_ERROR, 'modTemplateVar::parseBinding - third parameter is invalid JSON :' . $binding_string);
                    }
                }
            }

            $binding_array = [
                strtoupper($match[1]),
                trim($match[2]),
                $properties
            ]; /* Make command uppercase */

            return $binding_array;
        }
    }

Step to reproduce

  1. Create a TV with input option values:
@SELECT CONCAT(`pagetitle`, ' (', `id`, ')') AS `name`,`id` FROM `[[+PREFIX]]site_content` WHERE `published` = 1 AND `deleted` = 0 AND `context_key` = '[[+context_key]]'
  1. Open resource
  2. Open MODX error log

Observed behavior

An error is written in the error log which I don't think is okay.

Expected behavior

I'd expect this to not trigger any errors, because it is a valid/working value.

Environment

MODX 3.0.1-pl PHP 8.1

sdrenth avatar Jun 02 '22 14:06 sdrenth

In my test the error was logged but it doesn't appear to break anything.

Adding support properties for @CHUNK/@SNIPPET bindings introduced in PR #15488 is the reason why this error is logged.

JoshuaLuckers avatar Jun 09 '22 11:06 JoshuaLuckers

This issue has been mentioned on MODX Community. There might be relevant details there:

https://community.modx.com/t/formalicious-and-modx-3/5904/2

rthrash avatar Oct 17 '22 19:10 rthrash