From bb5eac023ca1945622e35ef1100153fe580d5aa3 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 30 Mar 2026 14:33:13 +0200 Subject: [PATCH] Change ReplacePlaceholders to return NULL for unresolved Previously, ReplacePlaceholders threw PlaceholderResolutionException when a placeholder could not be resolved. Now, unresolved placeholders are replaced with "NULL" instead. All exception references and related tests have been updated to reflect this new behavior. Documentation has also been revised accordingly. --- .../Common/Dto/PlaceholderExtensions.cs | 7 ++--- .../Behaviors/InvokeActionTests.cs | 31 +++++++------------ 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/ReC.Application/Common/Dto/PlaceholderExtensions.cs b/src/ReC.Application/Common/Dto/PlaceholderExtensions.cs index 35a864f..f7e3a71 100644 --- a/src/ReC.Application/Common/Dto/PlaceholderExtensions.cs +++ b/src/ReC.Application/Common/Dto/PlaceholderExtensions.cs @@ -1,7 +1,6 @@ using System.ComponentModel.DataAnnotations.Schema; using System.Reflection; using System.Text.RegularExpressions; -using ReC.Application.Common.Exceptions; namespace ReC.Application.Common.Dto; @@ -14,10 +13,8 @@ public static partial class PlaceholderExtensions /// Replaces placeholders in the format {#ANY_STRING#COLUMN_NAME} with the corresponding /// property value resolved via from the provided objects. /// Values are converted to SQL-compatible string representations. + /// If a placeholder cannot be resolved, it is replaced with NULL. /// - /// - /// Thrown when a placeholder's column name cannot be resolved from any of the provided objects. - /// public static string ReplacePlaceholders(this string str, params object?[] objects) { return PlaceholderRegex().Replace(str, match => @@ -37,7 +34,7 @@ public static partial class PlaceholderExtensions return ToSqlLiteral(value); } - throw new PlaceholderResolutionException(placeholder, columnName, str); + return "NULL"; }); } diff --git a/tests/ReC.Tests/Application/Behaviors/InvokeActionTests.cs b/tests/ReC.Tests/Application/Behaviors/InvokeActionTests.cs index 9ac80ad..2d3db35 100644 --- a/tests/ReC.Tests/Application/Behaviors/InvokeActionTests.cs +++ b/tests/ReC.Tests/Application/Behaviors/InvokeActionTests.cs @@ -1,6 +1,5 @@ using System.ComponentModel.DataAnnotations.Schema; using ReC.Application.Common.Dto; -using ReC.Application.Common.Exceptions; namespace ReC.Tests.Application.Behaviors; @@ -211,46 +210,40 @@ public class InvokeActionTests #region ReplacePlaceholders - Exception Tests [Test] - public void ReplacePlaceholders_UnresolvableColumn_ThrowsPlaceholderResolutionException() + public void ReplacePlaceholders_UnresolvableColumn_ReturnsNull() { var input = "WHERE X = {#INT#NON_EXISTING}"; - var ex = Assert.Throws(() => - input.ReplacePlaceholders(_foo, _bar, _fuz)); - - Assert.That(ex!.ColumnName, Is.EqualTo("NON_EXISTING")); - Assert.That(ex.Placeholder, Is.EqualTo("{#INT#NON_EXISTING}")); - Assert.That(ex.Input, Is.EqualTo(input)); + var result = input.ReplacePlaceholders(_foo, _bar, _fuz); + Assert.That(result, Is.EqualTo("WHERE X = NULL")); } [Test] - public void ReplacePlaceholders_NoObjectsProvided_ThrowsPlaceholderResolutionException() + public void ReplacePlaceholders_NoObjectsProvided_ReturnsNull() { var input = "WHERE X = {#INT#BAZ}"; - Assert.Throws(() => - input.ReplacePlaceholders()); + var result = input.ReplacePlaceholders(); + Assert.That(result, Is.EqualTo("WHERE X = NULL")); } [Test] - public void ReplacePlaceholders_ObjectWithoutColumnAttributes_ThrowsPlaceholderResolutionException() + public void ReplacePlaceholders_ObjectWithoutColumnAttributes_ReturnsNull() { var model = new NoColumnModel { Id = 1, Name = "Test" }; var input = "WHERE X = {#INT#Id}"; - Assert.Throws(() => - input.ReplacePlaceholders(model)); + var result = input.ReplacePlaceholders(model); + Assert.That(result, Is.EqualTo("WHERE X = NULL")); } [Test] - public void ReplacePlaceholders_MixedResolvableAndUnresolvable_ThrowsOnUnresolvable() + public void ReplacePlaceholders_MixedResolvableAndUnresolvable_ReturnsNullForUnresolvable() { var input = "WHERE BAZ = {#INT#BAZ} AND X = {#INT#UNKNOWN}"; - var ex = Assert.Throws(() => - input.ReplacePlaceholders(_foo, _bar, _fuz)); - - Assert.That(ex!.ColumnName, Is.EqualTo("UNKNOWN")); + var result = input.ReplacePlaceholders(_foo, _bar, _fuz); + Assert.That(result, Is.EqualTo("WHERE BAZ = 2 AND X = NULL")); } #endregion