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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! Protobuf 3 `.proto` source → `Contract`.
//!
//! The governing fact of this ingester is that **protobuf compatibility is
//! defined by field number, not by field name**. A field renamed with a stable
//! number is wire-compatible; a field renumbered with a stable name is a hard
//! break that no name-based diff can see. Every field and enum value therefore
//! carries its number into the model, and `compare/` uses it as the identity.

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

use prost_types::{DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, SourceCodeInfo};
use thiserror::Error;

use super::{
    Constraints, Contract, Endpoint, EndpointKey, Field, MEDIA_GRPC, MEDIA_GRPC_STREAM, Payload,
    Span, TypeRef, Unmodelled, UnmodelledKind,
};

/// Field descriptor label values, from `descriptor.proto`.
const LABEL_OPTIONAL: i32 = 1;
const LABEL_REQUIRED: i32 = 2;
const LABEL_REPEATED: i32 = 3;

/// `FileDescriptorProto` field numbers, for `source_code_info` paths.
const PATH_SERVICE: i32 = 6;
const PATH_SERVICE_METHOD: i32 = 2;

#[derive(Debug, Error)]
pub enum ProtoError {
    #[error("contract source `{contract_source}` is not valid UTF-8: {error}")]
    InvalidUtf8 {
        contract_source: String,
        error: std::str::Utf8Error,
    },
    #[error("failed to parse protobuf contract `{contract_source}`: {error}")]
    Parse {
        contract_source: String,
        error: Box<protox_parse::ParseError>,
    },
}

pub fn ingest(source: &str, bytes: &[u8]) -> Result<Contract, ProtoError> {
    ingest_with_resolver(source, bytes, &crate::contract::SingleDocumentResolver)
}

pub fn ingest_with_resolver(
    source: &str,
    bytes: &[u8],
    _resolver: &dyn crate::contract::DocumentResolver,
) -> Result<Contract, ProtoError> {
    let input = std::str::from_utf8(bytes).map_err(|error| ProtoError::InvalidUtf8 {
        contract_source: source.to_owned(),
        error,
    })?;
    let descriptor = protox_parse::parse(source, input).map_err(|error| ProtoError::Parse {
        contract_source: source.to_owned(),
        error: Box::new(error),
    })?;

    let package = descriptor.package.clone().unwrap_or_default();
    let mut registry = Registry {
        source: source.to_owned(),
        package: package.clone(),
        messages: BTreeMap::new(),
        enums: BTreeMap::new(),
        unmodelled: Vec::new(),
    };
    registry.collect_messages(&package, &descriptor.message_type);
    registry.collect_enums(&package, &descriptor.enum_type);

    let spans = Spans::new(source, descriptor.source_code_info.as_ref());
    let mut contract = Contract::empty();

    for (service_index, service) in descriptor.service.iter().enumerate() {
        let Some(service_name) = service.name.as_ref() else {
            continue;
        };
        let scoped_service = qualify(&package, service_name);

        for (method_index, method) in service.method.iter().enumerate() {
            let Some(method_name) = method.name.as_ref() else {
                continue;
            };
            let pointer = format!("/service/{service_index}/method/{method_index}");
            let span = spans.for_path(
                &[
                    PATH_SERVICE,
                    service_index as i32,
                    PATH_SERVICE_METHOD,
                    method_index as i32,
                ],
                &pointer,
            );

            // A method that changes between unary and streaming changes the
            // call shape a generated client uses, in both directions.
            let request_media = if method.client_streaming.unwrap_or(false) {
                MEDIA_GRPC_STREAM
            } else {
                MEDIA_GRPC
            };
            let response_media = if method.server_streaming.unwrap_or(false) {
                MEDIA_GRPC_STREAM
            } else {
                MEDIA_GRPC
            };

            let request = method.input_type.as_deref().map(|name| Payload {
                media_types: BTreeMap::from([(
                    request_media.to_owned(),
                    registry.resolve(name, &mut BTreeSet::new()),
                )]),
                span: span.clone(),
            });
            let mut responses = BTreeMap::new();
            if let Some(output) = method.output_type.as_deref() {
                responses.insert(
                    "200".to_owned(),
                    Payload {
                        media_types: BTreeMap::from([(
                            response_media.to_owned(),
                            registry.resolve(output, &mut BTreeSet::new()),
                        )]),
                        span: span.clone(),
                    },
                );
            }

            contract.endpoints.insert(
                EndpointKey {
                    method: "RPC".to_owned(),
                    path: format!("/{scoped_service}/{method_name}"),
                },
                Endpoint {
                    operation_id: Some(format!("{scoped_service}.{method_name}")),
                    deprecated: method
                        .options
                        .as_ref()
                        .and_then(|options| options.deprecated)
                        .unwrap_or(false),
                    sunset: None,
                    parameters: Vec::new(),
                    request,
                    responses,
                    security: Vec::new(),
                    span,
                },
            );
        }
    }

    contract.unmodelled = registry.unmodelled;
    contract
        .unmodelled
        .sort_by(|a, b| a.pointer.cmp(&b.pointer).then_with(|| a.kind.cmp(&b.kind)));
    contract.unmodelled.dedup();
    Ok(contract)
}

struct Registry {
    source: String,
    /// The file's `package`, which is the outermost scope a relative type
    /// reference is resolved against.
    package: String,
    messages: BTreeMap<String, DescriptorProto>,
    enums: BTreeMap<String, EnumDescriptorProto>,
    unmodelled: Vec<Unmodelled>,
}

impl Registry {
    fn collect_messages(&mut self, parent: &str, descriptors: &[DescriptorProto]) {
        for descriptor in descriptors {
            let Some(name) = descriptor.name.as_ref() else {
                continue;
            };
            let fq = qualify(parent, name);
            self.messages.insert(fq.clone(), descriptor.clone());
            self.collect_messages(&fq, &descriptor.nested_type);
            self.collect_enums(&fq, &descriptor.enum_type);
        }
    }

    fn collect_enums(&mut self, parent: &str, descriptors: &[EnumDescriptorProto]) {
        for descriptor in descriptors {
            let Some(name) = descriptor.name.as_ref() else {
                continue;
            };
            self.enums.insert(qualify(parent, name), descriptor.clone());
        }
    }

    fn record(&mut self, kind: UnmodelledKind, pointer: &str) -> TypeRef {
        self.unmodelled.push(Unmodelled {
            kind: kind.clone(),
            pointer: pointer.to_owned(),
            span: Span::new(&self.source, 1, 1, pointer),
        });
        TypeRef::Unknown(kind)
    }

    /// Resolve a type reference the way protoc does.
    ///
    /// `protox_parse` is a parse-only step and leaves references as written,
    /// so `rpc Get(Req)` arrives as `Req` rather than `.payments.Req`. A
    /// leading dot means fully qualified; otherwise the innermost enclosing
    /// scope wins and the search widens outward to the package root.
    fn qualify_reference(&self, type_name: &str) -> String {
        if let Some(absolute) = type_name.strip_prefix('.') {
            return absolute.to_owned();
        }

        let mut scope = self.package.as_str();
        loop {
            let candidate = qualify(scope, type_name);
            if self.messages.contains_key(&candidate) || self.enums.contains_key(&candidate) {
                return candidate;
            }
            match scope.rfind('.') {
                Some(cut) => scope = &scope[..cut],
                None => break,
            }
        }
        type_name.to_owned()
    }

    fn resolve(&mut self, type_name: &str, visiting: &mut BTreeSet<String>) -> TypeRef {
        let normalized = self.qualify_reference(type_name);

        if let Some(message) = self.messages.get(&normalized).cloned() {
            if !visiting.insert(normalized.clone()) {
                return TypeRef::Cycle(normalized);
            }
            let object = self.object_for(&normalized, &message, visiting);
            visiting.remove(&normalized);
            return object;
        }

        if let Some(enum_descriptor) = self.enums.get(&normalized).cloned() {
            let mut values = BTreeSet::new();
            let mut numbers = BTreeMap::new();
            for value in &enum_descriptor.value {
                let Some(name) = value.name.clone() else {
                    continue;
                };
                if let Some(number) = value.number {
                    numbers.insert(name.clone(), number);
                }
                values.insert(name);
            }

            for range in &enum_descriptor.reserved_range {
                if let (Some(start), Some(end)) = (range.start, range.end) {
                    let reused = numbers
                        .values()
                        .any(|number| *number >= start && *number <= end);
                    if reused {
                        return self.record(
                            UnmodelledKind::Unsupported(format!(
                                "an enum value reuses reserved number range {start}..{end}"
                            )),
                            &format!("/enum/{normalized}/reserved"),
                        );
                    }
                }
            }
            for name in &enum_descriptor.reserved_name {
                if values.contains(name) {
                    return self.record(
                        UnmodelledKind::Unsupported(format!(
                            "an enum value reuses reserved name `{name}`"
                        )),
                        &format!("/enum/{normalized}/reserved"),
                    );
                }
            }

            return TypeRef::Enum { values, numbers };
        }

        // An unresolved type is almost always an `import` this ingester was
        // not given. Reporting it keeps the run honest: bytes-only ingest
        // cannot read the imported file, and pretending the type matched
        // would be a false clean.
        self.record(
            UnmodelledKind::ExternalRef(normalized.clone()),
            &format!("/type/{normalized}"),
        )
    }

    fn object_for(
        &mut self,
        message_name: &str,
        message: &DescriptorProto,
        visiting: &mut BTreeSet<String>,
    ) -> TypeRef {
        // A `map<k, v>` is compiled to a synthetic repeated entry message.
        // Both sides normalise the same way, so it compares consistently.
        let mut fields = BTreeMap::new();
        for field in &message.field {
            let Some(name) = field.name.clone() else {
                continue;
            };
            let ty = self.field_type(message_name, field, visiting);
            fields.insert(
                name,
                Field {
                    ty,
                    // proto3 has no `required`; presence is what `optional`
                    // and message-typed fields express, and neither makes a
                    // field mandatory on the wire.
                    required: field.label == Some(LABEL_REQUIRED),
                    // Field-level `source_code_info` paths need the enclosing
                    // message's index threaded through the registry. Not done:
                    // a proto finding falls back to the method's span, which
                    // is the current behaviour and still the right file.
                    span: None,
                    deprecated: field
                        .options
                        .as_ref()
                        .and_then(|options| options.deprecated)
                        .unwrap_or(false),
                    number: field.number,
                },
            );
        }

        // A number or name inside a `reserved` declaration must never come back:
        // reusing it makes new data decode as the old field in deployed clients.
        for range in &message.reserved_range {
            if let (Some(start), Some(end)) = (range.start, range.end) {
                for (name, field) in fields.iter_mut() {
                    if let Some(num) = field.number
                        && num >= start
                        && num < end
                    {
                        field.ty = self.record(
                            UnmodelledKind::Unsupported(format!(
                                "a field reuses reserved number range {start}..{end}"
                            )),
                            &format!("/message/{message_name}/field/{name}"),
                        );
                    }
                }
            }
        }
        for name in &message.reserved_name {
            if let Some(field) = fields.get_mut(name) {
                field.ty = self.record(
                    UnmodelledKind::Unsupported(format!("a field reuses reserved name `{name}`")),
                    &format!("/message/{message_name}/field/{name}"),
                );
            }
        }

        TypeRef::Object {
            fields,
            additional: false,
            nullable: false,
        }
    }

    fn field_type(
        &mut self,
        message_name: &str,
        field: &FieldDescriptorProto,
        visiting: &mut BTreeSet<String>,
    ) -> TypeRef {
        let pointer = format!(
            "/message/{message_name}/field/{}",
            field.name.clone().unwrap_or_default()
        );
        // `type_name` is checked first because `protox_parse` is parse-only:
        // it cannot know whether `Status` names a message or an enum without
        // resolution, so it leaves `type` unset and only fills `type_name`.
        let mut ty = if let Some(type_name) = field.type_name.as_deref() {
            self.resolve(type_name, visiting)
        } else if let Some((name, format)) = field.r#type.and_then(scalar_for_proto_type) {
            TypeRef::Scalar {
                ty: name.to_owned(),
                format: format.map(ToOwned::to_owned),
                nullable: false,
                constraints: Constraints::default(),
            }
        } else {
            self.record(UnmodelledKind::InvalidShape, &pointer)
        };

        if field.label == Some(LABEL_REPEATED) {
            ty = TypeRef::Array {
                items: Box::new(ty),
                nullable: false,
            };
        } else if field.label == Some(LABEL_OPTIONAL)
            && field.proto3_optional.unwrap_or(false)
            && let TypeRef::Scalar {
                ty: name,
                format,
                constraints,
                ..
            } = ty
        {
            // `optional` in proto3 restores explicit presence, which is the
            // closest thing the format has to nullability.
            ty = TypeRef::Scalar {
                ty: name,
                format,
                nullable: true,
                constraints,
            };
        }
        ty
    }
}

/// Proto scalar types, preserving the distinctions that matter on the wire.
///
/// `int32` and `int64` are wire-compatible with each other and are not
/// collapsed here anyway: keeping the declared name means a change between
/// signed and unsigned, or fixed and varint, is visible rather than lost to a
/// coarse "integer" bucket.
fn scalar_for_proto_type(value: i32) -> Option<(&'static str, Option<&'static str>)> {
    let mapped = match value {
        1 => ("number", Some("double")),
        2 => ("number", Some("float")),
        3 => ("integer", Some("int64")),
        4 => ("integer", Some("uint64")),
        5 => ("integer", Some("int32")),
        6 => ("integer", Some("fixed64")),
        7 => ("integer", Some("fixed32")),
        8 => ("boolean", None),
        9 => ("string", None),
        12 => ("string", Some("bytes")),
        13 => ("integer", Some("uint32")),
        15 => ("integer", Some("sfixed32")),
        16 => ("integer", Some("sfixed64")),
        17 => ("integer", Some("sint32")),
        18 => ("integer", Some("sint64")),
        // 10 group, 11 message, 14 enum — resolved through `type_name`.
        _ => return None,
    };
    Some(mapped)
}

/// Source locations from the descriptor's `source_code_info`.
///
/// Without this every protobuf finding pointed at line 1, which makes a SARIF
/// annotation useless and a text diagnostic misleading.
struct Spans<'a> {
    source: &'a str,
    info: Option<&'a SourceCodeInfo>,
}

impl<'a> Spans<'a> {
    fn new(source: &'a str, info: Option<&'a SourceCodeInfo>) -> Self {
        Self { source, info }
    }

    fn for_path(&self, path: &[i32], pointer: &str) -> Span {
        let located = self.info.and_then(|info| {
            info.location
                .iter()
                .find(|location| location.path == path)
                .and_then(|location| {
                    // span is [start_line, start_col, end_col] or
                    // [start_line, start_col, end_line, end_col], zero-based.
                    let line = *location.span.first()? as usize + 1;
                    let column = *location.span.get(1)? as usize + 1;
                    Some((line, column))
                })
        });
        let (line, column) = located.unwrap_or((1, 1));
        Span::new(self.source, line, column, pointer)
    }
}

fn qualify(parent: &str, name: &str) -> String {
    if parent.is_empty() {
        name.to_owned()
    } else {
        format!("{parent}.{name}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compare::{ChangeKind, compare_contracts};

    fn body(fields: &str, service: &str) -> String {
        format!(
            r#"
syntax = "proto3";
package payments;

message Payment {{
{fields}
}}

message Req {{
  string id = 1;
}}

service PaymentService {{
{service}
}}
"#
        )
    }

    fn kinds(base: &str, head: &str) -> Vec<ChangeKind> {
        let base = ingest("api/payments.proto", base.as_bytes()).expect("base");
        let head = ingest("api/payments.proto", head.as_bytes()).expect("head");
        compare_contracts(&base, &head)
            .iter()
            .map(|change| change.kind)
            .collect()
    }

    #[test]
    fn extracts_services_messages_and_field_numbers() {
        let source = body(
            "  string id = 1;\n  int32 amount = 2;",
            "  rpc Get(Req) returns (Payment);",
        );
        let contract = ingest("api/service.proto", source.as_bytes()).expect("ingest");

        assert_eq!(contract.endpoints.len(), 1);
        let endpoint = &contract.endpoints[&EndpointKey {
            method: "RPC".to_owned(),
            path: "/payments.PaymentService/Get".to_owned(),
        }];
        assert_eq!(
            endpoint.operation_id.as_deref(),
            Some("payments.PaymentService.Get")
        );

        let TypeRef::Object { fields, .. } = &endpoint.responses["200"].media_types[MEDIA_GRPC]
        else {
            panic!("response should be an object");
        };
        assert_eq!(fields["id"].number, Some(1));
        assert_eq!(fields["amount"].number, Some(2));
        assert!(contract.unmodelled.is_empty());
    }

    #[test]
    fn renumbering_a_field_is_a_wire_break() {
        let kinds = kinds(
            &body("  string id = 1;", "  rpc Get(Req) returns (Payment);"),
            &body("  string id = 7;", "  rpc Get(Req) returns (Payment);"),
        );
        assert!(
            kinds.contains(&ChangeKind::FieldNumberChanged),
            "renumbering is the canonical protobuf break: {kinds:?}"
        );
    }

    #[test]
    fn renaming_a_field_with_a_stable_number_is_a_rename_not_a_removal() {
        let kinds = kinds(
            &body("  string id = 1;", "  rpc Get(Req) returns (Payment);"),
            &body(
                "  string identifier = 1;",
                "  rpc Get(Req) returns (Payment);",
            ),
        );
        assert!(kinds.contains(&ChangeKind::FieldRenamed));
        assert!(
            !kinds.contains(&ChangeKind::ResponseFieldRemoved),
            "a stable wire number means the field survived: {kinds:?}"
        );
    }

    #[test]
    fn identical_files_produce_nothing() {
        let source = body("  string id = 1;", "  rpc Get(Req) returns (Payment);");
        assert!(kinds(&source, &source).is_empty());
    }

    #[test]
    fn removing_an_rpc_is_an_endpoint_removal() {
        let kinds = kinds(
            &body("  string id = 1;", "  rpc Get(Req) returns (Payment);"),
            &body("  string id = 1;", ""),
        );
        assert!(kinds.contains(&ChangeKind::EndpointRemoved));
    }

    #[test]
    fn switching_a_method_to_streaming_is_visible() {
        let kinds = kinds(
            &body("  string id = 1;", "  rpc Get(Req) returns (Payment);"),
            &body(
                "  string id = 1;",
                "  rpc Get(Req) returns (stream Payment);",
            ),
        );
        assert!(
            kinds.contains(&ChangeKind::ResponseMediaTypeRemoved),
            "a unary method becoming streaming changes every client: {kinds:?}"
        );
    }

    #[test]
    fn enum_value_renumbering_is_a_wire_break() {
        let base = r#"
syntax = "proto3";
package payments;
enum Status { UNKNOWN = 0; PAID = 1; }
message Payment { Status status = 1; }
message Req { string id = 1; }
service S { rpc Get(Req) returns (Payment); }
"#;
        let head = r#"
syntax = "proto3";
package payments;
enum Status { UNKNOWN = 0; PAID = 5; }
message Payment { Status status = 1; }
message Req { string id = 1; }
service S { rpc Get(Req) returns (Payment); }
"#;
        let kinds = kinds(base, head);
        assert!(
            kinds.contains(&ChangeKind::FieldNumberChanged),
            "renumbering an enum value changes what the bytes mean: {kinds:?}"
        );
    }

    #[test]
    fn an_unresolved_import_is_reported_not_assumed_compatible() {
        let source = r#"
syntax = "proto3";
package payments;
import "other/common.proto";
message Req { string id = 1; }
message Payment { common.Money amount = 1; }
service S { rpc Get(Req) returns (Payment); }
"#;
        let contract = ingest("api/payments.proto", source.as_bytes()).expect("ingest");
        assert!(
            !contract.unmodelled.is_empty(),
            "a type from an unread import must not compare as verified"
        );
    }

    #[test]
    fn narrowing_an_integer_format_is_visible() {
        let kinds = kinds(
            &body("  int64 amount = 1;", "  rpc Get(Req) returns (Payment);"),
            &body("  uint32 amount = 1;", "  rpc Get(Req) returns (Payment);"),
        );
        assert!(
            kinds.contains(&ChangeKind::ResponseTypeChanged),
            "int64 to uint32 changes what values can be represented: {kinds:?}"
        );
    }

    #[test]
    fn reusing_reserved_field_name_or_enum_range_is_unmodelled() {
        let message_src = r#"
syntax = "proto3";
package payments;
message Req { string id = 1; }
message Payment {
  reserved "old_name";
  string old_name = 1;
}
service S { rpc Get(Req) returns (Payment); }
"#;
        let contract = ingest("api/payments.proto", message_src.as_bytes()).expect("ingest");
        assert!(
            contract
                .unmodelled
                .iter()
                .any(|u| matches!(&u.kind, UnmodelledKind::Unsupported(msg) if msg.contains("reuses reserved name `old_name`"))),
            "reusing reserved field name must be recorded: {:?}",
            contract.unmodelled
        );

        let enum_src = r#"
syntax = "proto3";
package payments;
enum Status {
  reserved 1;
  UNKNOWN = 0;
  PAID = 1;
}
message Req { string id = 1; }
message Payment { Status status = 1; }
service S { rpc Get(Req) returns (Payment); }
"#;
        let enum_contract = ingest("api/payments.proto", enum_src.as_bytes()).expect("ingest");
        assert!(
            enum_contract
                .unmodelled
                .iter()
                .any(|u| matches!(&u.kind, UnmodelledKind::Unsupported(msg) if msg.contains("reuses reserved number range"))),
            "reusing reserved enum number range must be recorded: {:?}",
            enum_contract.unmodelled
        );
    }

    #[test]
    fn spans_point_at_the_method_not_line_one() {
        let source = body("  string id = 1;", "  rpc Get(Req) returns (Payment);");
        let contract = ingest("api/service.proto", source.as_bytes()).expect("ingest");
        let endpoint = contract.endpoints.values().next().expect("one endpoint");
        assert!(
            endpoint.span.line > 1,
            "protobuf spans must locate the method, got line {}",
            endpoint.span.line
        );
    }
}