Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VariableNotExistException for variable knowingly set to null #295

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kohlschuetter
Copy link
Contributor

Currently, we would get a VariableNotExistException when in strict mode, even if a page property, for example, is knowingly set to null.

This is specifically important since {% if val %} returns true even for empty strings, making it almost impossible to avoid an exception and still support properties like page.last_modified_at.

Introduce a "found" state when traversing structures, and only consider a non-existant variable if found=false

This change breaks backwards compatibility with classes implementing LookupNode.Indexable. This is considered low-risk at the moment.

Currently, we would get a VariableNotExistException when in strict mode,
even if a page property, for example, is knowingly set to null.

This is specifically important since {% if val %} returns true even for
empty strings, making it almost impossible to avoid an exception and
still support properties like page.last_modified_at.

Introduce a "found" state when traversing structures, and only consider
a non-existant variable if found=false

This change breaks backwards compatibility with classes implementing
LookupNode.Indexable. This is considered low-risk at the moment.
@msangel
Copy link
Collaborator

msangel commented Dec 27, 2023

@kohlschuetter having the variable at context level needs some more explanation. Description said about missing variable and nullable variable difference. I agree somehow about this bug.

As for me solution may be in use Map implementation that permits null values, use return type something more then value or null (some wrapper with states: not_found, found_null, found_value).

But this global for template context not clear how will solve the issue.

The current "strict variables" checking strategy in Liquid Templates is
somewhat unusable as one cannot even check for the existence of an
object property without raising an error in strict mode.

This is a known limitation, and actively being discussed upstream
(liquid issue 1034).

Several Jekyll template make use of checks like {% if page.mermaid %},
etc., preventing enforcement of strict mode.

This patch adds a "sane" strict variable policy, which allows for such
checks to succeed.

Since this is new functionality specific to liqp, we keep the existing
strict mode configuration unchanged.

Shopify/liquid#1034
@kohlschuetter
Copy link
Contributor Author

kohlschuetter commented Dec 27, 2023

@msangel sorry, I just saw your message. I've added another change that depends on this one, which is tangentially related ("sane" strict variable checks)

Regarding your question:

{% assign var = null %}{% if var %}true{% else %}false{% endif %}  

would fail without this patch (also see "paleo"'s comment in Shopify/liquid#1034)

I thought about adding a wrapper, but then we would have to rely on all participating classes, which is hard to enforce. Adding a "found" property looks like the simpler approach.

Do you think there's a situation we currently don't cover?

Previously, only "obj.missing" was allowed.
@kohlschuetter kohlschuetter changed the title Differentiate between null and missing values Fix VariableNotExistException for variable knowingly set to null Dec 28, 2023
@msangel
Copy link
Collaborator

msangel commented Feb 2, 2024

I dug the issue and agree that we must provide a fix. What I still think is wrong in this change is:
image

We have global flag for anyone for everyone for any variable. This goes against thread safety, immutability (which we are trying to achieve) and static behavior. Can this functionality be done without such flags?

@kohlschuetter, don't worry about backward compatibility, a lot of recent commits are breaking compatibility, so next version probably will have own major version set. There a lot of deprecated removals. Thanks!

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.

2 participants