brake 2026.11.3

A brake on breaking API changes. Checks OpenAPI, protobuf and GraphQL contracts for backward compatibility, hermetically.
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
//! Static structural lint pass.
//!
//! Checks the head contract for defects that break consumers regardless of
//! version history. See `design/07-structural-lint.md`.

use std::collections::{BTreeMap, BTreeSet};

use crate::config::Compatibility;
use crate::contract::{Contract, LintCandidateKind};
use crate::rules::{Finding, catalogue};

/// Run all structural lint rules against a single contract.
///
/// Returns findings at or above the given compatibility level; rules below it
/// do not fire (consistent with `rules::evaluate`).
#[must_use]
pub fn lint(contract: &Contract, contract_name: &str, level: Compatibility) -> Vec<Finding> {
    let mut findings = Vec::new();

    findings.extend(from_candidates(contract, contract_name, level));
    findings.extend(check_operation_id_duplicate(contract, contract_name, level));
    findings.extend(check_path_param_undeclared(contract, contract_name, level));

    findings.sort();
    findings.dedup();
    findings
}

/// The lint findings `head` has and `base` does not.
///
/// A check reports what a change introduced, never what was already there. A
/// defect already in the baseline fails no commit, which is the ratchet every
/// comparison rule follows (`design/01-thesis.md`, "Scope is a better ratchet
/// than a baseline"). Reporting the whole file instead blocked every edit to a
/// contract that had one old duplicate `operationId`.
#[must_use]
pub fn introduced(
    base: &Contract,
    head: &Contract,
    contract_name: &str,
    level: Compatibility,
) -> Vec<Finding> {
    let base_findings = lint(base, contract_name, level);
    let existing: BTreeSet<_> = base_findings.iter().map(identity).collect();
    lint(head, contract_name, level)
        .into_iter()
        .filter(|finding| !existing.contains(&identity(finding)))
        .collect()
}

/// What makes two lint findings the same defect.
///
/// Not the span and not the message: a defect that moved down the file, or a
/// duplicate `operationId` that gained a third endpoint, is still the defect
/// the baseline already had.
fn identity(finding: &Finding) -> (&'static str, &str, Option<&str>, Option<&str>, Option<&str>) {
    (
        finding.rule_id,
        finding.pointer.as_str(),
        finding.subject.as_deref(),
        finding.method.as_deref(),
        finding.path.as_deref(),
    )
}

fn from_candidates(contract: &Contract, contract_name: &str, level: Compatibility) -> Vec<Finding> {
    contract
        .lint_candidates
        .iter()
        .filter_map(|candidate| {
            let rule_id = match candidate.kind {
                LintCandidateKind::EnumTypeMismatch => "enum-type-mismatch",
                LintCandidateKind::EnumDuplicateValue => "enum-duplicate-value",
            };
            let rule = catalogue::lookup(rule_id)?;
            if level < rule.level {
                return None;
            }
            Some(Finding {
                affects: Vec::new(),
                note: None,
                rule_id: rule.id,
                severity: rule.severity,
                contract: contract_name.to_owned(),
                message: candidate.detail.clone(),
                method: None,
                path: None,
                pointer: candidate.pointer.clone(),
                subject: None,
                span: Some(candidate.span.clone()),
            })
        })
        .collect()
}

fn check_operation_id_duplicate(
    contract: &Contract,
    contract_name: &str,
    level: Compatibility,
) -> Vec<Finding> {
    let rule = catalogue::lookup("operation-id-duplicate").expect("catalogued");
    if level < rule.level {
        return Vec::new();
    }

    // Collect every (operation_id → list of endpoints that use it).
    let mut by_id: BTreeMap<&str, Vec<String>> = BTreeMap::new();
    for (key, endpoint) in &contract.endpoints {
        if let Some(id) = endpoint.operation_id.as_deref() {
            by_id
                .entry(id)
                .or_default()
                .push(format!("{} {}", key.method, key.path));
        }
    }

    by_id
        .into_iter()
        .filter(|(_, endpoints)| endpoints.len() > 1)
        .map(|(id, endpoints)| {
            let list = endpoints.join(", ");
            Finding {
                affects: Vec::new(),
                note: None,
                rule_id: rule.id,
                severity: rule.severity,
                contract: contract_name.to_owned(),
                message: format!("operationId `{id}` is used by multiple operations: {list}"),
                method: None,
                path: None,
                pointer: String::new(),
                subject: Some(id.to_owned()),
                span: None,
            }
        })
        .collect()
}

fn check_path_param_undeclared(
    contract: &Contract,
    contract_name: &str,
    level: Compatibility,
) -> Vec<Finding> {
    let rule = catalogue::lookup("path-param-undeclared").expect("catalogued");
    if level < rule.level {
        return Vec::new();
    }

    let mut findings = Vec::new();
    for (key, endpoint) in &contract.endpoints {
        let declared_path_params: std::collections::BTreeSet<&str> = endpoint
            .parameters
            .iter()
            .filter(|p| p.location == "path")
            .map(|p| p.name.as_str())
            .collect();

        for param in path_template_params(&key.path) {
            if !declared_path_params.contains(param) {
                findings.push(Finding {
                    affects: Vec::new(),
                    note: None,
                    rule_id: rule.id,
                    severity: rule.severity,
                    contract: contract_name.to_owned(),
                    message: format!(
                        "path template `{path}` uses `{{{param}}}` but no `in: path` \
                         parameter named `{param}` is declared on `{method} {path}`",
                        path = key.path,
                        method = key.method,
                    ),
                    method: Some(key.method.clone()),
                    path: Some(key.path.clone()),
                    pointer: format!(
                        "/paths/{}/{}",
                        key.path.replace('~', "~0").replace('/', "~1"),
                        key.method.to_ascii_lowercase()
                    ),
                    subject: Some(param.to_owned()),
                    span: Some(endpoint.span.clone()),
                });
            }
        }
    }
    findings
}

/// Extract `param` names from `{param}` placeholders in an OpenAPI path template.
fn path_template_params(path: &str) -> impl Iterator<Item = &str> {
    path.split('{')
        .skip(1)
        .filter_map(|segment| segment.split('}').next())
        .filter(|name| !name.is_empty())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Compatibility;
    use crate::contract::Contract;

    fn parse(yaml: &str) -> Contract {
        crate::contract::openapi::ingest("test.yaml", yaml.as_bytes())
            .expect("valid OpenAPI fixture")
    }

    // ── introduced ───────────────────────────────────────────────────────────

    const DUPLICATED: &str = "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /a:
    get:
      operationId: doThing
      responses: {'200': {description: ok}}
  /b:
    get:
      operationId: doThing
      responses: {'200': {description: ok}}
";

    #[test]
    fn a_defect_already_in_the_baseline_is_not_introduced() {
        let contract = parse(DUPLICATED);
        let introduced = introduced(&contract, &contract, "api", Compatibility::Surface);
        assert!(introduced.is_empty(), "{introduced:?}");
    }

    #[test]
    fn a_defect_the_change_adds_is_introduced() {
        let base = parse(&DUPLICATED.replace(
            "operationId: doThing\n      responses: {'200': {description: ok}}\n  /b",
            "operationId: doOther\n      responses: {'200': {description: ok}}\n  /b",
        ));
        let head = parse(DUPLICATED);
        let ids: Vec<_> = introduced(&base, &head, "api", Compatibility::Surface)
            .iter()
            .map(|finding| finding.rule_id)
            .collect();
        assert_eq!(ids, vec!["operation-id-duplicate"]);
    }

    #[test]
    fn an_old_defect_that_moved_is_still_the_old_defect() {
        let base = parse(DUPLICATED);
        let head = parse(&DUPLICATED.replace(
            "paths:\n",
            "paths:\n  /z:\n    get:\n      operationId: unrelated\n      responses: {'200': {description: ok}}\n",
        ));
        let introduced = introduced(&base, &head, "api", Compatibility::Surface);
        assert!(introduced.is_empty(), "{introduced:?}");
    }

    // ── operation-id-duplicate ───────────────────────────────────────────────

    #[test]
    fn operation_id_duplicate_fires_when_two_ops_share_an_id() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /a:
    get:
      operationId: doThing
      responses: {'200': {description: ok}}
  /b:
    get:
      operationId: doThing
      responses: {'200': {description: ok}}
",
        );
        let findings = lint(&contract, "api", Compatibility::Surface);
        let ids: Vec<_> = findings.iter().map(|f| f.rule_id).collect();
        assert!(
            ids.contains(&"operation-id-duplicate"),
            "expected operation-id-duplicate, got: {ids:?}"
        );
    }

    #[test]
    fn operation_id_duplicate_silent_when_ids_unique() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /a:
    get:
      operationId: getA
      responses: {'200': {description: ok}}
  /b:
    get:
      operationId: getB
      responses: {'200': {description: ok}}
",
        );
        let findings = lint(&contract, "api", Compatibility::Surface);
        assert!(
            findings
                .iter()
                .all(|f| f.rule_id != "operation-id-duplicate"),
            "should not fire when all operationIds are unique"
        );
    }

    #[test]
    fn operation_id_duplicate_not_fire_below_surface_level() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /a:
    get:
      operationId: doThing
      responses: {'200': {description: ok}}
  /b:
    get:
      operationId: doThing
      responses: {'200': {description: ok}}
",
        );
        // At WireJson level, Surface rules do not fire.
        let findings = lint(&contract, "api", Compatibility::WireJson);
        assert!(
            findings
                .iter()
                .all(|f| f.rule_id != "operation-id-duplicate"),
            "Surface rule must not fire below Surface level"
        );
    }

    // ── path-param-undeclared ────────────────────────────────────────────────

    #[test]
    fn path_param_undeclared_fires_when_template_param_missing() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /payments/{id}:
    get:
      responses: {'200': {description: ok}}
",
        );
        let findings = lint(&contract, "api", Compatibility::Surface);
        let ids: Vec<_> = findings.iter().map(|f| f.rule_id).collect();
        assert!(
            ids.contains(&"path-param-undeclared"),
            "expected path-param-undeclared, got: {ids:?}"
        );
        assert_eq!(
            findings
                .iter()
                .find(|f| f.rule_id == "path-param-undeclared")
                .and_then(|f| f.subject.as_deref()),
            Some("id")
        );
    }

    #[test]
    fn path_param_undeclared_silent_when_param_declared() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /payments/{id}:
    get:
      parameters:
        - name: id
          in: path
          required: true
          schema: {type: string}
      responses: {'200': {description: ok}}
",
        );
        let findings = lint(&contract, "api", Compatibility::Surface);
        assert!(
            findings
                .iter()
                .all(|f| f.rule_id != "path-param-undeclared"),
            "should not fire when path param is properly declared"
        );
    }

    #[test]
    fn path_param_undeclared_accepts_path_item_level_parameters() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /payments/{id}:
    parameters:
      - name: id
        in: path
        required: true
        schema: {type: string}
    get:
      responses: {'200': {description: ok}}
",
        );
        let findings = lint(&contract, "api", Compatibility::Surface);
        assert!(
            findings
                .iter()
                .all(|f| f.rule_id != "path-param-undeclared"),
            "path-item-level parameters satisfy the path template"
        );
    }

    #[test]
    fn path_param_undeclared_not_fire_below_surface_level() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /payments/{id}:
    get:
      responses: {'200': {description: ok}}
",
        );
        let findings = lint(&contract, "api", Compatibility::WireJson);
        assert!(
            findings
                .iter()
                .all(|f| f.rule_id != "path-param-undeclared"),
            "Surface rule must not fire below Surface level"
        );
    }

    // ── path_template_params helper ──────────────────────────────────────────

    #[test]
    fn extracts_params_from_path_template() {
        let params: Vec<&str> =
            path_template_params("/payments/{id}/receipts/{receiptId}").collect();
        assert_eq!(params, vec!["id", "receiptId"]);
    }

    #[test]
    fn no_params_in_plain_path() {
        let params: Vec<&str> = path_template_params("/payments").collect();
        assert!(params.is_empty());
    }

    // ── enum-type-mismatch ───────────────────────────────────────────────────

    #[test]
    fn enum_type_mismatch_fires_on_string_in_integer_enum() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /a:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                type: integer
                enum: [1, 2, three]
",
        );
        let findings = lint(&contract, "api", Compatibility::Wire);
        let ids: Vec<_> = findings.iter().map(|f| f.rule_id).collect();
        assert!(
            ids.contains(&"enum-type-mismatch"),
            "expected enum-type-mismatch, got: {ids:?}"
        );
    }

    #[test]
    fn enum_type_mismatch_silent_when_values_match_type() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /a:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                type: string
                enum: [active, inactive]
",
        );
        let findings = lint(&contract, "api", Compatibility::Wire);
        assert!(
            findings.iter().all(|f| f.rule_id != "enum-type-mismatch"),
            "should not fire when all enum values match declared type"
        );
    }

    // ── enum-duplicate-value ─────────────────────────────────────────────────

    #[test]
    fn enum_duplicate_value_fires_on_repeated_member() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /a:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                type: string
                enum: [active, inactive, active]
",
        );
        let findings = lint(&contract, "api", Compatibility::Wire);
        let ids: Vec<_> = findings.iter().map(|f| f.rule_id).collect();
        assert!(
            ids.contains(&"enum-duplicate-value"),
            "expected enum-duplicate-value, got: {ids:?}"
        );
    }

    #[test]
    fn enum_duplicate_value_silent_when_all_unique() {
        let contract = parse(
            "openapi: 3.1.0
info: {title: T, version: 0}
paths:
  /a:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                type: string
                enum: [active, inactive, pending]
",
        );
        let findings = lint(&contract, "api", Compatibility::Wire);
        assert!(
            findings.iter().all(|f| f.rule_id != "enum-duplicate-value"),
            "should not fire when all enum values are unique"
        );
    }
}