Comments (5)
Getting the OpenApi object is now very easy in okapi/rocket-okapi without needing to jump to hoops.
You can use the openapi_get_routes_spec
or openapi_get_spec
macro depending on your needs.
See this example for more info: https://github.com/GREsau/okapi/tree/master/examples/custom_schema
from okapi.
Out of curiosity, what's the chance that this (or something like it) could be applied in a build.rs
build script? I am still pretty new to Rust and, since I am not quite able to parse out all of what okapi is doing to generate the file, I suspect that full compilation might be needed for the api spec to be created (specifically because custom data type de/serialization would need to be computed)? And thus would be difficult to do in the build step - but perhaps I'm missing something?
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.
from okapi.
@nathanielford I'm not quite sure what you mean.
For the build.rs
I'm not sure how this works. But I thinks you should be able to get the datastructure or openapi.json file in the build script with the code above. Okapi generates the datastructured during compilation. It uses derive macros and other macros for this. (it basically generates code during compilation and then compiles that)
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.
Do you want to dynamically generate JS using the Rust build.rs script or what are you asking?
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/ )
The frontend is totally independent of the backend code (and is written in JS mostly).
Here you can find a very simple example of how JS code could look like: https://gitlab.com/df_storyteller/df-storyteller-sketches/-/blob/master/Simple%20list/index.html
I hope this answers your question. If not, could you explain it a bit more what you are looking for?
from okapi.
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 openapi.json
file that gets generated is the artifact I'm looking for - it's just not available unless you're running the web application (per your original comment). So, if I'm running yarn build
or yarn build-with-codegen
(or whatever script I'm using to properly prepare my js files), and that requires the openapi.json
file, I need to currently build and start the rocket server, make a fake call to it and save it out. That's a lot of lifting for a file that ought to be generate-able based on source alone. Thus, I was looking at build.rs
(which runs by default when you build) to see if I can short circuit doing a full compilation of the rocket server artifact. The Little Book of Rust Macros at least seems to suggest the rust compilation is structured in such a way that this is possible, but I will have to learn a lot more about macros to convince myself of that - hence reaching out.
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!
from okapi.
TL;DR: Yes it is possible, just call the function inside your build.rs
to get the structure (note that it has to be library crate in that case, not a binary crate). You however need to modify some crates to do this. As it is not in the main repo at the moment.
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 build.rs
you can use the file or just the data structures.
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.
The code is MIT licensed in the https://gitlab.com/df_storyteller/df-storyteller/-/tree/master/df_rocket_okapi_codegen crate. so feel free to use it. It has some additional changes to, so take a look before using it.
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)
If you have questions, let me know!
(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 rocket_okapi_codegen
, so for testing this is okay, but otherwise fork it or create your own version. )
from okapi.
Related Issues (20)
- openapi doesn't ignore Rocket request guards? HOT 2
- Build with latest commit failed HOT 2
- Add support for persist-auth HOT 2
- How to impl JsonSchema for chrono::DateTime<Utc> or other lib struct? HOT 1
- Publishing updated versions to crates.io HOT 2
- Incorrect openapi.json Generated for Response with Tuple HOT 2
- Enforce strict key values for BTreeMap with enum key HOT 5
- Unable to borrow value returned from definitions_mut() function HOT 1
- Update rocket dependency to v0.5.0-rc.4 HOT 2
- OpenAPI support for FromForm for MsgPack and TempFile
- (waiting for) Impl JsonSchema for Date/Time
- When 0.8 release HOT 5
- Add support for OpenAPI 3.1.0
- Nested routes? HOT 3
- How to specify image response content-type? HOT 1
- How to document handlers with rocket_oauth2 structs HOT 1
- [FEATURE]: Support for Validated<T> implementation . HOT 2
- Reference object and nullable HOT 1
- Derive OpenApiResponderInner for #[derive(Responder)] enums HOT 1
- Request guard, impl OpenApiFromRequest with Bearer HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from okapi.