github.com/googleapis/api-linter@v1.65.2/docs/rules/0124/reference-same-package.md (about)

     1  ---
     2  rule:
     3    aip: 124
     4    name: [core, '0124', reference-same-package]
     5    summary: Resource references should refer to resources in the same package.
     6  permalink: /124/reference-same-package
     7  redirect_from:
     8    - /0124/reference-same-package
     9  ---
    10  
    11  # Resource reference package
    12  
    13  This rule enforces that resource reference annotations refer resources defined
    14  in the same package, as described in [AIP-124][].
    15  
    16  ## Details
    17  
    18  This rule scans all fields with `google.api.resource_reference` annotations,
    19  and complains if the `type` on them refers to a resource that is defined in a
    20  different protobuf package.
    21  
    22  Certain common resource types are exempt from this rule.
    23  
    24  ## Examples
    25  
    26  **Incorrect** code for this rule:
    27  
    28  ```proto
    29  // Incorrect.
    30  package google.example.library.v1;
    31  
    32  message Book {
    33    option (google.api.resource) = {
    34      type: "library.googleapis.com/Book"
    35      pattern: "publishers/{publisher}/books/{book}"
    36    };
    37  
    38    string name = 1;
    39  
    40    // ...
    41  }
    42  ```
    43  
    44  ```proto
    45  // Incorrect.
    46  package google.example.libray.v1;  // Typo: Different package.
    47  
    48  message GetBookRequest {
    49    string name = 1 [(google.api.resource_reference) = {
    50      type: "library.googleapis.com/Book"  // Lint warning: package mismatch.
    51    }];
    52  }
    53  ```
    54  
    55  **Correct** code for this rule:
    56  
    57  ```proto
    58  // Correct.
    59  package google.example.library;
    60  
    61  message Book {
    62    option (google.api.resource) = {
    63      type: "library.googleapis.com/Book"
    64      pattern: "publishers/{publisher}/books/{book}"
    65    };
    66  
    67    string name = 1;
    68  
    69    // ...
    70  }
    71  
    72  message GetBookRequest {
    73    string name = 1 [(google.api.resource_reference) = {
    74      type: "library.googleapis.com/Book"
    75    }];
    76  }
    77  ```
    78  
    79  ## Disabling
    80  
    81  If you need to violate this rule, use a leading comment above the field.
    82  Remember to also include an [aip.dev/not-precedent][] comment explaining why.
    83  
    84  ```proto
    85  package google.example.library.common;
    86  
    87  message Book {
    88    option (google.api.resource) = {
    89      type: "library.googleapis.com/Book"
    90      pattern: "publishers/{publisher}/books/{book}"
    91    };
    92  }
    93  ```
    94  
    95  ```proto
    96  package google.example.library.v1;
    97  
    98  message GetBookRequest {
    99    // (-- api-linter: core::0124::reference-same-package=disabled
   100    //     aip.dev/not-precedent: We need to do this because reasons. --)
   101    string name = 1 [(google.api.resource_reference) = {
   102      type: "library.googleapis.com/Book"
   103    }];
   104  }
   105  ```
   106  
   107  If you need to violate this rule for an entire file, place the comment at the
   108  top of the file.
   109  
   110  [aip-124]: http://aip.dev/124
   111  [aip.dev/not-precedent]: https://aip.dev/not-precedent