Skip to main content

MessageInfo

MessageInfo names the component of a message in the document and describes it, whatever the Rust type is called.

Channels and operations refer to a message by its component name. Without MessageInfo that name is the schema's title, which schemars takes from the type's name, so renaming the type renames the published message. An impl of MessageInfo keeps the name: NAME is the component's name and DESCRIPTION its description. #[derive(MessageInfo)] writes the impl from the type's name and doc comment. With #[message(headers(Meta))] the derive also declares the type's header contract. #[derive(Outgoing)] implements MessageInfo for the types it derives. Without the macros a registration names the component by the schema's title, so #[schemars(rename = "..")] keeps the published name there. The example runs with cargo test --example asyncapi_schemas --features macros,memory,asyncapi.

Example​

/// A refund a customer asked for.
#[derive(Debug, Deserialize, JsonSchema)]
struct RefundRequestV2 {
order_id: u64,
}

/// The component keeps the name the contract was published under, whatever the type is called.
impl MessageInfo for RefundRequestV2 {
const NAME: &'static str = "RefundRequest";
const DESCRIPTION: Option<&'static str> = Some("A refund a customer asked for.");
}

#[subscriber("refunds")]
async fn refund(request: &RefundRequestV2) -> HandlerOutcome {
tracing::info!(request.order_id, "refund requested");
HandlerOutcome::ack()
}