pub trait AsSchema {
// Required method
fn as_schema() -> Schema;
}
Expand description
Trait for Rust types that can generate a Schema
(a subset of OpenAPI schemas) automatically.
Implement this trait or derive AsSchema
to enable schema generation for your types.
The derive macro supports extensive customization through attributes and integrates with Serde.
§Description Attributes
Descriptions can now span multiple lines. You can use multiple #[schema(description = "...")]
attributes on a field or a struct. Each attribute’s content will be concatenated.
To add a new line, use an empty #[schema(description = "")]
attribute, similar to how
the standard ///
documentation comments work.
§Examples
use google_ai_rs::AsSchema;
#[derive(AsSchema)]
#[schema(rename_all = "camelCase")]
struct AiReport {
// A single-line description
#[schema(description = "This should include user's name and date of birth", required)]
data: String,
// A multi-line description
#[schema(description = "This field contains important metadata.")]
#[schema(description = "")] // New line
#[schema(description = "For example, the creation timestamp and author.")]
metadata: String,
}
§Customizing Foreign Types
For types from other crates where you can’t add the derive
, you can either manually
specify its schema or reference a function that generates it.
1. Using r#type
and format
:
use google_ai_rs::AsSchema;
#[derive(AsSchema)]
struct AiReport {
#[schema(r#type = "Number", format = "double")]
foreign: some_crate::TheirType
}
2. Using as_schema
with a function:
This is useful for more complex foreign types.
use google_ai_rs::{AsSchema, Schema, schema::SchemaFormat};
#[derive(AsSchema)]
struct AiReport {
#[schema(as_schema = "foreign_type_schema")]
foreign: some_crate::TheirType
}
fn foreign_type_schema() -> Schema {
Schema::new_object()
.description("A custom schema for a foreign type.")
.property("id", Schema::new_number().format(SchemaFormat::Int64))
}
§Serde Compatibility
The AsSchema
derive macro automatically integrates with serde
attributes for convenience.
#[serde(rename)]
/#[serde(rename_all)]
are respected for naming fields in the schema.#[serde(skip)]
fields are automatically excluded from the generated schema.- You can disable Serde integration for a specific field with
#[schema(ignore_serde)]
or for the whole type with#[schema(serde_ignore)]
on the struct.
§Examples with Serde
use google_ai_rs::AsSchema;
#[derive(AsSchema, serde::Deserialize)]
struct AiReport {
#[schema(description = "Important field", required)]
#[serde(rename = "json_field")] // Applies to the schema too
field: String,
#[serde(skip)] // Excluded from schema
internal: String,
#[schema(skip)] // Overrides Serde's behavior and excludes from schema
#[serde(rename = "count")] // This rename is ignored by the schema
item_count: i32,
}
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.