DioxusLabs / dioxus

Fullstack GUI library for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
18.5k stars 703 forks source link

Route order matters between url string and enum fields #2353

Closed rogusdev closed 1 week ago

rogusdev commented 1 week ago

Per @ealmloff at https://discord.com/channels/899851952891002890/1231274531004481627/1231297848130404402

Problem

If I do this:

    #[route("/:id2/blog/:id")]
    Blog { id: i32, id2: i32 },

(Note that the url string has id2 then id, but the enum variant has id then id2, the reverse of the url.)

I get this error:

error: Could not find a field with the name 'id'
 --> src/main.rs:8:5
  |
8 |     Blog { id: i32, id2: i32 },

But if the order in the enum matches the url, it compiles fine. It is an easy fix, ofc, but it is 1) not obvious from the error, and 2) seems like something that should not break, ideally.

Also note that the order in the actual component definition does not matter:

#[component]
fn Blog(id: i32, id2: i32) -> Element {

Or

#[component]
fn Blog(id2: i32, id: i32) -> Element {

Both behave the same -- i.e. the critical factor is matching the order between the url string and the enum variant arguments

Steps To Reproduce

Full code:

use dioxus::prelude::*;

#[derive(Clone, Routable, Debug, PartialEq)]
enum Route {
    #[route("/")]
    Home {},
    #[route("/:id2/blog/:id")]
    Blog { id: i32, id2: i32 },
}

fn main() {
    launch(App);
}

#[component]
fn App() -> Element {
    rsx! {
        Router::<Route> {}
    }
}

#[component]
fn Blog(id: i32, id2: i32) -> Element {
    rsx! {
        "Blog post {id2}/{id}"
    }
}

#[component]
fn Home() -> Element {
    rsx! {
        Link {
            to: Route::Blog {
                id: 0,
                id2: 0
            },
            "Go to blog"
        }
    }
}

Expected behavior

Don't get an error if the order does not match up.