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 matching issue with placeholders #365

Merged
merged 2 commits into from
Jun 27, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,12 @@ private CompareResult compareComponentParam(
+ new String(placeholderValue), false);
} else return null;
}
if (byteArray[valueStartPos + j] != valueToMatch[k]) {
// We do the comparison from the back. If the value in the index (before the placeholder
// starts) is longer than valueToMatch we need to abort.
// e.g. template value: "{something}longer" and valueToMatch: "onger". As we compare
// from back all the chars match but the template value is longer so as soon as we reach
// the end of valueToMatch before we reach the placeholder end char we need to abort.
if (k < 0 || byteArray[valueStartPos + j] != valueToMatch[k]) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix.

return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MatchIndexTests {
private val entryWithAllowedValues = DeepLinkEntry.MethodDeeplinkEntry("http{scheme(|s)}://www.example.{tld(de|fr)}/somePath1/differentPath2", MatchIndexTests::class.java.name, "someMethod10")
private val entryWithAllowedValueOnlyOneElement = DeepLinkEntry.MethodDeeplinkEntry("http{scheme(s)}://www.justtesting.com/somePath", MatchIndexTests::class.java.name, "someMethod10")
private val entryWithEmptyAllowedValue = DeepLinkEntry.MethodDeeplinkEntry("http{scheme()}://www.anothertest.com/somePath", MatchIndexTests::class.java.name, "someMethod10")
private val entryWithAllowedValueAndLongerValueThan = DeepLinkEntry.MethodDeeplinkEntry("scheme://{some_value(allowed|values)}somethinglonger/one/{param}/three", MatchIndexTests::class.java.name, "someMethod10")

private val allowedValuesPlaceholderNames = setOf("scheme", "tld")

Expand All @@ -23,9 +24,23 @@ class MatchIndexTests {
DeepLinkEntry.MethodDeeplinkEntry("http://example.com/path1/someOtherPathElement2", MatchIndexTests::class.java.name, "someMethod6"),
DeepLinkEntry.MethodDeeplinkEntry("http://example.com/", MatchIndexTests::class.java.name, "someMethod8"),
entryWithAllowedValues,
entryWithAllowedValueOnlyOneElement
entryWithAllowedValueOnlyOneElement,
entryWithAllowedValueAndLongerValueThan
).sortedBy { it.uriTemplate }

@Test
fun testMatchWithPlaceholderThatEndsInSegmentWithLongerMatch() {
// This is testing a condition where an URL segment (in this case the host) has a placeholder with allowed values
// in the template and is matching (from the back) to what is in the to be matched URL, where the entry in the template
// is matching but longer (and thus not matching) before the placeholder starts (from the back).
// In this case the host is it like this:
// template host: {some_value(allowed|values)}somethinglonger
// to match host: longer
val deepLinkUri = DeepLinkUri.parse("scheme://longer/one/param/three")
val matchEntry = testRegistry(testEntries).idxMatch(deepLinkUri, emptyMap())
assertThat(matchEntry).isNull()
}

@Test
fun testGetAllEntries() {
val allEntries = testRegistry(testEntries).getAllEntries().sortedBy { it.uriTemplate }
Expand Down