juhaku / utoipa

Simple, Fast, Code first and Compile time generated OpenAPI documentation for Rust
Apache License 2.0
2.48k stars 197 forks source link

actix-web with `ResourceFiles` #1173

Closed ctron closed 3 weeks ago

ctron commented 3 weeks ago

I am trying utoipa_actix_web with ResourceFiles, and to struggle a bit. Here is the snippet:

use actix_web_static_files::resource_files::ResourceFiles;

pub fn configure(config: &mut utoipa_actix_web::service_config::ServiceConfig, ui: &UiResources) {
    config.service(ResourceFiles::new("/", ui.resources()).resolve_not_found_to(""));
}

I can't add ResourceFiles with service, because it doesn't implement OpenApiFactory. Makes sense. But how do I work around this?

It's ok to just leave this out of the OpenAPI spec.

ctron commented 3 weeks ago

I came up with this:


pub fn configure(config: &mut utoipa_actix_web::service_config::ServiceConfig, ui: &UiResources) {
    config.service(NoSpecWrapper(
        ResourceFiles::new("/", ui.resources()).resolve_not_found_to(""),
    ));
}

struct NoSpecWrapper<T: HttpServiceFactory>(pub T);

impl<T: HttpServiceFactory> HttpServiceFactory for NoSpecWrapper<T> {
    fn register(self, config: &mut AppService) {
        self.0.register(config);
    }
}
impl<T: HttpServiceFactory> OpenApiFactory for NoSpecWrapper<T> {
    fn paths(&self) -> utoipa::openapi::path::Paths {
        Default::default()
    }

    fn schemas(
        &self,
        _schemas: &mut Vec<(
            String,
            utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
        )>,
    ) {
        Default::default()
    }
}

Not sure that works, need to test. But I think this should be somehow part of the crate.

juhaku commented 3 weeks ago

Yeah, I have totally ignored that it would be useful for ServiceConfig to also have direct access to actix_web::web::ServiceConfig :facepalm:

I shortly fix this with PR and make a release for it.

juhaku commented 3 weeks ago

This PR #1174 Adds similar map method for ServiceConfig that is present in Scope and UtoipaApp which allows direct access to underlying actix_web type which in this case is actix_web::web::ServiceConfig.