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

Handling Nulls in Currency Filter #477

Merged
merged 3 commits into from Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/DotLiquid.Tests/StandardFilterTests.cs
Expand Up @@ -714,6 +714,26 @@ public void TestEuroCurrencyFromString(object input, string expected, string lan
Assert.AreEqual(expected, StandardFilters.Currency(context: _contextV20, input: input, languageTag: languageTag));
}

[Test]
[TestCase(null)]
[TestCase("")]
public void TestNullOrEmptyInputCurrency(string input)
{
// Set the thread culture and test for backward compatibility
using (CultureHelper.SetCulture("en-US"))
{
Helper.AssertTemplateResult(
expected: string.Empty,
template: "{{ input | currency: 'de-DE' }}",
localVariables: Hash.FromAnonymousObject(new { input = input }));
}

// _contextV20 is initialized with InvariantCulture
Assert.AreEqual(
expected: input,
actual: StandardFilters.Currency(context: _contextV20, input: input, languageTag: "de-DE"));
}

[Test]
public void TestMalformedCurrency()
{
Expand Down
3 changes: 3 additions & 0 deletions src/DotLiquid/StandardFilters.cs
Expand Up @@ -359,6 +359,9 @@ public static string Rstrip(string input)
/// <seealso href="https://shopify.dev/api/liquid/filters/money-filters#money">Shopify Money filter</seealso>
public static string Currency(Context context, object input, string languageTag = null)
{
// Check for null input, return null
if (input == null) return null;
microalps marked this conversation as resolved.
Show resolved Hide resolved

// Check for null only, allow an empty string as it represent the InvariantCulture
var culture = languageTag == null ? context.CurrentCulture : new CultureInfo(languageTag.Trim());

Expand Down