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

Can't use uri! with routes that have optional query params #827

Closed
anna-is-cute opened this issue Nov 14, 2018 · 5 comments
Closed

Can't use uri! with routes that have optional query params #827

anna-is-cute opened this issue Nov 14, 2018 · 5 comments
Labels
deficiency Something doesn't work as well as it could
Milestone

Comments

@anna-is-cute
Copy link
Contributor

anna-is-cute commented Nov 14, 2018

  1. The version of Rocket you're using. Ensure it's the latest, if possible: 0.4.0-rc.1

  2. The operating system (distribution and version) where the issue occurs: macOS 10.14.1

  3. A brief description of the bug: It is not possible to use uri! for a route that has optional query params (Option<T>).

  4. How you uncovered the bug. Short, reproducible tests are especially useful.

#[get("/?<a>")]
fn get(a: Option<u64>) -> String {
  format!("{:?}", a);
}

fn whatever() {
  let _uri = uri!(get: Some(1));
  let _uri = uri!(get: None);
}

the trait rocket::http::uri::FromUriParam<_> is not implemented for std::option::Option<u64>

@SergioBenitez SergioBenitez added the deficiency Something doesn't work as well as it could label Nov 14, 2018
@SergioBenitez SergioBenitez added this to the 0.4.0 milestone Nov 19, 2018
@cazgp
Copy link

cazgp commented Aug 15, 2019

I don't think the linked commit resolves the issue, as I'm having the same problem and the provided code snippet continues to fail:

let _uri = uri!(get: None);
   |                          ^^^^ the trait `rocket::http::uri::FromUriParam<rocket::http::uri::Query, std::option::Option<_>>` is not implemented for `u64`

My use-case is that several parameters are optional for an endpoint. They are then passed through to a pagination template which generates "next" and "page number" links, keeping the values of the optional parameters so that the user's filters and searches persist.

However this is currently not possible using the uri! macro and the uri needs to be constructed manually. So instead of something simple like:

let link = uri!(
    actv::activities::all::all_get:
        date = filters.search_date,
        locations = &filters.locations_chosen,
        page = page_number,
        types = &filters.types_chosen,
);

you get:

let mut uri = vec![];
if filters.search_date.is_some() {
    uri.push(format!("date={}", filters.search_date.unwrap());
}
if filters.locations { ... }
...

Obviously this is not ideal.

@jebrosen
Copy link
Collaborator

@cazgp How did you define the route in question?

@cazgp
Copy link

cazgp commented Aug 15, 2019

Similarly to the OP's code snippet, which doesn't compile. Optional URL query params.

@jebrosen
Copy link
Collaborator

It turns out that what was implemented does not allow exactly what was originally posted for this issue. You can either provide a value (without Some) or omit it (by specifying _). There is a deficiency in this: it's not possible to insert a parameter if you already have an Option<T>. Unfortunately supporting both methods turns out to be tricky, so it will need some thought.

@cazgp
Copy link

cazgp commented Aug 18, 2019

Can we re-open this issue then please? It appears to be either a feature request or a bug. If there's a route with multiple optional parameters, a separate function which manually builds the URL is needed, because otherwise it ends up with a combinatorial explosion of the uri! macro (checking every combination for whether or not it should be value or _).

@jebrosen jebrosen mentioned this issue Oct 19, 2019
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deficiency Something doesn't work as well as it could
Projects
None yet
Development

No branches or pull requests

4 participants