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

Template is unable to make Axum into_response #658

Closed
genusistimelord opened this issue Apr 1, 2022 · 5 comments
Closed

Template is unable to make Axum into_response #658

genusistimelord opened this issue Apr 1, 2022 · 5 comments

Comments

@genusistimelord
Copy link
Contributor

genusistimelord commented Apr 1, 2022

I have everything set from the current git.

[dependencies]
axum = "0.5"
axum-macros = "0.2"
askama = { version = "0.11", git = "https://github.com/djc/askama.git", features = [
    "serde-json",
    "with-axum",
] }
askama_axum = { version = "0.1", git = "https://github.com/djc/askama.git" }

and This is my file thats having issues.

use crate::forms::User;
use crate::{
    extensions::{Flashes, FullState},
    forms::UserOnlineDisplay,
};
use askama::Template;
use axum::{routing::get, Router};
use axum_macros::debug_handler;
use axum_sessions_auth::Authentication;
use serde::*;

#[derive(Template, Debug, Deserialize, Serialize)]
#[template(path = "main/main.html")]
struct MainTemplate {
    #[serde(default)]
    current_user: User,
    #[serde(default)]
    online: Vec<UserOnlineDisplay>,
    #[serde(default)]
    flashes: Flashes,
}

pub fn main_routes() -> Router {
    Router::new().route("/main", get(main_page))
}

#[debug_handler]
async fn main_page(state: FullState) -> MainTemplate {
    let online_user = state.state.users_online().await;

    MainTemplate {
        current_user: state.auth.current_user.unwrap_or_default(),
        online: online_user,
        flashes: state.flashes,
    }
}

the macro output is

https://pastebin.com/dsmSqB5t

tried with both stable and nightly fully updated.

It is causing the error

the trait bound `site::main::MainTemplate: axum::response::IntoResponse` is not satisfied
the trait `axum::response::IntoResponse` is not implemented for `site::main::MainTemplate`

which is further also causing

the trait bound `fn(extensions::state::FullState) -> impl futures::Future<Output = site::main::MainTemplate> {site::main::main_page}: axum::handler::Handler<_, _>` is not satisfied\nthe trait `axum::handler::Handler<_, _>` is not implemented for `fn(extensions::state::FullState) -> impl futures::Future<Output = site::main::MainTemplate> {site::main::main_page}`

even if I remove the part erroring out and it compiles without error it still does not build the into_response.
thank you for looking into the issue. Makes me wounder if the cfg is not working.

If i manually implement 'IntoResponse' it works just fine. So this part missing when Generated is causing issues.

impl IntoResponse for MainTemplate {
    fn into_response(self) -> Response {
        match self.render() {
            Ok(body) => {
                let headers = [(
                    http::header::CONTENT_TYPE,
                    http::HeaderValue::from_static(Self::MIME_TYPE),
                )];

                (headers, body).into_response()
            }
            Err(_) => http::StatusCode::INTERNAL_SERVER_ERROR.into_response(),
        }
    }
}
@genusistimelord
Copy link
Contributor Author

updating this with my test where i had to manually add in the into_response code.
https://github.com/genusistimelord/AskamaTest

@djc
Copy link
Collaborator

djc commented Apr 4, 2022

I suppose this could be another variant of #656?

@genusistimelord
Copy link
Contributor Author

I have already found the fix. I am checking each source manually to make sure all the tests work then i will publish a PR for it.

@djc
Copy link
Collaborator

djc commented Apr 4, 2022

Sounds good, thanks!

@genusistimelord
Copy link
Contributor Author

this was resolved in the PR https://github.com/djc/askama/pull/662

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants