Commit bc6c7e1
fix: Remove null check for paged Protobuf messages (#3164)
## Background
This was caught from ErrorProne flagging an
[ImpossibleNullComparison](https://errorprone.info/bugpattern/ImpossibleNullComparison)
when upgrading to Protobuf 4.27.4 runtime. Protobuf messages should be
non-null (with type specific default values) when they are serialized.
Removing the null check that is being flagged from ErrorProne.
## Testing
We have some issues with testing this inside Showcase. Protobuf
serializes messages with default values and does not allow for null
(will throw a NPE). Since showcase is configured to receive and send
Protobuf Messages, we cannot configure the response to be return null
values (at best we can return something like an empty string or an empty
map).
Instead, we have opted to create a sample Spring server to return a JSON
payload with certain fields set to null. Create a GAPIC client and
manually set the endpoint to point to the local server (i.e.
`localhost:8080`). The Spring application will have a endpoint that
matches the path of the RPC (i.e.
`@GetMapping("/compute/v1/projects/{project}/zones/{zone}/instances")`)
and returns the JSON. This proves that a null fields sent from the
server is serialized properly into a Protobuf message and the message's
fields are non-null.
```
@RestController
public class ComputeController {
@GetMapping("/compute/v1/projects/{project}/zones/{zone}/instances")
String listInstances(@PathVariable String project, String zone) {
return "{\"items\": null, \"id\": 5}";
}
}
```
In the example above, the `items` field is a list and the printing it
out in the client library will result in `[]`.
Additionally, we have added some small, local tests against two repeated
Protobuf messages:
1. BackendBucketList's items is a non-null List
```
JsonFormat.Parser parser = JsonFormat.parser().ignoringUnknownFields()
.usingTypeRegistry(TypeRegistry.newBuilder().add(BackendBucketList.getDescriptor()).build());
BackendBucketList.Builder builder = BackendBucketList.newBuilder();
parser.merge("{\"items\": null}", builder);
BackendBucketList list = builder.build();
System.out.println(list.getItemsList());
```
2. PredictResponse's metadata is a non-null Map
```
JsonFormat.Parser parser = JsonFormat.parser().ignoringUnknownFields()
.usingTypeRegistry(TypeRegistry.newBuilder().add(PredictResponse.getDescriptor()).build());;
PredictResponse.Builder builder = PredictResponse.newBuilder();
parser.merge("{\"recommendation_token\": \"343\"}", builder);
System.out.println(builder.build().getMetadataMap());
```
---------
Co-authored-by: cloud-java-bot <[email protected]>
Co-authored-by: cloud-java-bot <[email protected]>1 parent 594a9e1 commit bc6c7e1
File tree
25 files changed
+77
-220
lines changed- gapic-generator-java/src
- main/java/com/google/api/generator/gapic/composer/common
- test/java/com/google/api/generator/gapic/composer
- grpcrest/goldens
- grpc/goldens
- showcase/gapic-showcase/src/main
- java/com/google/showcase/v1beta1/stub
- resources/META-INF/native-image/com.google.showcase.v1beta1
- test/integration/goldens
- apigeeconnect/src/com/google/cloud/apigeeconnect/v1/stub
- asset/src/com/google/cloud/asset/v1/stub
- compute/src/com/google/cloud/compute/v1small/stub
- kms/src/com/google/cloud/kms/v1/stub
- library/src/com/google/cloud/example/library/v1/stub
- logging/src/com/google/cloud/logging/v2/stub
- pubsub/src/com/google/cloud/pubsub/v1/stub
- redis/src/com/google/cloud/redis/v1beta1/stub
- storage/src/com/google/storage/v2/stub
25 files changed
+77
-220
lines changedLines changed: 2 additions & 47 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
69 | 68 | | |
70 | 69 | | |
71 | 70 | | |
72 | 71 | | |
73 | 72 | | |
74 | | - | |
75 | 73 | | |
76 | 74 | | |
77 | 75 | | |
| |||
788 | 786 | | |
789 | 787 | | |
790 | 788 | | |
791 | | - | |
792 | | - | |
793 | | - | |
794 | 789 | | |
795 | 790 | | |
796 | | - | |
797 | | - | |
798 | | - | |
799 | | - | |
800 | | - | |
801 | | - | |
802 | | - | |
803 | | - | |
804 | | - | |
805 | | - | |
806 | | - | |
807 | | - | |
808 | | - | |
809 | | - | |
810 | | - | |
811 | | - | |
| 791 | + | |
812 | 792 | | |
813 | 793 | | |
814 | 794 | | |
| |||
820 | 800 | | |
821 | 801 | | |
822 | 802 | | |
823 | | - | |
| 803 | + | |
824 | 804 | | |
825 | 805 | | |
826 | 806 | | |
827 | 807 | | |
828 | 808 | | |
829 | 809 | | |
830 | | - | |
831 | | - | |
832 | | - | |
833 | | - | |
834 | | - | |
835 | | - | |
836 | | - | |
837 | | - | |
838 | | - | |
839 | 810 | | |
840 | | - | |
841 | | - | |
842 | | - | |
843 | | - | |
844 | | - | |
845 | | - | |
846 | | - | |
847 | | - | |
848 | | - | |
849 | | - | |
850 | | - | |
851 | | - | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | 811 | | |
857 | 812 | | |
858 | 813 | | |
| |||
Lines changed: 2 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
184 | | - | |
185 | | - | |
| 183 | + | |
186 | 184 | | |
187 | 185 | | |
188 | 186 | | |
| |||
216 | 214 | | |
217 | 215 | | |
218 | 216 | | |
219 | | - | |
220 | | - | |
221 | | - | |
| 217 | + | |
222 | 218 | | |
223 | 219 | | |
224 | 220 | | |
| |||
Lines changed: 3 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
172 | | - | |
173 | | - | |
| 171 | + | |
174 | 172 | | |
175 | 173 | | |
176 | 174 | | |
| |||
217 | 215 | | |
218 | 216 | | |
219 | 217 | | |
220 | | - | |
221 | | - | |
222 | | - | |
| 218 | + | |
223 | 219 | | |
224 | 220 | | |
225 | 221 | | |
| |||
253 | 249 | | |
254 | 250 | | |
255 | 251 | | |
256 | | - | |
257 | | - | |
258 | | - | |
| 252 | + | |
259 | 253 | | |
260 | 254 | | |
261 | 255 | | |
| |||
Lines changed: 3 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
170 | | - | |
171 | | - | |
| 169 | + | |
172 | 170 | | |
173 | 171 | | |
174 | 172 | | |
| |||
208 | 206 | | |
209 | 207 | | |
210 | 208 | | |
211 | | - | |
212 | | - | |
213 | | - | |
| 209 | + | |
214 | 210 | | |
215 | 211 | | |
216 | 212 | | |
| |||
247 | 243 | | |
248 | 244 | | |
249 | 245 | | |
250 | | - | |
251 | | - | |
252 | | - | |
| 246 | + | |
253 | 247 | | |
254 | 248 | | |
255 | 249 | | |
| |||
Lines changed: 2 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
189 | | - | |
190 | | - | |
191 | | - | |
| 189 | + | |
192 | 190 | | |
193 | 191 | | |
194 | 192 | | |
| |||
222 | 220 | | |
223 | 221 | | |
224 | 222 | | |
225 | | - | |
226 | | - | |
227 | | - | |
| 223 | + | |
228 | 224 | | |
229 | 225 | | |
230 | 226 | | |
| |||
showcase/gapic-showcase/src/main/java/com/google/showcase/v1beta1/stub/ComplianceStubSettings.java
Lines changed: 1 addition & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | | - | |
175 | | - | |
176 | | - | |
| 174 | + | |
177 | 175 | | |
178 | 176 | | |
179 | 177 | | |
| |||
Lines changed: 3 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
86 | 85 | | |
87 | 86 | | |
88 | 87 | | |
| |||
225 | 224 | | |
226 | 225 | | |
227 | 226 | | |
228 | | - | |
229 | | - | |
230 | | - | |
| 227 | + | |
231 | 228 | | |
232 | 229 | | |
233 | 230 | | |
| |||
268 | 265 | | |
269 | 266 | | |
270 | 267 | | |
271 | | - | |
272 | | - | |
273 | | - | |
| 268 | + | |
274 | 269 | | |
275 | 270 | | |
276 | 271 | | |
| |||
304 | 299 | | |
305 | 300 | | |
306 | 301 | | |
307 | | - | |
308 | | - | |
309 | | - | |
| 302 | + | |
310 | 303 | | |
311 | 304 | | |
312 | 305 | | |
| |||
Lines changed: 2 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
175 | | - | |
176 | | - | |
177 | | - | |
| 175 | + | |
178 | 176 | | |
179 | 177 | | |
180 | 178 | | |
| |||
208 | 206 | | |
209 | 207 | | |
210 | 208 | | |
211 | | - | |
212 | | - | |
213 | | - | |
| 209 | + | |
214 | 210 | | |
215 | 211 | | |
216 | 212 | | |
| |||
Lines changed: 3 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
236 | | - | |
237 | | - | |
238 | | - | |
| 236 | + | |
239 | 237 | | |
240 | 238 | | |
241 | 239 | | |
| |||
269 | 267 | | |
270 | 268 | | |
271 | 269 | | |
272 | | - | |
273 | | - | |
274 | | - | |
| 270 | + | |
275 | 271 | | |
276 | 272 | | |
277 | 273 | | |
| |||
305 | 301 | | |
306 | 302 | | |
307 | 303 | | |
308 | | - | |
309 | | - | |
310 | | - | |
| 304 | + | |
311 | 305 | | |
312 | 306 | | |
313 | 307 | | |
| |||
Lines changed: 1 addition & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
185 | | - | |
186 | | - | |
187 | | - | |
| 185 | + | |
188 | 186 | | |
189 | 187 | | |
190 | 188 | | |
| |||
0 commit comments