FIX: category routes model params should decode their URL parts (#8612)

FIX: category routes model params should decode their URL parts (#8612)

  • FIX: category routes model params should decode their URL parts

Ember’s route star globbing does not uri decode by default. This is problematic for subcategory globs with encoded URL site settings enabled.

Subcategories with encoded URLs will 404 without this decode.

I found this https://github.com/tildeio/route-recognizer/pull/91 which explicitly explains that globbing does not decode automatically.

diff --git a/app/assets/javascripts/discourse/models/category.js.es6 b/app/assets/javascripts/discourse/models/category.js.es6
index a1dada7188..78832ccbf3 100644
--- a/app/assets/javascripts/discourse/models/category.js.es6
+++ b/app/assets/javascripts/discourse/models/category.js.es6
@@ -334,7 +334,11 @@ Category.reopenClass({
   },
 
   findBySlugPathWithID(slugPathWithID) {
-    const parts = slugPathWithID.split("/");
+    let parts = slugPathWithID.split("/");
+    // slugs found by star/glob pathing in emeber do not automatically url decode - ensure that these are decoded
+    if (Discourse.SiteSettings.slug_generation_method === "encoded") {
+      parts = parts.map(urlPart => decodeURI(urlPart));
+    }
     let category = null;
 
     if (parts.length > 0 && parts[parts.length - 1].match(/^\d+$/)) {

GitHub sha: 63323bd7

1 Like

We should add a test to ensure this does not regress.

1 Like

Long overdue, added an acceptance test for this here:

1 Like

Can this be this.siteSettings instead? I think we inject siteSettings into models by default.

Maybe that’s still a work in progress? I’m not getting successful tests with this.siteSettings here.

1 Like

Models don’t get siteSettings unless queried by the store. I would have expected Categories to get built by the store but I suppose they are not!

2 Likes