DioxusLabs / DioxusLabs/dioxus
Compile time check for body in `GET` request on server fn macro
- Dominant language
- Rust
- Stars
- 39.1k
- Forks
- 1.9k
- Avg merge
- 4d 10h
- Merged PRs (30d)
- 4
Description
## Feature Request
Add compile-time validation for server function parameters to ensure `GET` requests don't have parameters missing from the route definition. Currently, this compiles but fails at runtime:
```rust
#[get("/users/{id}")]
async fn get_user(id: String, role: String) -> Result {
// `role` not in route - will fail when browser navigates to /users/123
Ok(User { id, role })
}
```
Per [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html#section-9.3.1), `GET` requests should not have request bodies and this fails on chromium browsers.
## Implement Suggestion
I tried to implement this via method-specific diagnostic traits using `#[diagnostic::on_unimplemented]`:
```rust
#[diagnostic::on_unimplemented(message = "GET requests cannot have body parameters per RFC 9110...")]
pub trait AssertGetNoBodyParams {}
impl> AssertGetNoBodyParams for T {}
```
to generate compile-time assertions for each body param on restricted methods. This didn't work because I don't think we can distinguish `FromRequest` extractors (valid) from `Deserialize` body params (invalid) at proc-macro time. Current implementation seems to intentionally supports `GET` with JSON bodies.
https://github.com/DioxusLabs/dioxus/blob/e03d8ff215ce9c617cc6e8661565609765299071/packages/fullstack/tests/compile-test.rs#L105-L109
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.