Question: Accessing `State` in `OpenApiFromRequest` Similar to `FromRequest`
- Dominant language
- Rust
- Stars
- 740
- Forks
- 127
- PR merge metrics
- No merged PRs in 30d
Description
Hello `rocket_okapi` team,
I'm currently working on the `zitadel-rust` project and generating OpenAPI documentation using the `OpenApiFromRequest` trait. You can find the relevant PR here: [smartive/zitadel-rust#559](https://github.com/smartive/zitadel-rust/pull/559).
In this project, I need to configure the `SecurityScheme` for OpenID Connect, and the authority URL for this configuration is stored in Rocket's `State`.
In a typical request guard implementation using `FromRequest`, I can access the `request` object and retrieve the configuration from `State` like this:
```rust
#[async_trait]
impl<'request> FromRequest<'request> for &'request IntrospectedUser {
type Error = &'request IntrospectionGuardError;
async fn from_request(request: &'request Request<'_>) -> Outcome {
// Accessing the authorization header
let auth: Vec<_> = request.headers().get("authorization").collect();
if auth.len() > 1 {
return Outcome::Error((Status::BadRequest, &IntrospectionGuardError::InvalidHeader));
} else if auth is_empty() {
return Outcome::Error((Status::Unauthorized, &IntrospectionGuardError::Unauthorized));
}
let token = auth[0];
if !token.starts_with("Bearer ") {
return Outcome::Error((Status::Unauthorized, &IntrospectionGuardError::WrongScheme));
}
// Accessing the configuration from State
let config = request.rocket().state::();
if config is_none() {
return Outcome::Error((Status::InternalServerError, &IntrospectionGuardError::MissingConfig));
}
// Further processing...
}
}
```
However, when working within the `OpenApiFromRequest` trait, I haven't found a way to similarly access the `request` object or `State` to retrieve the configuration data. Currently, I'm using `figment` to extract the configuration, like this:
```rust
let config: IntrospectionRocketConfig = figment
.extract()
.expect("authority must be set in Rocket.toml");
```
But I would prefer to directly access the `State` via the `request`, similar to how it's done in `FromRequest`, to keep consistency and avoid duplicating configuration extraction logic.
### My Question:
Is it possible to access the `request` object within `OpenApiFromRequest`, or is there another recommended way to retrieve the `State` while generating OpenAPI documentation with `rocket_okapi`?
Thank you for your assistance!
Contributor guide
Assessment
This issue has not been assessed yet.