Make optional route segments resolvable - #351
Open
sadiqk2 wants to merge 1 commit into
Open
Conversation
`Route::native('/posts/{slug?}', PostScreen::class)` could not match any URI at
all — not `/posts/hello`, not `/posts`.
resolve() builds its regex with `preg_replace('/\{(\w+)\}/', ...)`, and `\w` does
not match `?`. So a `{slug?}` placeholder was left in the pattern verbatim and the
compiled regex could only ever match the literal string `/posts/{slug?}`.
What makes it more than a dead pattern is that BootPlanner on both platforms
matches these routes happily when deciding whether to boot native. An app using
the documented optional syntax therefore launched into the native runloop and then
resolved to no screen — a blank native window rather than a fallback to the
WebView.
Optional segments are now substituted first, consuming their leading slash so the
segment and its separator disappear together, and an omitted one is dropped from
the parameters rather than reported as an empty string — a screen asking whether
it has a parameter should not be told yes for a segment nobody sent. `[^/]+`
cannot match empty, so no legitimate value is filtered out.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
This route cannot match any URI at all — not
/posts/hello, not/posts.NativeRouter::resolve()compiles its pattern withpreg_replace('/\{(\w+)\}/', '(?P<$1>[^/]+)', $pattern), and\wdoes not match?. The{slug?}placeholder therefore survives into the regex verbatim, so the compiled pattern only ever matches the literal string/posts/{slug?}.What turns a dead pattern into a device-visible failure:
BootPlanneron both Android and iOS matches these patterns when it decides whether to boot native. An app using the documented optional syntax boots into the native runloop and then resolves to no screen — a blank native window instead of the WebView fallback that would otherwise have carried it.The fix
#/\{(\w+)\?\}#→(?:/(?P<$1>[^/]+))?), so the segment and its separator disappear together and/postsmatches.paramsrather than reported as''. A screen checking whether it received a parameter should not be told yes for a segment nobody sent.[^/]+cannot match empty, so no legitimate value is filtered.Required segments are unaffected.
Verification
New
tests/Unit/Edge/NativeRouterOptionalParamsTest.php— 6 cases: segment present, segment omitted,isNativeRoute()agreeing withresolve()for both (the BootPlanner half), required segments still required, an optional segment after a required one, and an optional segment not swallowing a deeper path.4 of the 6 fail against
main. Full suite: 887 passed, and the 13 failures are identical tomain's baseline (Android splash-screen and release-build tests, unrelated to this change).vendor/bin/pint --testclean.