Skip to content

Commit

Permalink
Add rules for batch-method parent resource reference (#643)
Browse files Browse the repository at this point in the history
Co-authored-by: Luke Sneeringer <[email protected]>
  • Loading branch information
apasel422 and Luke Sneeringer authored Oct 5, 2020
1 parent b9364d8 commit d7f33f4
Show file tree
Hide file tree
Showing 27 changed files with 809 additions and 38 deletions.
20 changes: 16 additions & 4 deletions docs/rules/0231/request-names-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@ accepted in its place.
```proto
// Incorrect.
message BatchGetBooksRequest {
string parent = 1;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated string books = 2; // Field name should be `names`.
}
```

```proto
// Incorrect.
message BatchGetBooksRequest {
string parent = 1;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
string names = 2; // Field should be repeated.
}
```
Expand All @@ -47,8 +53,14 @@ message BatchGetBooksRequest {
```proto
// Correct.
message BatchGetBooksRequest {
string parent = 1;
repeated string names = 2;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated string names = 2 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

Expand Down
18 changes: 15 additions & 3 deletions docs/rules/0231/request-names-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ complains if it does not have a `google.api.resource_reference` annotation.
```proto
// Incorrect.
message BatchGetBooksRequest {
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
// The `google.api.resource_reference` annotation should also be included.
repeated string names = 1 [(google.api.field_behavior) = REQUIRED];
repeated string names = 2 [(google.api.field_behavior) = REQUIRED];
}
```

Expand All @@ -37,7 +41,11 @@ message BatchGetBooksRequest {
```proto
// Correct.
message BatchGetBooksRequest {
repeated string names = 1 [
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated string names = 2 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
Expand All @@ -51,9 +59,13 @@ Remember to also include an [aip.dev/not-precedent][] comment explaining why.

```proto
message BatchGetBooksRequest {
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
// (-- api-linter: core::0231::request-names-reference=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
repeated string names = 1 [(google.api.field_behavior) = REQUIRED];
repeated string names = 2 [(google.api.field_behavior) = REQUIRED];
}
```

Expand Down
28 changes: 23 additions & 5 deletions docs/rules/0231/request-parent-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@ either the `parent` field is missing, or if it has any type other than
// Incorrect.
message BatchGetBooksRequest {
string publisher = 1; // Field name should be `parent`.
repeated string names = 2;
repeated string names = 2 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

```proto
// Incorrect.
message BatchGetBooksRequest {
bytes parent = 1; // Field type should be `string`.
repeated string names = 2;
repeated string names = 2 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

Expand All @@ -44,8 +52,14 @@ message BatchGetBooksRequest {
```proto
// Correct.
message BatchGetBooksRequest {
string parent = 1;
repeated string names = 2;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated string names = 2 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

Expand All @@ -60,7 +74,11 @@ Remember to also include an [aip.dev/not-precedent][] comment explaining why.
// aip.dev/not-precedent: We need to do this because reasons. --)
message BatchGetBooksRequest {
string publisher = 1; // Field name should be `parent`.
repeated string names = 2;
repeated string names = 2 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

Expand Down
78 changes: 78 additions & 0 deletions docs/rules/0231/request-parent-reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
rule:
aip: 231
name: [core, '0231', request-parent-reference]
summary: |
Batch Get requests should annotate the `parent` field with `google.api.resource_reference`.
permalink: /231/request-parent-reference
redirect_from:
- /0231/request-parent-reference
---

# Batch Get methods: Parent resource reference

This rule enforces that all `BatchGet` requests have
`google.api.resource_reference` on their `string parent` field, as mandated in
[AIP-231][].

## Details

This rule looks at the `parent` field of any message matching `BatchGet*Request` and
complains if it does not have a `google.api.resource_reference` annotation.

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
message BatchGetBooksRequest {
// The `google.api.resource_reference` annotation should also be included.
string parent = 1;
repeated string names = 2 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

**Correct** code for this rule:

```proto
// Correct.
message BatchGetBooksRequest {
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated string names = 2 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

## Disabling

If you need to violate this rule, use a leading comment above the field.
Remember to also include an [aip.dev/not-precedent][] comment explaining why.

```proto
message BatchGetBooksRequest {
// (-- api-linter: core::0231::request-parent-reference=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
string parent = 1;
repeated string names = 2 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library.googleapis.com/Book"
];
}
```

If you need to violate this rule for an entire file, place the comment at the
top of the file.

[aip-231]: https://aip.dev/231
[aip.dev/not-precedent]: https://aip.dev/not-precedent
5 changes: 4 additions & 1 deletion docs/rules/0233/request-parent-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ message BatchCreateBooksRequest {
```proto
// Correct.
message BatchCreateBooksRequest {
string parent = 1;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated CreateBookRequest requests = 2;
}
```
Expand Down
69 changes: 69 additions & 0 deletions docs/rules/0233/request-parent-reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
rule:
aip: 233
name: [core, '0233', request-parent-reference]
summary: |
Batch Create requests should annotate the `parent` field with `google.api.resource_reference`.
permalink: /233/request-parent-reference
redirect_from:
- /0233/request-parent-reference
---

# Batch Create methods: Parent resource reference

This rule enforces that all `BatchCreate` requests have
`google.api.resource_reference` on their `string parent` field, as mandated in
[AIP-233][].

## Details

This rule looks at the `parent` field of any message matching `BatchCreate*Request` and
complains if it does not have a `google.api.resource_reference` annotation.

## Examples

**Incorrect** code for this rule:

```proto
// Incorrect.
message BatchCreateBooksRequest {
// The `google.api.resource_reference` annotation should also be included.
string parent = 1;
repeated CreateBookRequest requests = 2;
}
```

**Correct** code for this rule:

```proto
// Correct.
message BatchCreateBooksRequest {
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated CreateBookRequest requests = 2;
}
```

## Disabling

If you need to violate this rule, use a leading comment above the field.
Remember to also include an [aip.dev/not-precedent][] comment explaining why.

```proto
message BatchCreateBooksRequest {
// (-- api-linter: core::0233::request-parent-reference=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
string parent = 1;
repeated CreateBookRequest requests = 2;
}
```

If you need to violate this rule for an entire file, place the comment at the
top of the file.

[aip-233]: https://aip.dev/233
[aip.dev/not-precedent]: https://aip.dev/not-precedent
20 changes: 16 additions & 4 deletions docs/rules/0233/request-requests-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ or if it is not `repeated`.
```proto
// Incorrect.
message BatchCreateBooksRequest {
string parent = 1;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated CreateBookRequest req = 2; // Field name should be `requests`.
}
```

```proto
// Incorrect.
message BatchCreateBooksRequest {
string parent = 1;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
CreateBookRequest requests = 2; // Field should be repeated.
}
```
Expand All @@ -45,7 +51,10 @@ message BatchCreateBooksRequest {
```proto
// Correct.
message BatchCreateBooksRequest {
string parent = 1;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated CreateBookRequest requests = 2;
}
```
Expand All @@ -60,7 +69,10 @@ Remember to also include an [aip.dev/not-precedent][] comment explaining why.
// (-- api-linter: core::0233::request-requests-field=disabled
// aip.dev/not-precedent: We need to do this because reasons. --)
message BatchCreateBooksRequest {
string parent = 1;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated string books = 2; // should be "repeated CreateBookRequest requests"
}
```
Expand Down
5 changes: 4 additions & 1 deletion docs/rules/0234/request-parent-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ message BatchUpdateBooksRequest {
```proto
// Correct.
message BatchUpdateBooksRequest {
string parent = 1;
string parent = 1 [
(google.api.resource_reference).child_type = "library.googleapis.com/Book"
];
repeated UpdateBookRequest requests = 2;
}
```
Expand Down
Loading

0 comments on commit d7f33f4

Please sign in to comment.