Closed simongoricar closed 4 months ago
Now that I think about it, I think it's possible to use Cow<'a, str>
's instead of 'static
s. This would eliminate breaking changes as the three lifetimes would still be present on RapiDoc
(one for each field, as before) and we could still pass in owned strings.
Let me know if you'd prefer manual lifetimes instead of 'static
s. It might make sense to refactor other UI crates to the same idea if we go that route, though.
I think this is just fine with the 'static
unless there is someone needing the manual lifetimes. Manual lifetimes might sometimes be quite annoying.
Nice update, probably something that should be done for the Redoc if not already there. 👍 But that is another thing for another time. Thanks 🙂
Sure, why not. I'll submit another PR :)
Problem
As far as I can tell, it was previously impossible to dynamically construct URLs or custom HTML in certain cases. This was due to
RapiDoc
'spath
,spec_url
andhtml
fields being&'_ str
.Not bad in principle of course, but a problem arose when trying to use this in combination with
actix_web
(possibly other integrations, but I haven't tried).actix_web
'sHttpServer::new
requires that you pass a factory closure with the following conditions:Fn() -> I + Send + Clone + 'static
, and inside of that closure we need to initializeRapiDoc
. As such, the issue is that we can't dynamically construct a'static
string in these conditions (excluding quite terrible practices).For example, this would not compile beforehand (in this case due to
temporary value dropped while borrowed
):Solution
Instead of having
RapiDoc
's methods take&'_ str
, I modified them to takeInto<Cow<'static, str>>
and modified thepath
,spec_url
andhtml
fields to beCow<'static, str>
. This is also how this is handled in the adjacentutoipa_swagger_ui
crate.This example with a dynamic URL now compiles:
Breaking changes
As far as the methods go, I don't think this has any breaking changes due to the
Into<Cow<'static, str>>
generic. However, I think the removal of the three lifetime parameters onRapiDoc
can break some code.