use kynos_openapi::{
Document, Info, Method, Operation, PathItem, PathTemplate, Response, Responses, Schema,
SpecVersion, model::schema::types::SchemaType, validate::Validator,
};
fn list_orders() -> Operation {
Operation {
operation_id: Some("listOrders".to_owned()),
summary: Some("Lists orders".to_owned()),
tags: vec!["orders".to_owned()],
responses: Responses::new().with(
200,
Response::with_content(
"Every order the caller may see",
"application/json",
order_list(),
),
),
..Operation::default()
}
}
fn order_list() -> kynos_openapi::MediaType {
let mut items = Schema::of_type(SchemaType::Integer);
if let Schema::Object(object) = &mut items {
object.format = Some("uint64".to_owned());
object.minimum = Some(0.0);
}
let mut array = Schema::of_type(SchemaType::Array);
if let Schema::Object(object) = &mut array {
object.items = Some(Box::new(items));
}
kynos_openapi::MediaType::new(array)
}
fn search_orders() -> Operation {
Operation {
operation_id: Some("searchOrders".to_owned()),
responses: Responses::new().with(200, Response::new("Orders matching the filter")),
..Operation::default()
}
}
fn main() {
let mut document = Document::new(SpecVersion::V3_2, Info::new("Orders", "2.0.0"));
let orders = PathTemplate::parse("/orders").expect("a valid path template");
document.paths.insert(
&orders,
PathItem::new()
.with_operation(Method::Get, list_orders())
.with_operation(Method::Query, search_orders()),
);
let violations = Validator::new(SpecVersion::V3_2).validate(&document);
if violations.is_empty() {
println!("the description is valid at 3.2");
}
for violation in &violations {
println!("violation: {violation}");
}
println!("{}", document.to_json().expect("serializable"));
println!("{}", document.to_yaml().expect("serializable"));
match document.emit(SpecVersion::V3_1) {
Ok(_) => println!("unexpected: 3.1 accepted a QUERY operation"),
Err(error) => println!("3.1 refused, correctly: {error}"),
}
}