GREsau/okapi

Using `okapi` with Templates?

Glitchy-Tozier opened this issue · 6 comments

When trying to use Rocket's Templates AND use okapi, rust complains about OpenApiResponderInner being implemented for Template. This is the error I get:

the trait bound `rocket_dyn_templates::Template: rocket_okapi::response::OpenApiResponderInner` is not satisfied
required because of the requirements on the impl of `rocket_okapi::response::OpenApiResponderInner` for `std::result::Result<rocket_dyn_templates::Template, errors::ApiError>`
required because of the requirements on the impl of `rocket_okapi::response::OpenApiResponder<'_, '_>` for `std::result::Result<rocket_dyn_templates::Template, errors::ApiError>`

However, when trying to implement OpenApiResponderInner error-messages still get created. What are users supposed to do in this case?

impl OpenApiResponderInner for Template {
    fn responses(gen: &mut OpenApiGenerator) -> rocket_okapi::Result<okapi::openapi3::Responses> {
        todo!()
    }
}
only traits defined in the current crate can be implemented for arbitrary types
define and implement a trait or new type instead

Thanks for reporting this.
It will be fixed in the next release (currently in master).
I also created an example, both for testing and as an example. Hope that helps.

You can use the master branch if you want, but release should be out soon.

rocket_okapi = { git = "https://github.com/GREsau/okapi", features = ["..."] }

You're a hero, thank you so much for the quick implementation! ❤️
I used the master-branch and everything worked out perfectly.


Somewhat off-topic:
In case you're doing the versioning, keep in mind that the next update adds some breaking changes. See what I had to do to my errors.rs-file to make it work again. (Basically, openapi3::Responses moved.)

You're a hero, thank you so much for the quick implementation! heart I used the master-branch and everything worked out perfectly.

Haha, you just happen to be in luck that I had time today to fix a bunch of issues in the crate. And make sure everything works with new Rocket version.

Somewhat off-topic: In case you're doing the versioning, keep in mind that the next update adds some breaking changes. See what I had to do to my errors.rs-file to make it work again. (Basically, openapi3::Responses moved.)

I just had a look, but openapi3::Responses has not been moved. This has always been part of okapi but rocket_okapi re-export is.
So both will be the same.

use rocket_okapi::okapi::openapi3::Responses;
use okapi::openapi3::Responses;

Of course rocket_okapi and okapi have to use the same version.
This done so most people don't have to have a direct dependency on okapi, but only on rocket_okapi. As shown here:
https://github.com/GREsau/okapi/blob/master/examples/dyn_templates/Cargo.toml for example. There is no okapi dependency.

But you could add okapi = "0.7.0-rc.1" and then use use okapi::openapi3::Responses; and all will be the same.

I know this is confusing.
I know there are other breaking changes, because Rocket had some breaking changes.
Soon I'll bump the version, but want to check out all other issues and see what can be closed/improved/merged/...

Interesting. I had used the okapi-dependency as I assumed it was required! It's strange that Rust still insisted that I move the dependency, forcing me to use the re-export.

Anyway, thank you for pointing out that okapi is not mandatory. I was really confused by that naming scheme and "requiring" two imports from the same repo.

It's strange that Rust still insisted that I move the dependency, forcing me to use the re-export.

That is probably because you imported a slightly different version of okapi then rocket_okapi used. Rust does not see types with the same name, but different versions as the same thing, but as totally different types.
That is sometimes the reason for weird errors. (so keep that in mind you might see them at some point 😅 )

Got it! 😁👍🏻