Skip to content

Commit 22029de

Browse files
author
Tobias Feijten
committed
Properly add support for explicit inclusion of defaults in url generation
1 parent 1774562 commit 22029de

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

Resources/js/router.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function testGenerateSupportsOptionalPlaceholderDefaults() {
187187
function testGenerateSupportsExplicitNonOptionalPlaceholderDefaults() {
188188
var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http"}, {
189189
homepage: {
190-
tokens: [['variable', '/','', '!page'],['text', '/bar']],
190+
tokens: [['variable', '/','', 'page', false, true],['text', '/bar']],
191191
defaults: {page: 'api'},
192192
requirements: {},
193193
hosttokens: [

Resources/js/router.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ export class Router {
202202
}
203203

204204
if ('variable' === token[0]) {
205-
if (typeof token[3] === 'string' && token[3][0] === '!') { // If parameter starts with exclamation mark, always include in path
206-
token[3] = token[3].substring(1); // Remove the exclamation mark
205+
if (token.length === 6 && token[5] === true) { // Sixth part of the token array indicates if it should be included in case of defaults
207206
optional = false;
208207
}
209208
let hasDefault = route.defaults && !Array.isArray(route.defaults) && typeof token[3] === 'string' && (token[3] in route.defaults);

Resources/public/js/router.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ var Router = /*#__PURE__*/function () {
211211
}
212212

213213
if ('variable' === token[0]) {
214-
if (typeof token[3] === 'string' && token[3][0] === '!') {
215-
token[3] = token[3].substring(1); // Remove the exclamation mark
216-
214+
if (token.length === 6 && token[5] === true) {
215+
// Sixth part of the token array indicates if it should be included in case of defaults
217216
optional = false;
218217
}
219218

Resources/public/js/router.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/Controller/ControllerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ public function testIndexAction()
5353
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":"","locale":"en"}', $response->getContent());
5454
}
5555

56+
public function testIndexWithExplicitTokenAction()
57+
{
58+
$routes = new RouteCollection();
59+
$routes->add('literal', new Route('/homepage'));
60+
$routes->add('blog', new Route('/blog-post/{!slug}', array(), array(), array(), 'localhost'));
61+
62+
$controller = new Controller(
63+
$this->getSerializer(),
64+
$this->getExtractor($routes)
65+
);
66+
67+
$response = $controller->indexAction($this->getRequest('/'), 'json');
68+
69+
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug",false,true],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":"","locale":"en"}', $response->getContent());
70+
}
71+
5672
public function testIndexActionWithLocalizedRoutes()
5773
{
5874
$routes = new RouteCollection();

0 commit comments

Comments
 (0)