Skip to content

Commit

Permalink
feat: support compact_str for oapi (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yvictor authored Jan 10, 2025
1 parent 089f284 commit cfc5cde
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ ulid = { version = "1", default-features = false }
url = "2"
uuid = "1"
x509-parser = "0.16"
compact_str = { version = "0.7", features = ["serde"] }

# Compress
brotli = { version = "7", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions crates/oapi-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ smallvec = []
repr = []
indexmap = []
non-strict-integers = []
compact_str = []

[dependencies]
proc-macro2 = { workspace = true }
Expand All @@ -49,6 +50,7 @@ assert-json-diff = { workspace = true }
time = { workspace = true, features = ["serde-human-readable"] }
serde_with = { workspace = true }
paste = { workspace = true }
compact_str = { workspace = true, features = ["serde"] }

[lints]
workspace = true
25 changes: 23 additions & 2 deletions crates/oapi-macros/src/schema_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl SchemaType<'_> {
feature = "ulid",
feature = "uuid",
feature = "time",
feature = "compact_str"
)))]
{
is_primitive(name)
Expand All @@ -91,6 +92,7 @@ impl SchemaType<'_> {
feature = "ulid",
feature = "uuid",
feature = "time",
feature = "compact_str"
))]
{
let mut primitive = is_primitive(name);
Expand Down Expand Up @@ -125,6 +127,10 @@ impl SchemaType<'_> {
"Date" | "PrimitiveDateTime" | "OffsetDateTime" | "Duration"
);
}
#[cfg(feature = "compact_str")]
if !primitive {
primitive = matches!(name, "CompactString");
}

primitive
}
Expand Down Expand Up @@ -246,6 +252,10 @@ impl TryToTokens for SchemaType<'_> {
"Date" | "Duration" => {
schema_type_tokens(tokens, oapi, SchemaTypeInner::String, self.nullable)
}
#[cfg(feature = "compact_str")]
"CompactString" => {
schema_type_tokens(tokens, oapi, SchemaTypeInner::String, self.nullable)
}
#[cfg(all(feature = "decimal", feature = "decimal-float"))]
"Decimal" => schema_type_tokens(tokens, oapi, SchemaTypeInner::String, self.nullable),
#[cfg(all(feature = "decimal", not(feature = "decimal-float")))]
Expand Down Expand Up @@ -333,7 +343,8 @@ impl Type<'_> {
feature = "url",
feature = "ulid",
feature = "uuid",
feature = "time"
feature = "time",
feature = "compact_str"
)))]
{
is_known_format(name)
Expand All @@ -346,7 +357,8 @@ impl Type<'_> {
feature = "url",
feature = "ulid",
feature = "uuid",
feature = "time"
feature = "time",
feature = "compact_str"
))]
{
let mut known_format = is_known_format(name);
Expand Down Expand Up @@ -381,6 +393,11 @@ impl Type<'_> {
known_format = matches!(name, "Date" | "PrimitiveDateTime" | "OffsetDateTime");
}

#[cfg(feature = "compact_str")]
if !known_format {
known_format = matches!(name, "CompactString");
}

known_format
}
}
Expand Down Expand Up @@ -460,6 +477,10 @@ impl TryToTokens for Type<'_> {
"NaiveDateTime" => {
tokens.extend(quote! { #oapi::oapi::SchemaFormat::KnownFormat(#oapi::oapi::KnownFormat::DateTime) })
}
#[cfg(feature = "compact_str")]
"CompactString" => {
tokens.extend(quote! { #oapi::oapi::SchemaFormat::KnownFormat(#oapi::oapi::KnownFormat::String) })
}
#[cfg(feature = "time")]
"Date" => tokens.extend(quote! { #oapi::oapi::SchemaFormat::KnownFormat(#oapi::oapi::KnownFormat::Date) }),
#[cfg(feature = "url")]
Expand Down
5 changes: 4 additions & 1 deletion crates/oapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ authors = ["Juha Kukkonen <[email protected]>", "Chrislearn Young <chris@a

[features]
default = []
full = ["swagger-ui", "scalar", "rapidoc", "redoc", "chrono", "decimal", "url", "ulid", "uuid", "time", "smallvec", "indexmap", "yaml", "non-strict-integers"]
full = ["swagger-ui", "scalar", "rapidoc", "redoc", "chrono", "decimal", "url", "ulid", "uuid", "time", "smallvec", "indexmap", "yaml", "non-strict-integers", "compact_str"]
swagger-ui = ["dep:rust-embed"]
scalar = []
rapidoc = []
Expand All @@ -34,6 +34,7 @@ yaml = ["dep:serde_yaml"]
preserve-order = ["preserve-path-order", "preserve-prop-order"]
preserve-path-order = []
preserve-prop-order = []
compact_str = ["salvo-oapi-macros/compact_str", "dep:compact_str"]

[dependencies]
salvo_core = { workspace = true, default-features = false, features = ["cookie"] }
Expand Down Expand Up @@ -63,6 +64,7 @@ time = { workspace = true, optional = true }
ulid = { workspace = true, optional = true }
url = { workspace = true, optional = true }
uuid = { workspace = true, optional = true }
compact_str = { workspace = true, optional = true }

[build-dependencies]
regex = { workspace = true }
Expand All @@ -80,6 +82,7 @@ smallvec = { workspace = true, features = ["serde"] }
rust_decimal = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
time = { workspace = true, features = ["serde-human-readable"] }
compact_str = { workspace = true, features = ["serde"] }

[lints]
workspace = true
3 changes: 3 additions & 0 deletions crates/oapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ 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<schema::Schema> {
crate::RefOr::Type(Schema::OneOf(
Expand All @@ -212,6 +213,8 @@ impl<T: chrono::TimeZone> ToSchema for chrono::DateTime<T> {
schema!(#[inline] DateTime<T>).into()
}
}
#[cfg(feature = "compact_str")]
impl_to_schema_primitive!(compact_str::CompactString);
#[cfg(any(feature = "decimal", feature = "decimal-float"))]
impl_to_schema!(rust_decimal::Decimal);
#[cfg(feature = "url")]
Expand Down
2 changes: 2 additions & 0 deletions crates/oapi/src/openapi/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ pub enum KnownFormat {
Duration,
/// Hint to UI to obscure input.
Password,
/// Use for compact string
String,
/// Used with [`String`] values to indicate value is in decimal format.
///
/// **decimal** feature need to be enabled.
Expand Down
1 change: 1 addition & 0 deletions examples/oapi-todos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ salvo = { workspace = true, features = ["oapi"] }
serde = { workspace = true, features = ["derive"] }
serde_json = "1"
tokio = { workspace = true, features = ["macros"] }
compact_str = { version = "0.7", features = ["serde"] }
tracing.workspace = true
tracing-subscriber.workspace = true

0 comments on commit cfc5cde

Please sign in to comment.