roas-asyncapi 0.4.0

Rust implementation of the AsyncAPI Specification v2.6 / v3.0 / v3.1 — parse and validate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! AsyncAPI v3.0 `Operation`, `Operation Trait`, and reply objects.
//!
//! Per [Operation Object](https://www.asyncapi.com/docs/reference/specification/v3.0.0#operationObject).
//!
//! v3 hoists operations out of channels into a top-level map, and states
//! them from the application's point of view: `send` means this
//! application sends to the channel, `receive` means it consumes from
//! it. (AsyncAPI 2.x said `subscribe` / `publish`, describing the *other*
//! side — the inversion is the classic migration trap.)

use crate::common::bindings::OperationBindings;
use crate::common::reference::{RefOr, Reference};
use crate::common::runtime_expression;
use crate::v3_0::external_documentation::ExternalDocumentation;
use crate::v3_0::security_scheme::SecurityScheme;
use crate::v3_0::tag::Tag;
use crate::validation::{Context, ValidateWithContext};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Whether the application sends to, or receives from, the channel.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum OperationAction {
    /// The application sends a message to the channel.
    #[default]
    Send,
    /// The application receives messages from the channel.
    Receive,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct Operation {
    /// **Required** Whether the application sends or receives.
    pub action: OperationAction,

    /// **Required** A `$ref` to the channel this operation belongs to.
    pub channel: Reference,

    /// A `$ref` subset of the channel's messages that this operation
    /// processes. Empty means every message on the channel.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub messages: Vec<Reference>,

    /// The reply part of a request/reply operation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reply: Option<RefOr<OperationReply>>,

    /// Traits to apply to the operation. Traits are validated but not
    /// merged into the operation by this crate.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub traits: Vec<RefOr<OperationTrait>>,

    /// A human-friendly title for the operation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// A short summary of what the operation is about.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,

    /// A verbose explanation, CommonMark-formatted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// The security requirements for this operation.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub security: Vec<RefOr<SecurityScheme>>,

    /// Tags for logical grouping and categorization of operations.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<RefOr<Tag>>,

    /// Additional external documentation for this operation.
    #[serde(rename = "externalDocs", skip_serializing_if = "Option::is_none")]
    pub external_docs: Option<RefOr<ExternalDocumentation>>,

    /// Protocol-specific definitions for the operation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bindings: Option<RefOr<OperationBindings>>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl ValidateWithContext for Operation {
    fn validate_with_context(&self, ctx: &mut Context) {
        ctx.in_field("channel", |ctx| self.channel.validate_with_context(ctx));

        for (i, message) in self.messages.iter().enumerate() {
            ctx.in_index("messages", i, |ctx| message.validate_with_context(ctx));
        }
        if let Some(reply) = &self.reply {
            ctx.in_field("reply", |ctx| reply.validate_with_context(ctx));
        }
        for (i, operation_trait) in self.traits.iter().enumerate() {
            ctx.in_index("traits", i, |ctx| {
                operation_trait.validate_with_context(ctx)
            });
        }
        for (i, scheme) in self.security.iter().enumerate() {
            ctx.in_index("security", i, |ctx| scheme.validate_with_context(ctx));
        }
        for (i, tag) in self.tags.iter().enumerate() {
            ctx.in_index("tags", i, |ctx| tag.validate_with_context(ctx));
        }
        if let Some(docs) = &self.external_docs {
            ctx.in_field("externalDocs", |ctx| docs.validate_with_context(ctx));
        }
        if let Some(bindings) = &self.bindings {
            ctx.in_field("bindings", |ctx| bindings.validate_with_context(ctx));
        }
    }
}

/// A trait that MAY be applied to an [`Operation`].
///
/// Carries every Operation field except `action`, `channel`, `messages`,
/// `reply`, and `traits`.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct OperationTrait {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub security: Vec<RefOr<SecurityScheme>>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<RefOr<Tag>>,

    #[serde(rename = "externalDocs", skip_serializing_if = "Option::is_none")]
    pub external_docs: Option<RefOr<ExternalDocumentation>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub bindings: Option<RefOr<OperationBindings>>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl ValidateWithContext for OperationTrait {
    fn validate_with_context(&self, ctx: &mut Context) {
        for (i, scheme) in self.security.iter().enumerate() {
            ctx.in_index("security", i, |ctx| scheme.validate_with_context(ctx));
        }
        for (i, tag) in self.tags.iter().enumerate() {
            ctx.in_index("tags", i, |ctx| tag.validate_with_context(ctx));
        }
        if let Some(docs) = &self.external_docs {
            ctx.in_field("externalDocs", |ctx| docs.validate_with_context(ctx));
        }
        if let Some(bindings) = &self.bindings {
            ctx.in_field("bindings", |ctx| bindings.validate_with_context(ctx));
        }
    }
}

/// The reply half of a request/reply operation.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct OperationReply {
    /// Where to send the reply — a fixed location or a runtime
    /// expression evaluated against the request message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address: Option<RefOr<OperationReplyAddress>>,

    /// A `$ref` to the channel the reply travels over.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channel: Option<Reference>,

    /// A `$ref` subset of the reply channel's messages.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub messages: Vec<Reference>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl ValidateWithContext for OperationReply {
    fn validate_with_context(&self, ctx: &mut Context) {
        if let Some(address) = &self.address {
            ctx.in_field("address", |ctx| address.validate_with_context(ctx));
        }
        if let Some(channel) = &self.channel {
            ctx.in_field("channel", |ctx| channel.validate_with_context(ctx));
        }
        for (i, message) in self.messages.iter().enumerate() {
            ctx.in_index("messages", i, |ctx| message.validate_with_context(ctx));
        }
        // Naming reply messages without naming the channel they belong
        // to leaves them unresolvable.
        if !self.messages.is_empty() && self.channel.is_none() {
            ctx.error_field("channel", "is required when `messages` is set");
        }
    }
}

/// A runtime expression locating where a reply is sent.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct OperationReplyAddress {
    /// **Required** A runtime expression, e.g.
    /// `$message.header#/replyTo`.
    pub location: String,

    /// An optional description, CommonMark-formatted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl ValidateWithContext for OperationReplyAddress {
    fn validate_with_context(&self, ctx: &mut Context) {
        if self.location.is_empty() {
            ctx.error_field("location", "must not be empty");
        } else if let Err(err) = runtime_expression::parse(&self.location) {
            ctx.error_field(
                "location",
                format!(
                    "`{}` is not a valid runtime expression: {}",
                    self.location,
                    err.message()
                ),
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use enumset::EnumSet;
    use serde_json::json;

    fn errors_for(value: serde_json::Value) -> Vec<String> {
        let operation: Operation = serde_json::from_value(value).unwrap();
        let mut ctx = Context::with_path(EnumSet::empty(), "#.operations.sendSignup");
        operation.validate_with_context(&mut ctx);
        ctx.errors.iter().map(ToString::to_string).collect()
    }

    #[test]
    fn round_trips_a_full_operation() {
        let value = json!({
            "action": "receive",
            "channel": { "$ref": "#/channels/user" },
            "messages": [ { "$ref": "#/channels/user/messages/signup" } ],
            "reply": {
                "address": { "location": "$message.header#/replyTo" },
                "channel": { "$ref": "#/channels/replies" },
                "messages": [ { "$ref": "#/channels/replies/messages/ack" } ]
            },
            "traits": [ { "$ref": "#/components/operationTraits/common" } ],
            "title": "Receive signups",
            "summary": "Consume signup events",
            "description": "Long form",
            "security": [ { "type": "userPassword" } ],
            "tags": [ { "name": "user" } ],
            "externalDocs": { "url": "https://example.com" },
            "bindings": { "kafka": { "groupId": { "type": "string" } } },
            "x-owner": "team"
        });
        let operation: Operation = serde_json::from_value(value.clone()).unwrap();
        assert_eq!(operation.action, OperationAction::Receive);
        assert_eq!(serde_json::to_value(&operation).unwrap(), value);
        assert!(errors_for(value).is_empty());
    }

    #[test]
    fn action_round_trips_both_directions() {
        for (text, expected) in [
            ("send", OperationAction::Send),
            ("receive", OperationAction::Receive),
        ] {
            let operation: Operation =
                serde_json::from_value(json!({ "action": text, "channel": { "$ref": "#/c" } }))
                    .unwrap();
            assert_eq!(operation.action, expected);
            assert_eq!(serde_json::to_value(operation.action).unwrap(), json!(text));
        }
        assert!(
            serde_json::from_value::<Operation>(
                json!({ "action": "publish", "channel": { "$ref": "#/c" } })
            )
            .is_err(),
            "AsyncAPI 2.x actions must not parse as v3"
        );
    }

    #[test]
    fn action_and_channel_are_required_by_the_parser() {
        assert!(serde_json::from_value::<Operation>(json!({ "action": "send" })).is_err());
        assert!(
            serde_json::from_value::<Operation>(json!({ "channel": { "$ref": "#/c" } })).is_err()
        );
    }

    #[test]
    fn empty_channel_ref_is_reported() {
        let errors = errors_for(json!({ "action": "send", "channel": { "$ref": "" } }));
        assert!(
            errors
                .iter()
                .any(|e| e == "#.operations.sendSignup.channel.$ref: must not be empty")
        );
    }

    #[test]
    fn reply_messages_need_a_channel() {
        let errors = errors_for(json!({
            "action": "send",
            "channel": { "$ref": "#/channels/user" },
            "reply": { "messages": [ { "$ref": "#/channels/replies/messages/ack" } ] }
        }));
        assert!(
            errors.iter().any(|e| e
                == "#.operations.sendSignup.reply.channel: is required when `messages` is set"),
            "got: {errors:?}"
        );
    }

    #[test]
    fn reply_address_must_be_a_runtime_expression() {
        let errors = errors_for(json!({
            "action": "send",
            "channel": { "$ref": "#/channels/user" },
            "reply": { "address": { "location": "replyTo" } }
        }));
        assert!(
            errors
                .iter()
                .any(|e| e.contains("is not a valid runtime expression")),
            "got: {errors:?}"
        );

        let mut ctx = Context::with_path(EnumSet::empty(), "#.reply.address");
        OperationReplyAddress::default().validate_with_context(&mut ctx);
        assert!(ctx.errors[0] == "#.reply.address.location: must not be empty");
    }

    #[test]
    fn nested_errors_carry_their_path() {
        let errors = errors_for(json!({
            "action": "send",
            "channel": { "$ref": "#/channels/user" },
            "messages": [ { "$ref": "" } ],
            "traits": [ { "tags": [ { "name": "" } ] } ],
            "security": [ { "type": "http" } ],
            "tags": [ { "name": "" } ],
            "externalDocs": { "url": "" },
            "bindings": { "kafka": 1 }
        }));
        assert!(
            errors
                .iter()
                .any(|e| e == "#.operations.sendSignup.messages[0].$ref: must not be empty")
        );
        assert!(
            errors
                .iter()
                .any(|e| e == "#.operations.sendSignup.traits[0].tags[0].name: must not be empty")
        );
        assert!(
            errors
                .iter()
                .any(|e| e.starts_with("#.operations.sendSignup.security[0].scheme"))
        );
        assert!(
            errors
                .iter()
                .any(|e| e == "#.operations.sendSignup.tags[0].name: must not be empty")
        );
        assert!(
            errors
                .iter()
                .any(|e| e == "#.operations.sendSignup.externalDocs.url: must not be empty")
        );
        assert!(
            errors
                .iter()
                .any(|e| e.starts_with("#.operations.sendSignup.bindings.kafka"))
        );
    }

    #[test]
    fn operation_trait_validates_its_nested_objects() {
        let operation_trait: OperationTrait = serde_json::from_value(json!({
            "security": [ { "type": "oauth2" } ],
            "tags": [ { "name": "" } ],
            "externalDocs": { "url": "" },
            "bindings": { "amqp": 1 }
        }))
        .unwrap();
        let mut ctx = Context::with_path(EnumSet::empty(), "#.operationTraits.common");
        operation_trait.validate_with_context(&mut ctx);
        let msgs: Vec<_> = ctx.errors.iter().map(ToString::to_string).collect();
        assert!(msgs.iter().any(|e| e.contains(".security[0].flows")));
        assert!(msgs.iter().any(|e| e.contains(".tags[0].name")));
        assert!(msgs.iter().any(|e| e.contains(".externalDocs.url")));
        assert!(msgs.iter().any(|e| e.contains(".bindings.amqp")));
    }
}