drift 0.1.3

Library for comparing the compatibility of OpenAPI documents
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright 2025 Oxide Computer Company

use std::collections::BTreeMap;

use indexmap::IndexMap;
use openapiv3::{
    MediaType, Operation, Parameter, ParameterSchemaOrContent, ReferenceOr, RequestBody,
};
use serde_json::Value;

use crate::{
    Change, ChangeClass, ChangeComparison, ChangeDetails,
    context::{Context, Contextual, ToContext},
    operations::{all_params, operations},
    resolve::ReferenceOrResolver,
    schema::SchemaComparison,
    setops::SetCompare,
};

pub fn compare(old: &Value, new: &Value) -> anyhow::Result<Vec<Change>> {
    let mut comp = Compare::default();
    comp.compare(old, new)?;

    Ok(comp.changes)
}

#[derive(Default)]
pub(crate) struct Compare {
    pub changes: Vec<Change>,
    /// Cache comparisons (type of comparison, old and new path to the schema)
    /// and the boolean result (are the schemas backward-compatible?).
    pub visited: BTreeMap<(SchemaComparison, String, String), bool>,
}

impl Compare {
    pub fn compare(&mut self, old: &Value, new: &Value) -> anyhow::Result<()> {
        let old_context = Context::new(old);
        let old_operations = operations(&old_context)?;

        let new_context = Context::new(new);
        let new_operations = operations(&new_context)?;

        let SetCompare {
            a_unique,
            common,
            b_unique,
        } = SetCompare::new(old_operations, new_operations);

        // Any operation that was removed represents a breaking change.
        for op_info in a_unique.values() {
            let op_name = match &op_info.operation.operation_id {
                Some(name) => name.as_str(),
                None => "<unnamed>",
            };
            self.push_change(
                format!("The operation {op_name} was removed"),
                &op_info.operation,
                &new_context.append("paths"),
                ChangeComparison::Structural,
                ChangeClass::BackwardIncompatible,
                ChangeDetails::Removed,
            );
        }

        // Any new operation is a forward-incompatible change.
        for op_info in b_unique.values() {
            let op_name = match &op_info.operation.operation_id {
                Some(name) => name.as_str(),
                None => "<unnamed>",
            };

            self.push_change(
                format!("The operation {op_name} was added"),
                &old_context.append("paths"),
                &op_info.operation,
                ChangeComparison::Structural,
                ChangeClass::ForwardIncompatible,
                ChangeDetails::Added,
            );
        }

        // Anything that exists in both requires deeper inspection.
        for (old_info, new_info) in common.values() {
            self.compare_operations(old_info, new_info)?;
        }

        Ok(())
    }

    fn compare_operations(
        &mut self,
        old_operation: &crate::operations::OperationInfo<'_>,
        new_operation: &crate::operations::OperationInfo<'_>,
    ) -> anyhow::Result<()> {
        self.compare_parameters(old_operation, new_operation)?;
        self.compare_request_body(&old_operation.operation, &new_operation.operation)?;
        self.compare_responses(&old_operation.operation, &new_operation.operation)?;
        Ok(())
    }

    fn compare_parameters(
        &mut self,
        old_operation: &crate::operations::OperationInfo<'_>,
        new_operation: &crate::operations::OperationInfo<'_>,
    ) -> anyhow::Result<()> {
        let old_params = all_params(old_operation)?;
        let new_params = all_params(new_operation)?;

        let SetCompare {
            a_unique,
            common,
            b_unique,
        } = SetCompare::new(old_params, new_params);

        // It should not be possible to have arrived here with path parameters
        // added or removed.
        for old_param in a_unique.values().chain(b_unique.values()) {
            assert!(!matches!(old_param.as_ref(), Parameter::Path { .. }));
        }

        // A parameter that has been removed should be ignored, but it's
        // possible that the semantics of the operation change as a result,
        // therefore we treat the removal of a parameter as backward
        // incompatible.
        for old_param in a_unique.values() {
            let param_name = &old_param.parameter_data_ref().name;
            self.push_change(
                format!("The parameter '{param_name}' was removed"),
                old_param,
                &new_operation.operation,
                ChangeComparison::Input,
                ChangeClass::BackwardIncompatible,
                ChangeDetails::Removed,
            );
        }

        // New required parameters will break old clients.
        // New optional parameters are forward-incompatible because new clients
        // might send the parameter to old servers that don't understand it.
        for new_param in b_unique.values() {
            let param_name = &new_param.parameter_data_ref().name;
            if new_param.parameter_data_ref().required {
                self.push_change(
                    format!("A new, required parameter '{param_name}' was added"),
                    &old_operation.operation,
                    new_param,
                    ChangeComparison::Input,
                    ChangeClass::BackwardIncompatible,
                    ChangeDetails::AddedRequired,
                );
            } else {
                self.push_change(
                    format!("A new, optional parameter '{param_name}' was added"),
                    &old_operation.operation,
                    new_param,
                    ChangeComparison::Input,
                    ChangeClass::ForwardIncompatible,
                    ChangeDetails::Added,
                );
            }
        }

        for (old_param, new_param) in common.values() {
            let old_param_data = old_param.parameter_data_ref();
            let new_param_data = new_param.parameter_data_ref();
            let param_name = &new_param_data.name;

            // A parameter that is "more" required is backward incompatible.
            if !old_param_data.required && new_param_data.required {
                self.push_change(
                    format!("The parameter '{param_name}' was optional and is now required"),
                    old_param,
                    new_param,
                    ChangeComparison::Input,
                    ChangeClass::BackwardIncompatible,
                    ChangeDetails::MoreStrict,
                );
            }

            // A parameter that is "less" required is forward incompatible.
            if old_param_data.required && !new_param_data.required {
                self.push_change(
                    format!("The parameter '{param_name}' was required and is now optional"),
                    old_param,
                    new_param,
                    ChangeComparison::Input,
                    ChangeClass::ForwardIncompatible,
                    ChangeDetails::LessStrict,
                );
            }

            match (&old_param_data.format, &new_param_data.format) {
                (
                    ParameterSchemaOrContent::Schema(old_schema),
                    ParameterSchemaOrContent::Schema(new_schema),
                ) => {
                    let old_schema =
                        Contextual::new(old_param.context().append("schema"), old_schema);
                    let new_schema =
                        Contextual::new(new_param.context().append("schema"), new_schema);
                    self.compare_schema_ref(SchemaComparison::Input, old_schema, new_schema)?
                }

                (old, new) if old == new => {}

                _ => {
                    self.push_change(
                        "Unhandled change to parameter schema or content",
                        old_param,
                        new_param,
                        ChangeComparison::Input,
                        ChangeClass::Unhandled,
                        ChangeDetails::UnknownDifference,
                    );
                }
            }
        }

        Ok(())
    }

    fn compare_request_body(
        &mut self,
        old_operation: &Contextual<'_, Operation>,
        new_operation: &Contextual<'_, Operation>,
    ) -> anyhow::Result<()> {
        let old_request_body =
            old_operation.append_deref(&old_operation.request_body, "request_body");
        let new_request_body =
            new_operation.append_deref(&new_operation.request_body, "request_body");

        match (old_request_body.as_ref(), new_request_body.as_ref()) {
            (None, None) => {}
            // A request body is no longer specified. Old clients may still
            // send a body, which the new server won't expect.
            (Some(old_body_or_ref), None) => {
                let contextual_old_body =
                    Contextual::new(old_request_body.context().clone(), old_body_or_ref);
                let (old_body, _) = contextual_old_body.contextual_resolve()?;

                if old_body.required {
                    // Old clients will send a required body that new servers
                    // don't expect.
                    self.push_change(
                        "a required body parameter was removed",
                        old_operation,
                        new_operation,
                        ChangeComparison::Input,
                        ChangeClass::ForwardIncompatible,
                        ChangeDetails::Removed,
                    );
                } else {
                    // Old clients may send an optional body that new servers
                    // don't expect.
                    self.push_change(
                        "an optional body parameter was removed",
                        old_operation,
                        new_operation,
                        ChangeComparison::Input,
                        ChangeClass::ForwardIncompatible,
                        ChangeDetails::Removed,
                    );
                }
            }

            (None, Some(new_body_or_ref)) => {
                let contextual_new_body =
                    Contextual::new(new_request_body.context().clone(), new_body_or_ref);
                let (new_body, _) = contextual_new_body.contextual_resolve()?;

                if new_body.required {
                    // This is very much like changing the type of a parameter:
                    // previously the body was "nothing" and now it must be
                    // "something".
                    self.push_change(
                        "no body parameter was specified and now one is required",
                        old_operation,
                        new_operation,
                        ChangeComparison::Input,
                        ChangeClass::Incompatible,
                        ChangeDetails::AddedRequired,
                    );
                } else {
                    // A new, optional body is backward-compatible because old
                    // clients will not send the body; it is
                    // forward-incompatible because new clients might try to
                    // send a body to an old server.
                    self.push_change(
                        "no body parameter was specified and now one is accepted",
                        old_operation,
                        new_operation,
                        ChangeComparison::Input,
                        ChangeClass::ForwardIncompatible,
                        ChangeDetails::Added,
                    );
                }
            }
            (Some(old_body_or_ref), Some(new_body_or_ref)) => {
                let (old_body, old_body_context) =
                    old_body_or_ref.resolve(old_request_body.context())?;
                let (new_body, new_body_context) =
                    new_body_or_ref.resolve(new_request_body.context())?;

                // Check if any of the fields changed.
                let RequestBody {
                    description: old_description,
                    content: old_content,
                    required: old_required,
                    extensions: old_extensions,
                } = &*old_body;
                let RequestBody {
                    description: new_description,
                    content: new_content,
                    required: new_required,
                    extensions: new_extensions,
                } = &*new_body;

                // Description and extension changes are trivial metadata changes.
                if old_description != new_description || old_extensions != new_extensions {
                    self.push_change(
                        "the body metadata (description or extensions) changed",
                        old_operation,
                        new_operation,
                        ChangeComparison::Input,
                        ChangeClass::Trivial,
                        ChangeDetails::Metadata,
                    );
                }

                if !*old_required && *new_required {
                    self.push_change(
                        "the body parameter was optional and is now required",
                        old_operation,
                        new_operation,
                        ChangeComparison::Input,
                        ChangeClass::BackwardIncompatible,
                        ChangeDetails::MoreStrict,
                    );
                } else if *old_required && !*new_required {
                    self.push_change(
                        "the body parameter was required and is now optional",
                        old_operation,
                        new_operation,
                        ChangeComparison::Input,
                        ChangeClass::ForwardIncompatible,
                        ChangeDetails::LessStrict,
                    );
                }

                let old_body_content =
                    Contextual::new(old_body_context.append("content"), old_content);
                let new_body_content =
                    Contextual::new(new_body_context.append("content"), new_content);

                self.compare_content(
                    SchemaComparison::Input,
                    &old_body_content,
                    &new_body_content,
                )?;
            }
        }
        Ok(())
    }

    fn compare_responses(
        &mut self,
        old_operation: &Contextual<'_, Operation>,
        new_operation: &Contextual<'_, Operation>,
    ) -> anyhow::Result<()> {
        match (
            &old_operation.responses.default,
            &new_operation.responses.default,
        ) {
            (None, None) => {}
            (None, Some(_)) => {
                // Considering the impact of a default response is complex and
                // requires us to consider other responses... and to perhaps
                // apply heuristic knowledge.
                self.push_change(
                    "operation added a default response",
                    old_operation,
                    new_operation,
                    ChangeComparison::Output,
                    ChangeClass::Unhandled,
                    ChangeDetails::Added,
                );
            }
            (Some(_), None) => {
                // Fewer responses is always backward-compatible, but
                // considering the full impact is, again, complex.
                self.push_change(
                    "operation removed a default response",
                    old_operation,
                    new_operation,
                    ChangeComparison::Output,
                    ChangeClass::Unhandled,
                    ChangeDetails::Removed,
                );
            }

            (Some(old_response_or_ref), Some(new_response_or_ref)) => {
                let old_response_or_ref = Contextual::new(
                    old_operation
                        .context()
                        .append("responses")
                        .append("default"),
                    old_response_or_ref,
                );
                let new_response_or_ref = Contextual::new(
                    new_operation
                        .context()
                        .append("responses")
                        .append("default"),
                    new_response_or_ref,
                );

                self.compare_response(old_response_or_ref, new_response_or_ref)?;
            }
        }

        let SetCompare {
            a_unique,
            common,
            b_unique,
        } = SetCompare::new(
            &old_operation.responses.responses,
            &new_operation.responses.responses,
        );

        // It's forward-incompatible for an operation to have responses it no
        // longer sends
        for old_status in a_unique.keys() {
            self.push_change(
                format!("operation no longer responds with status {old_status}"),
                old_operation,
                new_operation,
                ChangeComparison::Output,
                ChangeClass::ForwardIncompatible,
                ChangeDetails::Removed,
            );
        }

        // Adding a new response would break old clients that don't expect it
        // and is therefore backward-incompatible.
        for new_status in b_unique.keys() {
            self.push_change(
                format!("operation added a new response code {new_status}"),
                old_operation,
                new_operation,
                ChangeComparison::Output,
                ChangeClass::BackwardIncompatible,
                ChangeDetails::Added,
            );
        }

        // For common responses, we need to check that the content is
        // compatible.
        for (status, (old_response, new_response)) in common {
            let old_response = Contextual::new(
                old_operation
                    .context()
                    .append("responses")
                    .append(&status.to_string()),
                old_response,
            );
            let new_response = Contextual::new(
                new_operation
                    .context()
                    .append("responses")
                    .append(&status.to_string()),
                new_response,
            );

            self.compare_response(old_response, new_response)?;
        }

        Ok(())
    }

    fn compare_response(
        &mut self,
        old_response_or_ref: Contextual<'_, &ReferenceOr<openapiv3::Response>>,
        new_response_or_ref: Contextual<'_, &ReferenceOr<openapiv3::Response>>,
    ) -> anyhow::Result<()> {
        let (old_response, old_context) = old_response_or_ref.contextual_resolve()?;
        let (new_response, new_context) = new_response_or_ref.contextual_resolve()?;

        let old_content = Contextual::new(old_context.append("content"), &old_response.content);
        let new_content = Contextual::new(new_context.append("content"), &new_response.content);

        self.compare_content(SchemaComparison::Output, &old_content, &new_content)?;

        Ok(())
    }

    fn compare_content(
        &mut self,
        comparison: SchemaComparison,
        old_content: &Contextual<'_, &IndexMap<String, MediaType>>,
        new_content: &Contextual<'_, &IndexMap<String, MediaType>>,
    ) -> anyhow::Result<()> {
        let SetCompare {
            a_unique,
            common,
            b_unique,
        } = SetCompare::new(old_content.iter(), new_content.iter());

        // TODO
        assert!(a_unique.is_empty());
        assert!(b_unique.is_empty());

        for (mime_type, (old_media, new_media)) in common {
            match (&old_media.schema, &new_media.schema) {
                (None, None) => {}
                (None, Some(_)) => {
                    todo!()
                }
                (Some(_), None) => todo!(),
                (Some(old_schema), Some(new_schema)) => {
                    let old_schema = Contextual::new(
                        old_content.context().append(mime_type).append("schema"),
                        old_schema,
                    );
                    let new_schema = Contextual::new(
                        new_content.context().append(mime_type).append("schema"),
                        new_schema,
                    );

                    self.compare_schema_ref(comparison, old_schema, new_schema)?;
                }
            }
        }

        Ok(())
    }

    pub(crate) fn push_change(
        &mut self,
        message: impl ToString,
        old: &dyn ToContext<'_>,
        new: &dyn ToContext<'_>,
        comparison: ChangeComparison,
        class: ChangeClass,
        details: ChangeDetails,
    ) {
        let change = Change::new(
            message,
            old.to_context().stack.clone(),
            new.to_context().stack.clone(),
            comparison,
            class,
            details,
        );

        self.changes.push(change);
    }
}