diff --git a/crates/oapi-macros/src/schema_type.rs b/crates/oapi-macros/src/schema_type.rs index 54fb3f56c..ba04a7cdb 100644 --- a/crates/oapi-macros/src/schema_type.rs +++ b/crates/oapi-macros/src/schema_type.rs @@ -193,6 +193,8 @@ fn is_primitive(name: &str) -> bool { | "i128" | "f32" | "f64" + | "Ipv4Addr" + | "Ipv6Addr" ) } @@ -260,6 +262,9 @@ impl TryToTokens for SchemaType<'_> { "PrimitiveDateTime" | "OffsetDateTime" => { schema_type_tokens(tokens, oapi, SchemaTypeInner::String, self.nullable) } + "Ipv4Addr" | "Ipv6Addr" | "IpAddr" => { + schema_type_tokens(tokens, oapi, SchemaTypeInner::String, self.nullable) + } _ => schema_type_tokens(tokens, oapi, SchemaTypeInner::Object, self.nullable), }; Ok(()) @@ -385,7 +390,7 @@ impl Type<'_> { fn is_known_format(name: &str) -> bool { matches!( name, - "i8" | "i16" | "i32" | "u8" | "u16" | "u32" | "i64" | "u64" | "f32" | "f64" + "i8" | "i16" | "i32" | "u8" | "u16" | "u32" | "i64" | "u64" | "f32" | "f64" | "Ipv4Addr" | "Ipv6Addr" ) } @@ -456,6 +461,12 @@ impl TryToTokens for Type<'_> { #[cfg(feature = "time")] "PrimitiveDateTime" | "OffsetDateTime" => { tokens.extend(quote! { #oapi::oapi::SchemaFormat::KnownFormat(#oapi::oapi::KnownFormat::DateTime) }) + }, + "Ipv4Addr" => { + tokens.extend(quote! { #oapi::oapi::SchemaFormat::KnownFormat(#oapi::oapi::KnownFormat::Ipv4) }) + }, + "Ipv6Addr" => { + tokens.extend(quote! { #oapi::oapi::SchemaFormat::KnownFormat(#oapi::oapi::KnownFormat::Ipv6) }) } _ => (), }; diff --git a/crates/oapi/src/lib.rs b/crates/oapi/src/lib.rs index f8473c2f6..077d41598 100644 --- a/crates/oapi/src/lib.rs +++ b/crates/oapi/src/lib.rs @@ -191,6 +191,19 @@ impl_to_schema_primitive!( ); impl_to_schema!(&str); +impl_to_schema!(std::net::Ipv4Addr); +impl_to_schema!(std::net::Ipv6Addr); + +impl ToSchema for std::net::IpAddr { + fn to_schema(components: &mut Components) -> RefOr { + crate::RefOr::Type(Schema::OneOf( + OneOf::default() + .item(std::net::Ipv4Addr::to_schema(components)) + .item(std::net::Ipv6Addr::to_schema(components)), + )) + } +} + #[cfg(feature = "chrono")] impl_to_schema_primitive!(chrono::NaiveDate, chrono::Duration, chrono::NaiveDateTime); #[cfg(feature = "chrono")]