Skip to content

Commit

Permalink
add openapi schema for ipaddr, macaddr
Browse files Browse the repository at this point in the history
  • Loading branch information
qpalzmqaz123 committed Apr 23, 2024
1 parent c756476 commit 325f1d9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions rnest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ log = "0.4"
actix-web = { version = "4", default-features = false }
serde_json = { version = "1", default-features = false }
actix-web-validator = { version = "5", default-features = false }
eui48 = { version = "1", default-features = false, features = ["serde", "disp_hexstring"], optional = true }

[dev-dependencies]
env_logger = "0.9"
serde = { version = "1" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
validator = { version = "0.16", features = ["derive"] }

[features]
default = ["openapi_mac_str", "openapi_ip_str"]
openapi_mac_str = ["eui48"]
openapi_ip_str = []
76 changes: 76 additions & 0 deletions rnest/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,82 @@ impl_schema_for_boolean! {AtomicBool}
impl_schema_for_string! {&str}
impl_schema_for_string! {String}

#[cfg(feature = "openapi_ip_str")]
impl OpenApiSchema for std::net::IpAddr {
fn get_schema() -> serde_json::Value {
serde_json::json!({
"type": "string",
"example": "127.0.0.1 | ::1",
})
}
}

#[cfg(feature = "openapi_ip_str")]
impl OpenApiSchema for std::net::Ipv4Addr {
fn get_schema() -> serde_json::Value {
serde_json::json!({
"type": "string",
"example": "127.0.0.1",
})
}
}

#[cfg(feature = "openapi_ip_str")]
impl OpenApiSchema for std::net::Ipv6Addr {
fn get_schema() -> serde_json::Value {
serde_json::json!({
"type": "string",
"example": "::1",
})
}
}

#[cfg(feature = "openapi_ip_str")]
impl OpenApiSchema for std::net::SocketAddr {
fn get_schema() -> serde_json::Value {
serde_json::json!({
"type": "string",
"example": "127.0.0.1:80 or [::1]:80",
})
}
}

#[cfg(feature = "openapi_ip_str")]
impl OpenApiSchema for std::net::SocketAddrV4 {
fn get_schema() -> serde_json::Value {
serde_json::json!({
"type": "string",
"example": "127.0.0.1:80",
})
}
}

#[cfg(feature = "openapi_ip_str")]
impl OpenApiSchema for std::net::SocketAddrV6 {
fn get_schema() -> serde_json::Value {
serde_json::json!({
"type": "string",
"example": "[::1]:80",
})
}
}

#[cfg(feature = "openapi_mac_str")]
impl OpenApiSchema for eui48::MacAddress {
fn get_schema() -> serde_json::Value {
serde_json::json!({
"type": "string",
"example": "01:02:03:04:05:06",
})
}
}

impl OpenApiSchema for serde_json::Value {
fn get_schema() -> serde_json::Value {
crate::json!({})
}
}

impl OpenApiSchema for HttpResponse {
fn get_schema() -> serde_json::Value {
crate::json!({})
Expand Down

0 comments on commit 325f1d9

Please sign in to comment.