Skip to content

Commit 2069f7c

Browse files
author
monika.kwiatek
committed
Fixes when the search term is null
1 parent aa98c43 commit 2069f7c

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

be/src/Unic.UrlMapper2/code/Services/RedirectSearcher.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ protected virtual Redirect MapToSearchResult(RedirectSearchResultItem redirectSe
6363
this.logger.Error($"Failed to parse source protocol {redirectSearchResultItem.SourceProtocol}", this);
6464
}
6565

66+
var sourceTerm = redirectSearchResultItem.SourceTerm;
6667
var redirect = new Redirect
6768
{
6869
RedirectSearchData = redirectSearchData,
@@ -71,23 +72,22 @@ protected virtual Redirect MapToSearchResult(RedirectSearchResultItem redirectSe
7172
SourceProtocol = sourceProtocol,
7273
RegexEnabled = redirectSearchResultItem.RegexEnabled,
7374
PreserveQueryString = redirectSearchResultItem.PreserveQueryString,
74-
Term = redirectSearchResultItem.SourceTerm
75+
Term = sourceTerm
7576
};
7677

77-
this.HandlePreserveQueryString(redirect, redirectSearchResultItem);
78+
this.HandlePreserveQueryString(redirect, sourceTerm);
7879
return redirect;
7980
}
8081

81-
protected virtual void HandlePreserveQueryString(Redirect redirect, RedirectSearchResultItem redirectSearchResultItem)
82+
protected virtual void HandlePreserveQueryString(Redirect redirect, string sourceTerm)
8283
{
8384
if (!redirect.PreserveQueryString) return;
8485

8586
redirect.RegexEnabled = true;
86-
var sourceTerm = redirectSearchResultItem.SourceTerm;
8787
if (!string.IsNullOrWhiteSpace(sourceTerm)
8888
&& !sourceTerm.EndsWith(Constants.RegularExpressions.QueryStringExpression))
8989
{
90-
redirect.Term = redirectSearchResultItem.SourceTerm + Constants.RegularExpressions.QueryStringExpression;
90+
redirect.Term = sourceTerm + Constants.RegularExpressions.QueryStringExpression;
9191
}
9292
}
9393

be/src/Unic.UrlMapper2/code/Services/RedirectionService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ protected virtual Redirect GetRegexMatch(RedirectSearchData redirectSearchData,
102102
if (string.IsNullOrWhiteSpace(sourceTerm)) return default;
103103

104104
// We are going to take the one regex redirect which has the longest match within the term
105-
var regexMatches = enumerableRedirects.Where(r => r.RegexEnabled && Regex.IsMatch(input: sourceTerm, pattern: r.Term));
106-
var regexMatch = regexMatches.OrderByDescending(r => r.Term.Length).FirstOrDefault();
105+
var regexMatches = enumerableRedirects.Where(r => r.RegexEnabled && !string.IsNullOrWhiteSpace(r.Term) && Regex.IsMatch(input: sourceTerm, pattern: r.Term));
106+
var regexMatch = regexMatches.OrderByDescending(r => r.Term?.Length).FirstOrDefault();
107107

108108
return regexMatch;
109109
}

0 commit comments

Comments
 (0)