#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
extern crate self as navree_openapi;
mod component;
mod document;
mod schema;
mod to_schema;
pub use component::ComponentProperties;
pub use document::{
Components, Document, Info, MediaType, Operation, Parameter, PathItem, RequestBody, Response,
SecurityScheme, Server, Tag,
};
#[doc(hidden)]
pub use schema::RustType;
pub use schema::{NotASchema, Schema, SchemaType, Subschema, Type};
pub use to_schema::{Object, ToSchema, components_of};
pub use navree_macros::ToSchema;
#[doc(hidden)]
pub mod __private {
use std::cell::RefCell;
use std::marker::PhantomData;
use serde::Serialize;
pub use serde_json;
use serde_json::{Map, Value};
use crate::Schema;
pub const COMPONENTS: &str = "x-navree-components";
pub const COMPONENT_TYPES: &str = "x-navree-component-types";
pub const COMPONENT_CONFLICTS: &str = "x-navree-component-conflicts";
pub fn attach_components(schema: &mut Value, referenced: Vec<(String, Schema)>) {
let Some(object) = schema.as_object_mut() else {
return;
};
if referenced.is_empty() {
return;
}
let mut components = Map::new();
let mut types = Map::new();
let mut conflicts = Vec::new();
let mut first_of: Vec<(String, Value, Option<&'static str>)> = Vec::new();
for (name, schema) in referenced {
let rust_type = schema.rust_type.0;
let value = schema.to_value();
match first_of.iter().find(|(first, _, _)| *first == name) {
Some((_, first, _)) if *first == value => {}
Some((_, _, first_type)) => {
conflicts.push(serde_json::json!([name, first_type, rust_type]));
}
None => {
if let Some(rust_type) = rust_type {
types.insert(name.clone(), Value::from(rust_type));
}
components.insert(name.clone(), value.clone());
first_of.push((name, value, rust_type));
}
}
}
object.insert(COMPONENTS.to_owned(), Value::Object(components));
if !types.is_empty() {
object.insert(COMPONENT_TYPES.to_owned(), Value::Object(types));
}
if !conflicts.is_empty() {
object.insert(COMPONENT_CONFLICTS.to_owned(), Value::Array(conflicts));
}
}
thread_local! {
static INLINING: RefCell<Vec<&'static str>> = const { RefCell::new(Vec::new()) };
}
pub struct InlineGuard;
impl InlineGuard {
pub fn enter(rust_type: &'static str) -> InlineGuard {
INLINING.with(|inlining| {
let mut inlining = inlining.borrow_mut();
if inlining.contains(&rust_type) {
let path = inlining.join(" -> ");
inlining.clear();
panic!(
"#[schema(inline)] on a field of `{rust_type}` makes it contain itself ({path} -> {rust_type}): remove `inline` from one of the fields"
);
}
inlining.push(rust_type);
});
InlineGuard
}
}
impl Drop for InlineGuard {
fn drop(&mut self) {
INLINING.with(|inlining| {
inlining.borrow_mut().pop();
});
}
}
pub struct DefaultOf<T>(PhantomData<T>);
impl<T> DefaultOf<T> {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
DefaultOf(PhantomData)
}
}
pub trait HasDefault<T> {
fn get(&self) -> Option<T>;
}
impl<T: Default> HasDefault<T> for DefaultOf<T> {
fn get(&self) -> Option<T> {
Some(T::default())
}
}
pub trait NoDefault<T> {
fn get(&self) -> Option<T> {
None
}
}
impl<T> NoDefault<T> for &DefaultOf<T> {}
pub struct JsonOf<'a, T: ?Sized>(pub &'a T);
pub trait HasJson {
fn get(&self) -> Option<Value>;
}
impl<T: Serialize + ?Sized> HasJson for JsonOf<'_, T> {
fn get(&self) -> Option<Value> {
Some(serde_json::to_value(self.0).unwrap_or_default())
}
}
pub trait NoJson {
fn get(&self) -> Option<Value> {
None
}
}
impl<T: ?Sized> NoJson for &JsonOf<'_, T> {}
}