use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Schema {
Boolean(bool),
Ref(RefSchema),
Enum(EnumSchema),
OneOf(OneOfSchema),
AnyOf(AnyOfSchema),
#[serde(serialize_with = "serialize_array_schema")]
Array(ArraySchema),
#[serde(serialize_with = "serialize_primitive_schema")]
Primitive(PrimitiveSchema),
#[serde(serialize_with = "serialize_object_schema")]
Object(ObjectSchema),
}
#[derive(Serialize)]
struct TypedSchema<'a, T> {
#[serde(rename = "type")]
kind: &'static str,
#[serde(flatten)]
schema: &'a T,
}
fn serialize_primitive_schema<S: serde::Serializer>(
schema: &PrimitiveSchema,
serializer: S,
) -> Result<S::Ok, S::Error> {
if schema.nullable {
let mut non_null = schema.clone();
non_null.nullable = false;
Schema::any_of(vec![Schema::Primitive(non_null), <()>::schema()]).serialize(serializer)
} else {
schema.serialize(serializer)
}
}
fn serialize_object_schema<S: serde::Serializer>(
schema: &ObjectSchema,
serializer: S,
) -> Result<S::Ok, S::Error> {
TypedSchema {
kind: "object",
schema,
}
.serialize(serializer)
}
fn serialize_array_schema<S: serde::Serializer>(
schema: &ArraySchema,
serializer: S,
) -> Result<S::Ok, S::Error> {
TypedSchema {
kind: "array",
schema,
}
.serialize(serializer)
}
impl Schema {
pub fn string() -> Self {
Schema::Primitive(PrimitiveSchema::string())
}
pub fn integer(format: Option<&str>) -> Self {
Schema::Primitive(PrimitiveSchema::integer(format))
}
pub fn number(format: Option<&str>) -> Self {
Schema::Primitive(PrimitiveSchema::number(format))
}
pub fn boolean() -> Self {
Schema::Primitive(PrimitiveSchema::boolean())
}
pub fn reference(name: &str) -> Self {
Schema::Ref(RefSchema {
reference: format!("#/components/schemas/{name}"),
})
}
pub fn array(items: Schema) -> Self {
Schema::Array(ArraySchema {
items: Box::new(items),
min_items: None,
max_items: None,
})
}
pub fn object(properties: HashMap<String, Schema>, required: Vec<String>) -> Self {
Schema::Object(ObjectSchema {
title: None,
description: None,
properties,
required,
additional_properties: None,
})
}
#[must_use]
pub fn nullable(mut self) -> Self {
if let Schema::Primitive(ref mut p) = self {
p.nullable = true;
self
} else {
Self::any_of(vec![self, <()>::schema()])
}
}
#[must_use]
pub fn with_title(mut self, title: impl Into<String>) -> Self {
if let Schema::Object(ref mut o) = self {
o.title = Some(title.into());
}
self
}
#[must_use]
pub fn with_description(mut self, description: impl Into<String>) -> Self {
if let Schema::Object(ref mut o) = self {
o.description = Some(description.into());
}
self
}
pub fn string_enum(values: Vec<String>) -> Self {
Schema::Enum(EnumSchema {
schema_type: SchemaType::String,
enum_values: values,
})
}
pub fn one_of(schemas: Vec<Schema>) -> Self {
Schema::OneOf(OneOfSchema { one_of: schemas })
}
pub fn any_of(schemas: Vec<Schema>) -> Self {
Schema::AnyOf(AnyOfSchema { any_of: schemas })
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefSchema {
#[serde(rename = "$ref")]
pub reference: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ObjectSchema {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub properties: HashMap<String, Schema>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub required: Vec<String>,
#[serde(
rename = "additionalProperties",
default,
skip_serializing_if = "Option::is_none"
)]
pub additional_properties: Option<Box<Schema>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArraySchema {
pub items: Box<Schema>,
#[serde(rename = "minItems", default, skip_serializing_if = "Option::is_none")]
pub min_items: Option<usize>,
#[serde(rename = "maxItems", default, skip_serializing_if = "Option::is_none")]
pub max_items: Option<usize>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnumSchema {
#[serde(rename = "type")]
pub schema_type: SchemaType,
#[serde(rename = "enum")]
pub enum_values: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OneOfSchema {
#[serde(rename = "oneOf")]
pub one_of: Vec<Schema>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnyOfSchema {
#[serde(rename = "anyOf")]
pub any_of: Vec<Schema>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrimitiveSchema {
#[serde(rename = "type")]
pub schema_type: SchemaType,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub format: Option<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub nullable: bool,
}
impl PrimitiveSchema {
pub fn string() -> Self {
Self {
schema_type: SchemaType::String,
format: None,
nullable: false,
}
}
pub fn integer(format: Option<&str>) -> Self {
Self {
schema_type: SchemaType::Integer,
format: format.map(String::from),
nullable: false,
}
}
pub fn number(format: Option<&str>) -> Self {
Self {
schema_type: SchemaType::Number,
format: format.map(String::from),
nullable: false,
}
}
pub fn boolean() -> Self {
Self {
schema_type: SchemaType::Boolean,
format: None,
nullable: false,
}
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_false(b: &bool) -> bool {
!*b
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SchemaType {
String,
Number,
Integer,
Boolean,
Null,
}
pub trait JsonSchema {
fn schema() -> Schema;
#[must_use]
fn schema_name() -> Option<&'static str> {
None
}
}
impl JsonSchema for String {
fn schema() -> Schema {
Schema::Primitive(PrimitiveSchema {
schema_type: SchemaType::String,
format: None,
nullable: false,
})
}
}
impl JsonSchema for i64 {
fn schema() -> Schema {
Schema::Primitive(PrimitiveSchema {
schema_type: SchemaType::Integer,
format: Some("int64".to_string()),
nullable: false,
})
}
}
impl JsonSchema for i32 {
fn schema() -> Schema {
Schema::Primitive(PrimitiveSchema {
schema_type: SchemaType::Integer,
format: Some("int32".to_string()),
nullable: false,
})
}
}
impl JsonSchema for f64 {
fn schema() -> Schema {
Schema::Primitive(PrimitiveSchema {
schema_type: SchemaType::Number,
format: Some("double".to_string()),
nullable: false,
})
}
}
impl JsonSchema for bool {
fn schema() -> Schema {
Schema::Primitive(PrimitiveSchema {
schema_type: SchemaType::Boolean,
format: None,
nullable: false,
})
}
}
macro_rules! integer_schema {
($ty:ty, $format:expr) => {
impl JsonSchema for $ty {
fn schema() -> Schema {
Schema::integer($format)
}
}
};
}
integer_schema!(i8, Some("int8"));
integer_schema!(i16, Some("int16"));
integer_schema!(u8, Some("uint8"));
integer_schema!(u16, Some("uint16"));
integer_schema!(u32, Some("uint32"));
integer_schema!(u64, Some("uint64"));
integer_schema!(isize, Some("int64"));
integer_schema!(usize, Some("uint64"));
integer_schema!(i128, None);
integer_schema!(u128, None);
impl JsonSchema for f32 {
fn schema() -> Schema {
Schema::number(Some("float"))
}
}
impl JsonSchema for &str {
fn schema() -> Schema {
Schema::string()
}
}
impl JsonSchema for () {
fn schema() -> Schema {
Schema::Primitive(PrimitiveSchema {
schema_type: SchemaType::Null,
format: None,
nullable: false,
})
}
}
impl JsonSchema for serde_json::Value {
fn schema() -> Schema {
Schema::Boolean(true)
}
}
impl<T: JsonSchema, S: std::hash::BuildHasher> JsonSchema for HashMap<String, T, S> {
fn schema() -> Schema {
Schema::Object(ObjectSchema {
additional_properties: Some(Box::new(T::schema())),
..ObjectSchema::default()
})
}
}
impl<T: JsonSchema> JsonSchema for std::collections::BTreeMap<String, T> {
fn schema() -> Schema {
<HashMap<String, T>>::schema()
}
}
impl<T: JsonSchema> JsonSchema for Option<T> {
fn schema() -> Schema {
T::schema().nullable()
}
}
impl<T: JsonSchema> JsonSchema for Vec<T> {
fn schema() -> Schema {
Schema::Array(ArraySchema {
items: Box::new(T::schema()),
min_items: None,
max_items: None,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bounded_array_uses_json_schema_keywords_and_round_trips() {
let schema = Schema::Array(ArraySchema {
items: Box::new(Schema::integer(None)),
min_items: Some(2),
max_items: Some(4),
});
let json = serde_json::to_value(schema).unwrap();
assert_eq!(json["type"], "array");
assert_eq!(json["minItems"], 2);
assert_eq!(json["maxItems"], 4);
assert!(json["min_items"].is_null());
assert!(json["max_items"].is_null());
let decoded: Schema = serde_json::from_value(json.clone()).unwrap();
assert!(
matches!(&decoded, Schema::Array(array) if array.min_items == Some(2) && array.max_items == Some(4))
);
assert_eq!(serde_json::to_value(decoded).unwrap(), json);
}
#[test]
fn optional_array_schema_accepts_real_null_and_retains_item_type() {
let json = serde_json::to_value(<Option<Vec<String>>>::schema()).unwrap();
assert_eq!(json["anyOf"][0]["type"], "array");
assert_eq!(json["anyOf"][0]["items"]["type"], "string");
assert_eq!(json["anyOf"][1]["type"], "null");
let decoded: Schema = serde_json::from_value(json.clone()).unwrap();
assert!(matches!(&decoded, Schema::AnyOf(_)));
assert_eq!(serde_json::to_value(decoded).unwrap(), json);
}
#[test]
fn nullable_unions_allow_overlapping_null_branches() {
let any_json = serde_json::to_value(<Option<serde_json::Value>>::schema()).unwrap();
assert_eq!(any_json["anyOf"][0], true);
assert_eq!(any_json["anyOf"][1]["type"], "null");
assert!(any_json["oneOf"].is_null());
let nested = serde_json::to_value(<Option<Option<Vec<String>>>>::schema()).unwrap();
assert_eq!(nested["anyOf"][0]["anyOf"][1]["type"], "null");
assert_eq!(nested["anyOf"][1]["type"], "null");
let null = serde_json::to_value(<Option<()>>::schema()).unwrap();
assert_eq!(null["anyOf"][0]["type"], "null");
assert_eq!(null["anyOf"][1]["type"], "null");
}
}