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

Validate extension types of handlers #1303

Closed
julianbuettner opened this issue Aug 22, 2022 · 1 comment
Closed

Validate extension types of handlers #1303

julianbuettner opened this issue Aug 22, 2022 · 1 comment

Comments

@julianbuettner
Copy link

Feature Request

Motivation

fn handler(Extension(state): Extension<Arc<i32>>) {}

When trying to extract a extension of a State that ist not .layer()d on to the app,
a HTTP 500 is returned when querying the handler.

$ curl http://localhost:3000/
Missing request extension: Extension of type `alloc::sync::Arc<i32>` was not found. Perhaps you forgot to add it? See `axum::Extension`.

To validate whether or not all endpoints have valid extension types, every single endpoint has to be queried at runtime.

This feels frustrating and avoidable.
I assume many projects don't build their paths dynamically, but define the entire application at compile time.
In these cases it feels like it could be validated at compile time (maybe with macros? other ways?) to fit the
"If it compiles, it works" philosophy.
I understand that this is probably not an easy implementable solution.

However, validating extension types of endpoints when starting, is a simpler logic and would be nice to have available.

Proposal

Add a method to axum::Router which allows for validating handler extensions.

let state = State::new();
let app = Router::new()
    .route("/", service.get(handler))
    .layer(Extension(state))
    .validate_handler_extension_types()?;

Alternatively app could require to have the layer mounted before adding routes
requiring this extension type.

let state = State::new();
let app = Router::new()
     .layer(Extension(state))
    .route("/", service.get(handler));   // panic or Err(x) if layer would not have been mounted

Alternatives

  • Doing nothing
  • Documenting a way to figure out if all endpoints are using extractable extensions.
@davidpdrsn
Copy link
Member

This will be fixed in the next major version via the new State extractor.

See #1155

We don't have an ETA for when 0.6 will be shipped.

@davidpdrsn davidpdrsn closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants