|
| 1 | +# Array fields |
| 2 | + |
| 3 | +Representing lists of data in an API is trickier than it often appears. Users |
| 4 | +often need to modify lists in place, and longer data series within a single |
| 5 | +resource pose a challenge for pagination. |
| 6 | + |
| 7 | +## Guidance |
| 8 | + |
| 9 | +Resources **may** use array fields where appropriate. |
| 10 | + |
| 11 | +```typescript |
| 12 | +interface Book { |
| 13 | + // The resource name for the book. |
| 14 | + name: string; |
| 15 | + |
| 16 | + // The authors of the book. |
| 17 | + authors: string[]; |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +- Array fields **must** use a plural field name. |
| 22 | + - If the English singular and plural words are identical ("moose", "info"), |
| 23 | + the dictionary word **must** be used rather than attempting to coin a new |
| 24 | + plural form. |
| 25 | +- Array fields **should** have an enforced upper bound that will not cause a |
| 26 | + single resource payload to become too large. A good rule of thumb is 100 |
| 27 | + elements. |
| 28 | + - If an array data can not be bounded (in other words, if there is a chance |
| 29 | + that the array will be too large to be reasonably returned in a single |
| 30 | + request), the API **should** use a sub-resource instead. |
| 31 | +- Array fields **must not** represent the body of another resource inline. |
| 32 | + Instead, the field **should** be a array of strings providing the resource |
| 33 | + names of the associated resources. |
| 34 | + |
| 35 | +**Note:** This document uses the term "array" to refer to a field on a resource |
| 36 | +that is a list of elements that have the same type. Some languages and IDLs use |
| 37 | +other terms, such as "list", "repeated", or "sequence", and these terms are all |
| 38 | +synonymous for the purposes of this document. The term "collection" is |
| 39 | +distinct, and refers to a group of resources under a single parent rather than |
| 40 | +a field on a resource. |
| 41 | + |
| 42 | +### Scalars and structs |
| 43 | + |
| 44 | +Array fields **should** use a scalar type (such as `string`) if they are |
| 45 | +certain that additional data will not be needed in the future, as using a |
| 46 | +struct type adds significant cognitive overhead and leads to more complicated |
| 47 | +code. |
| 48 | + |
| 49 | +However, if additional data is likely to be needed in the future, array fields |
| 50 | +**should** use a struct instead of a scalar proactively, to avoid parallel |
| 51 | +array fields. |
| 52 | + |
| 53 | +### Update strategies |
| 54 | + |
| 55 | +A resource **may** use one of two strategies to enable updating a array field: |
| 56 | +direct update using the [standard `Update`][aip-134] method, or custom `Add` |
| 57 | +and `Remove` methods. |
| 58 | + |
| 59 | +A standard `Update` method has one key limitation: the user is only able to |
| 60 | +update _the entire_ array. This means that the user is required to read the |
| 61 | +resource, make modifications to the array field value as needed, and send it |
| 62 | +back. This is fine for many situations, particularly when the array field is |
| 63 | +expected to have a small size (fewer than 10 or so) and race conditions are not |
| 64 | +an issue, or can be guarded against with [ETags][aip-154]. |
| 65 | + |
| 66 | +**Note:** Declarative-friendly resources (AIP-128) **must** use the standard |
| 67 | +`Update` method, and not introduce `Add` and `Remove` methods. If declarative |
| 68 | +tools need to reason about particular relationships while ignoring others, |
| 69 | +consider using a subresource instead. |
| 70 | + |
| 71 | +If atomic modifications are required, and if the array is functionally a set |
| 72 | +(meaning that order does not matter, duplicate values are not meaningful, and |
| 73 | +non-comparable values such as `null` or `NaN` are not used), the API **should** |
| 74 | +define custom methods using the verbs `Add` and `Remove`: |
| 75 | + |
| 76 | +{% tab proto %} |
| 77 | + |
| 78 | +{% sample 'add_remove.proto', 'rpc AddAuthor', 'rpc RemoveAuthor' %} |
| 79 | + |
| 80 | +- The data being added or removed **should** be a primitive (usually a |
| 81 | + `string`). |
| 82 | + - For more complex data structures with a primary key, the API **should** use |
| 83 | + a map with the `Update` method instead. |
| 84 | +- The RPC's name **must** begin with the word `Add` or `Remove`. The remainder |
| 85 | + of the RPC name **should** be the singular form of the field being added. |
| 86 | +- The response **should** be the resource itself, and **should** fully-populate |
| 87 | + the resource structure. |
| 88 | +- The HTTP method **must** be `POST`, as usual for [custom methods][aip-136]. |
| 89 | +- The HTTP URI **must** end with `:add*` or `:remove*`, where `*` is the |
| 90 | + camel-case singular name of the field being added or removed. |
| 91 | +- The request field receiving the resource name **should** map to the URI path. |
| 92 | + - The HTTP variable **should** be the name of the resource (such as `book`) |
| 93 | + rather than `name` or `parent`. |
| 94 | + - That variable **should** be the only variable in the URI path. |
| 95 | +- The body clause in the `google.api.http` annotation **should** be `"*"`. |
| 96 | +- If the data being added in an `Add` operation is already present, the method |
| 97 | + **should** accept the request and make no changes (no-op), but **may** error |
| 98 | + with `ALREADY_EXISTS` if appropriate. |
| 99 | +- If the data being removed in a `Remove` operation is not present, the method |
| 100 | + **should** accept the request and make no changes (no-op), but **may** error |
| 101 | + with `NOT_FOUND` if appropriate. |
| 102 | + |
| 103 | +{% tab oas %} |
| 104 | + |
| 105 | +{% sample 'add_remove.oas.yaml', 'paths' %} |
| 106 | + |
| 107 | +- The data being added or removed **should** be a primitive (usually a |
| 108 | + `string`). |
| 109 | + - For more complex data structures with a primary key, the API **should** use |
| 110 | + a map with the `Update` method instead. |
| 111 | +- The `operationId` **must** begin with the word `add` or `remove`. The |
| 112 | + remainder of the `operationId` **should** be the singular form of the field |
| 113 | + being added. |
| 114 | +- The response **should** be the resource itself, and **should** fully-populate |
| 115 | + the resource structure. |
| 116 | +- The HTTP method **must** be `POST`, as usual for [custom methods][aip-136]. |
| 117 | +- The HTTP URI **must** end with `:add*` or `:remove*`, where `*` is the |
| 118 | + camel-case singular name of the field being added or removed. |
| 119 | +- If the data being added in an `Add` operation is already present, the method |
| 120 | + **should** accept the request and make no changes (no-op), but **may** error |
| 121 | + with `409 Conflict` if appropriate. |
| 122 | +- If the data being removed in a `Remove` operation is not present, the method |
| 123 | + **should** accept the request and make no changes (no-op), but **may** error |
| 124 | + with `404 Not Found` if appropriate. |
| 125 | + |
| 126 | +{% endtabs %} |
| 127 | + |
| 128 | +**Note:** If both of these strategies are too restrictive, consider using a |
| 129 | +subresource instead. |
| 130 | + |
| 131 | +#### Request Structure |
| 132 | + |
| 133 | +{% tab proto %} |
| 134 | + |
| 135 | +{% sample 'add_remove.proto', 'message AddAuthorRequest', 'message RemoveAuthorRequest' %} |
| 136 | + |
| 137 | +- A resource field **must** be included. It **should** be the name of the |
| 138 | + resource (such as `book`) rather than `name` or `parent`. |
| 139 | + - The field **should** be [annotated as required][aip-203]. |
| 140 | + - If the field represents the name of another resource, it **should** |
| 141 | + identify the [resource type][aip-123] that it references. |
| 142 | +- A field for the value being added or removed **must** be included. It |
| 143 | + **should** be the singular name of the field. |
| 144 | + - The field **should** be [annotated as required][aip-203]. |
| 145 | +- The request message **must not** contain any other required fields, and |
| 146 | + **should not** contain other optional fields except those described in this |
| 147 | + or another AIP. |
| 148 | + |
| 149 | +{% tab oas %} |
| 150 | + |
| 151 | +{% sample 'add_remove.oas.yaml', 'requestBody' %} |
| 152 | + |
| 153 | +- A field for the value being added or removed **must** be included. It |
| 154 | + **should** be the singular name of the field. |
| 155 | + - The field **should** be designated as required. |
| 156 | + |
| 157 | +{% endtabs %} |
0 commit comments