-
Notifications
You must be signed in to change notification settings - Fork 102
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
How to add custom paths #82
Comments
You are already on the right track. fn custom_openapi_spec() -> OpenApi {
use indexmap::indexmap;
use rocket_okapi::okapi::openapi3::*;
use rocket_okapi::okapi::schemars::schema::*;
OpenApi {
openapi: OpenApi::default_version(),
info: Info {
title: "The best API ever".to_owned(),
description: Some("This is the best API every, please use me!".to_owned()),
version: "1.2.3".to_owned(),
..Default::default()
},
servers: vec![Server {
url: "http://127.0.0.1:8000/".to_owned(),
description: Some("Localhost".to_owned()),
..Default::default()
}],
// Add paths that do not exist in Rocket (or add extra into to existing paths)
paths: {
indexmap! {
"home".to_owned() => PathItem{
get: Some(
Operation {
tags: vec!["HomePage".to_owned()],
summary: Some("This is my homepage".to_owned()),
responses: Responses{
responses: indexmap!{
"200".to_owned() => RefOr::Object(
Response{
description: "Return the page, no error.".to_owned(),
content: indexmap!{
"text/html".to_owned() => MediaType{
schema: Some(SchemaObject{
instance_type: Some(SingleOrVec::Single(Box::new(
InstanceType::String
))),
..Default::default()
}),
..Default::default()
}
},
..Default::default()
}
)
},
..Default::default()
},
..Default::default()
}
),
..Default::default()
}
}
},
..Default::default()
}
} The easiest way to figure out the structure is to look at the docs.
#[cfg(not(feature = "preserve_order"))]
pub type Map<K, V> = std::collections::BTreeMap<K, V>;
#[cfg(feature = "preserve_order")]
pub type Map<K, V> = indexmap::IndexMap<K, V>; Source: https://github.com/GREsau/schemars/blob/9464118c3a3894c95037b3e25cb3ac9116eaf849/schemars/src/lib.rs#L292-L295 This is basically a 1 to 1 mapping between the json output and the structure (except for some small exceptions). I hope this helps, if you have more questions let me know. I also add this code to the example for future reference: okapi/examples/custom_schema/src/main.rs Lines 100 to 136 in 9a57d2f
|
Code example solves this issue. Closing issue. |
I have a working example with one route and custom schema:
I am looking for a way to add paths into my schema, so I don't necessarily depend on the structs provided by the app.
Cant see the way, and there are no examples in the library: any help will be welcome.
The text was updated successfully, but these errors were encountered: