googleapis / googleapis/api-linter

Batch method rules do not respect google.api.resource singular annotation

Open
#1,628 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
765
Forks
181
Avg merge
2d 12h
Merged PRs (30d)
3

Description

## Description

The batch method rules for AIP-0231, 0233, 0234, and 0235 (`request-requests-field`, `request-names-field`, `response-resource-field`) do not consult the `google.api.resource` annotation when determining the singular form of a resource name. Instead, they rely solely on `go-pluralize` for singularization, which can produce incorrect results.

For example, given a resource with an explicit `singular` annotation:

```proto
message ImpressionMetadata {
option (google.api.resource) = {
type: "example.com/ImpressionMetadata"
singular: "impressionMetadata"
plural: "impressionMetadata"
};
}

message BatchUpdateImpressionMetadataRequest {
repeated UpdateImpressionMetadataRequest requests = 2;
}
```

The linter reports:
```
The "requests" field on Batch Update Request should be a "UpdateImpressionMetadatumRequest" type
```

The `singular: "impressionMetadata"` annotation is ignored, and `go-pluralize` incorrectly applies a Latin grammar rule (`(?i)(dat)a$` → `$1um`) to produce "Metadatum".

## Reproducible Example

Running the linter on the following proto incorrectly reports that `requests` should be of type `UpdateImpressionMetadatumRequest`, even though the resource annotation explicitly declares `singular: "impressionMetadata"`:

```proto
syntax = "proto3";

import "google/api/resource.proto";

message ImpressionMetadata {
option (google.api.resource) = {
type: "example.googleapis.com/ImpressionMetadata"
pattern: "dataProviders/{data_provider}/impressionMetadata/{impression_metadata}"
singular: "impressionMetadata"
plural: "impressionMetadata"
};
}

message BatchUpdateImpressionMetadataRequest {
repeated UpdateImpressionMetadataRequest requests = 1;
}

message UpdateImpressionMetadataRequest {}
```

## Root Cause

These 8 batch rules use `go-pluralize` directly to singularize the resource name extracted from the batch message name, without first checking if the resource message has a `google.api.resource` annotation with an explicit `singular` field.

This is the same class of bug that was fixed in #1573 for the `plural-method-name` rule, which now reads the `plural` field from `google.api.resource` before falling back to `go-pluralize`.

## Affected Rules

- `core::0231::request-names-field`
- `core::0231::response-resource-field`
- `core::0233::request-requests-field`
- `core::0233::response-resource-field`
- `core::0234::request-requests-field`
- `core::0234::response-resource-field`
- `core::0235::request-names-field`
- `core::0235::response-resource-field`

## Workaround

Disable the affected rules per-message with proto comments:
```proto
// (-- api-linter: core::0234::request-requests-field=disabled
// aip.dev/not-precedent: Linter does not respect google.api.resource
// singular annotation for batch method rules. --)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.