-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retrieve OpenApi object after generating #28
Comments
Out of curiosity, what's the chance that this (or something like it) could be applied in a My ultimate goal is to do codegen on client libraries (Javascript, Python, Rust) that will be calling the API, where the Rust Rocket code will be the source of truth. The Javascript case in particular is a bit circular; the JS FE code should be dependent on the Rust BE source of truth, but the BE will ultimately need to be linked to the FE code (so that it can be served). The FE isn't in the binary, so it's doable even if I can't get the spec as part of the build step, but I am trying to determine the minimum dependency path for both dev and production cases. |
@nathanielford I'm not quite sure what you mean. In regards to the frontend/backend a very common thing is to create a backend in Rust (in this case) with Rocket and expose a (REST) API. This api is then used in JS to populate webpages with data. This means separations of backend and frontend. I use the okapi to generate the docs for the API so they can be viewed by the frontend developers and okapi helps with keeping them up to date at all times. (here you can find documentation of my api: https://docs.dfstoryteller.com/rapidoc/ ) I hope this answers your question. If not, could you explain it a bit more what you are looking for? |
Mostly I'm looking to get an artifact that openapi codegen libraries can use, while minimizing the dependency tree. The goal of the codegen is to have functions created in the client-side applications that are already fully in line with api spec. This makes a class of errors apparent during development rather than a later testing stage. In this case, the All that said, in a strictly 'is this doable' sense I think you've answered my question: yes. It also seems like the axios library forgoes explicit codegen for a client object that has it all wrapped inside, which is something I did not know yesterday. In that sense, doing this in the build step may be unneeded, at least for javascript, and so that is the road I'm presently taking. I appreciate your time! |
TL;DR: Yes it is possible, just call the function inside your With the code in my first post you can do something like this: let openapi = api::get_openapi(); // see code above
api::write_json_file(output, &openapi).ok(); And the write function (if need be): /// Write A Json object to a file
pub fn write_json_file<P: AsRef<std::path::Path>, C: Serialize>(
filename: P,
object: &C,
) -> Result<(), Error> {
let mut file = File::create(filename)?;
let json_string = serde_json::to_string_pretty(object)?;
file.write_all(json_string.as_bytes())?;
Ok(())
} You can most likely just call this in the To not duplicate the code you have to create a new function something like this: fn get_spec_and_routes() -> (OpenApi, Vec<Route>) {
let (spec, routes) = routes_and_spec_with_openapi![
//... all routes here...
];
(spec, routes)
} I do this here: https://gitlab.com/df_storyteller/df-storyteller/-/blob/0c9e609f4c2e9723c799a085020425c9be71fa52/df_st_api/src/api/mod.rs#L42 I changed rocket_okapi-codegen to do this: https://gitlab.com/df_storyteller/df-storyteller/-/blob/0c9e609f4c2e9723c799a085020425c9be71fa52/df_rocket_okapi_codegen/src/lib.rs This might be added into the repo itself at some point. But is not there right now. It is not strait forward, but using my code as an example you might be able to figure it out. (just follow the function calls) (To get started you might be able to use: https://crates.io/crates/df_rocket_okapi_codegen , But this is custom and will be deprecated at some point when changes are in |
Getting the OpenApi object is now very easy in okapi/rocket-okapi without needing to jump to hoops. You can use the |
Currently there is no easy way to get the OpenApi object or the generated file without starting rocket.
I made a small (hacky) workaround to get the
OpenApi
object without starting rocket.Here are some code snippets if someone has the same issue. But it would be code if this was supported in the crate.
Somewhere in my code:
Rocket_okapi_codegen/src/lib.rs
Rocket_okapi_codegen/src/routes_with_openapi/mod.rs
Like I said this is a bit of a hack. But a native way would be nice.
The text was updated successfully, but these errors were encountered: