Skip to content

Commit

Permalink
fix nested fallback actionable also on nested root
Browse files Browse the repository at this point in the history
  • Loading branch information
dalton-oliveira committed Jan 16, 2024
1 parent 9e31949 commit 6a4325c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 38 deletions.
97 changes: 59 additions & 38 deletions axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- None.
- **fixed:** Fallback handlers on nested routers returning 404

# 0.7.4 (13. January, 2024)

Expand Down Expand Up @@ -52,9 +52,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# 0.7.0 (27. November, 2023)

- **breaking:** Update public dependencies. axum now requires
- [hyper](https://crates.io/crates/hyper) 1.0
- [http](https://crates.io/crates/http) 1.0
- [http-body](https://crates.io/crates/http-body) 1.0
- [hyper](https://crates.io/crates/hyper) 1.0
- [http](https://crates.io/crates/http) 1.0
- [http-body](https://crates.io/crates/http-body) 1.0
- **breaking:** axum now requires [tower-http](https://crates.io/crates/tower-http) 0.5
- **breaking:** Remove deprecated `WebSocketUpgrade::max_send_queue`
- **breaking:** The following types/traits are no longer generic over the request body
Expand Down Expand Up @@ -654,7 +654,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **fixed:** Used `400 Bad Request` for `FailedToDeserializeQueryString`
rejections, instead of `422 Unprocessable Entity` ([#1387])
- **changed**: The inner error of a `JsonRejection` is now
`serde_path_to_error::Error<serde_json::Error>`. Previously it was
`serde_path_to_error::Error<serde_json::Error>`. Previously it was
`serde_json::Error` ([#1371])
- **changed:** The default body limit now applies to the `Multipart` extractor ([#1420])
- **breaking:** `ContentLengthLimit` has been removed. Use `DefaultBodyLimit` instead ([#1400])
Expand Down Expand Up @@ -796,7 +796,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# 0.6.0-rc.4 (9. November, 2022)

- **changed**: The inner error of a `JsonRejection` is now
`serde_path_to_error::Error<serde_json::Error>`. Previously it was
`serde_path_to_error::Error<serde_json::Error>`. Previously it was
`serde_json::Error` ([#1371])
- **added**: `JsonRejection` now displays the path at which a deserialization
error occurred ([#1371])
Expand Down Expand Up @@ -875,6 +875,7 @@ Yanked, as it didn't compile in release mode.

This also applies to these extractors which used `Bytes::from_request`
internally:

- `Form`
- `Json`
- `String`
Expand All @@ -887,7 +888,6 @@ Yanked, as it didn't compile in release mode.
without any routes will now result in a panic. Previously, this just did
nothing. [#1327]


[`axum-macros`]: https://docs.rs/axum-macros/latest/axum_macros/

## Middleware
Expand Down Expand Up @@ -1132,6 +1132,7 @@ Yanked, as it didn't compile in release mode.
}
}
```

- **breaking:** It is now only possible for one extractor per handler to consume
the request body. In 0.5 doing so would result in runtime errors but in 0.6 it
is a compile error ([#1272])
Expand Down Expand Up @@ -1291,6 +1292,7 @@ Yanked, as it didn't compile in release mode.

This also applies to these extractors which used `Bytes::from_request`
internally:

- `Form`
- `Json`
- `String`
Expand Down Expand Up @@ -1457,36 +1459,38 @@ Yanked, as it contained an accidental breaking change.
headers you need ([#698])

This includes these breaking changes:
- `RequestParts::take_headers` has been removed.
- `RequestParts::headers` returns `&HeaderMap`.
- `RequestParts::headers_mut` returns `&mut HeaderMap`.
- `HeadersAlreadyExtracted` has been removed.
- The `HeadersAlreadyExtracted` variant has been removed from these rejections:
- `RequestAlreadyExtracted`
- `RequestPartsAlreadyExtracted`
- `JsonRejection`
- `FormRejection`
- `ContentLengthLimitRejection`
- `WebSocketUpgradeRejection`
- `<HeaderMap as FromRequest<_>>::Rejection` has been changed to `std::convert::Infallible`.

- `RequestParts::take_headers` has been removed.
- `RequestParts::headers` returns `&HeaderMap`.
- `RequestParts::headers_mut` returns `&mut HeaderMap`.
- `HeadersAlreadyExtracted` has been removed.
- The `HeadersAlreadyExtracted` variant has been removed from these rejections:
- `RequestAlreadyExtracted`
- `RequestPartsAlreadyExtracted`
- `JsonRejection`
- `FormRejection`
- `ContentLengthLimitRejection`
- `WebSocketUpgradeRejection`
- `<HeaderMap as FromRequest<_>>::Rejection` has been changed to `std::convert::Infallible`.

- **breaking:** `axum::http::Extensions` is no longer an extractor (ie it
doesn't implement `FromRequest`). The `axum::extract::Extension` extractor is
_not_ impacted by this and works the same. This change makes it harder to
accidentally remove all extensions which would result in confusing errors
elsewhere ([#699])
This includes these breaking changes:
- `RequestParts::take_extensions` has been removed.
- `RequestParts::extensions` returns `&Extensions`.
- `RequestParts::extensions_mut` returns `&mut Extensions`.
- `RequestAlreadyExtracted` has been removed.
- `<Request as FromRequest>::Rejection` is now `BodyAlreadyExtracted`.
- `<http::request::Parts as FromRequest>::Rejection` is now `Infallible`.
- `ExtensionsAlreadyExtracted` has been removed.
- The `ExtensionsAlreadyExtracted` removed variant has been removed from these rejections:
- `ExtensionRejection`
- `PathRejection`
- `MatchedPathRejection`
- `WebSocketUpgradeRejection`
- `RequestParts::take_extensions` has been removed.
- `RequestParts::extensions` returns `&Extensions`.
- `RequestParts::extensions_mut` returns `&mut Extensions`.
- `RequestAlreadyExtracted` has been removed.
- `<Request as FromRequest>::Rejection` is now `BodyAlreadyExtracted`.
- `<http::request::Parts as FromRequest>::Rejection` is now `Infallible`.
- `ExtensionsAlreadyExtracted` has been removed.
- The `ExtensionsAlreadyExtracted` removed variant has been removed from these rejections:
- `ExtensionRejection`
- `PathRejection`
- `MatchedPathRejection`
- `WebSocketUpgradeRejection`
- **breaking:** `Redirect::found` has been removed ([#800])
- **breaking:** `AddExtensionLayer` has been removed. Use `Extension` instead. It now implements
`tower::Layer` ([#807])
Expand Down Expand Up @@ -1650,7 +1654,7 @@ Yanked, as it contained an accidental breaking change.
`async` ([#534])
- **added:** `HandleErrorLayer` now supports running extractors.
- **breaking:** The `Handler<B, T>` trait is now defined as `Handler<T, B =
Body>`. That is the type parameters have been swapped and `B` defaults to
Body>`. That is the type parameters have been swapped and `B` defaults to
`axum::body::Body` ([#527])
- **breaking:** `Router::merge` will panic if both routers have fallbacks.
Previously the left side fallback would be silently discarded ([#529])
Expand Down Expand Up @@ -1710,6 +1714,7 @@ Yanked, as it contained an accidental breaking change.
# 0.3.0 (02. November, 2021)

- Overall:

- **fixed:** All known compile time issues are resolved, including those with
`boxed` and those introduced by Rust 1.56 ([#404])
- **breaking:** The router's type is now always `Router` regardless of how many routes or
Expand All @@ -1725,6 +1730,7 @@ Yanked, as it contained an accidental breaking change.
)
}
```

- **breaking:** Added feature flags for HTTP1 and JSON. This enables removing a
few dependencies if your app only uses HTTP2 or doesn't use JSON. This is only a
breaking change if you depend on axum with `default_features = false`. ([#286])
Expand All @@ -1746,6 +1752,7 @@ Yanked, as it contained an accidental breaking change.
- **added:** Add `Handler::into_make_service_with_connect_info` for serving a
handler without a `Router`, and storing info about the incoming connection.
- **breaking:** axum's minimum supported rust version is now 1.56

- Routing:
- Big internal refactoring of routing leading to several improvements ([#363])
- **added:** Wildcard routes like `.route("/api/users/*rest", service)` are now supported.
Expand Down Expand Up @@ -1789,7 +1796,9 @@ Yanked, as it contained an accidental breaking change.
- **added:** Add `extract::MatchedPath` for accessing path in router that
matched the request ([#412])
- Error handling:
- **breaking:** Simplify error handling model ([#402]):
- All services part of the router are now required to be infallible.
- Error handling utilities have been moved to an `error_handling` module.
- `Router::check_infallible` has been removed since routers are always
Expand Down Expand Up @@ -1856,6 +1865,7 @@ Yanked, as it contained an accidental breaking change.
// ...
}
```
- Misc:
- `InvalidWebsocketVersionHeader` has been renamed to `InvalidWebSocketVersionHeader` ([#416])
- `WebsocketKeyHeaderMissing` has been renamed to `WebSocketKeyHeaderMissing` ([#416])
Expand Down Expand Up @@ -1935,6 +1945,7 @@ Yanked, as it contained an accidental breaking change.
please file an issue! ([#184](https://github.com/tokio-rs/axum/pull/184)) ([#198](https://github.com/tokio-rs/axum/pull/198)) ([#220](https://github.com/tokio-rs/axum/pull/220))
- **changed:** Remove `prelude`. Explicit imports are now required ([#195](https://github.com/tokio-rs/axum/pull/195))
- Routing:
- **added:** Add dedicated `Router` to replace the `RoutingDsl` trait ([#214](https://github.com/tokio-rs/axum/pull/214))
- **added:** Add `Router::or` for combining routes ([#108](https://github.com/tokio-rs/axum/pull/108))
- **fixed:** Support matching different HTTP methods for the same route that aren't defined
Expand Down Expand Up @@ -1989,6 +2000,7 @@ Yanked, as it contained an accidental breaking change.
.boxed()
}
```
- Extractors:
- **added:** Make `FromRequest` default to being generic over `body::Body` ([#146](https://github.com/tokio-rs/axum/pull/146))
- **added:** Implement `std::error::Error` for all rejections ([#153](https://github.com/tokio-rs/axum/pull/153))
Expand All @@ -2002,14 +2014,15 @@ Yanked, as it contained an accidental breaking change.
- **changed:** `extract::BodyStream` is no longer generic over the request body ([#234](https://github.com/tokio-rs/axum/pull/234))
- **changed:** `extract::Body` has been renamed to `extract::RawBody` to avoid conflicting with `body::Body` ([#233](https://github.com/tokio-rs/axum/pull/233))
- **changed:** `RequestParts` changes ([#153](https://github.com/tokio-rs/axum/pull/153))
- `method` new returns an `&http::Method`
- `method_mut` new returns an `&mut http::Method`
- `take_method` has been removed
- `uri` new returns an `&http::Uri`
- `uri_mut` new returns an `&mut http::Uri`
- `take_uri` has been removed
- `method` new returns an `&http::Method`
- `method_mut` new returns an `&mut http::Method`
- `take_method` has been removed
- `uri` new returns an `&http::Uri`
- `uri_mut` new returns an `&mut http::Uri`
- `take_uri` has been removed
- **changed:** Remove several rejection types that were no longer used ([#153](https://github.com/tokio-rs/axum/pull/153)) ([#154](https://github.com/tokio-rs/axum/pull/154))
- Responses:
- **added:** Add `Headers` for easily customizing headers on a response ([#193](https://github.com/tokio-rs/axum/pull/193))
- **added:** Add `Redirect` response ([#192](https://github.com/tokio-rs/axum/pull/192))
- **added:** Add `body::StreamBody` for easily responding with a stream of byte chunks ([#237](https://github.com/tokio-rs/axum/pull/237))
Expand All @@ -2019,6 +2032,7 @@ Yanked, as it contained an accidental breaking change.
- **changed:** `tower::util::Either` no longer implements `IntoResponse` ([#229](https://github.com/tokio-rs/axum/pull/229))
This `IntoResponse` from 0.1:
```rust
use axum::{http::Response, prelude::*, response::IntoResponse};
Expand All @@ -2032,6 +2046,7 @@ Yanked, as it contained an accidental breaking change.
```
Becomes this in 0.2:
```rust
use axum::{body::Body, http::Response, response::IntoResponse};
Expand All @@ -2046,11 +2061,14 @@ Yanked, as it contained an accidental breaking change.
}
}
```
- SSE:
- **added:** Add `response::sse::Sse`. This implements SSE using a response rather than a service ([#98](https://github.com/tokio-rs/axum/pull/98))
- **changed:** Remove `axum::sse`. It has been replaced by `axum::response::sse` ([#98](https://github.com/tokio-rs/axum/pull/98))
Handler using SSE in 0.1:
```rust
use axum::{
prelude::*,
Expand Down Expand Up @@ -2089,7 +2107,9 @@ Yanked, as it contained an accidental breaking change.
}),
);
```
- WebSockets:
- **changed:** Change WebSocket API to use an extractor plus a response ([#121](https://github.com/tokio-rs/axum/pull/121))
- **changed:** Make WebSocket `Message` an enum ([#116](https://github.com/tokio-rs/axum/pull/116))
- **changed:** `WebSocket` now uses `Error` as its error type ([#150](https://github.com/tokio-rs/axum/pull/150))
Expand Down Expand Up @@ -2128,6 +2148,7 @@ Yanked, as it contained an accidental breaking change.
}),
);
```
- Misc
- **added:** Add default feature `tower-log` which exposes `tower`'s `log` feature. ([#218](https://github.com/tokio-rs/axum/pull/218))
- **changed:** Replace `body::BoxStdError` with `axum::Error`, which supports downcasting ([#150](https://github.com/tokio-rs/axum/pull/150))
Expand Down
10 changes: 10 additions & 0 deletions axum/src/routing/path_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,26 @@ where
.expect("no path for route id. This is a bug in axum. Please file an issue");

let path = path_for_nested_route(prefix, inner_path);
let route_nested_root = !path.ends_with('/') && "/".eq(inner_path.as_ref());

let layer = (
StripPrefix::layer(prefix),
SetNestedPath::layer(path_to_nest_at),
);
match endpoint.layer(layer) {
Endpoint::MethodRouter(method_router) => {
if IS_FALLBACK && route_nested_root {
let path = format!("{path}/");
self.route(&path, method_router.clone())?;
}
self.route(&path, method_router)?;
}
Endpoint::Route(route) => {
if IS_FALLBACK && route_nested_root {
let path = format!("{path}/");
self.route_endpoint(&path, Endpoint::Route(route.clone()))?;
}

self.route_endpoint(&path, Endpoint::Route(route))?;
}
}
Expand Down
13 changes: 13 additions & 0 deletions axum/src/routing/tests/nest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,19 @@ async fn nesting_with_root_inner_router() {
assert_eq!(res.status(), StatusCode::OK);
}

#[tokio::test]
async fn nesting_with_root_inner_fallback() {
let app = Router::new().nest("/router", Router::new().fallback(get(|| async {})));

let client = TestClient::new(app);

let res = client.get("/router").await;
assert_eq!(res.status(), StatusCode::OK);

let res = client.get("/router/").await;
assert_eq!(res.status(), StatusCode::OK);
}

macro_rules! nested_route_test {
(
$name:ident,
Expand Down

0 comments on commit 6a4325c

Please sign in to comment.