You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
Copy file name to clipboardExpand all lines: gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java
Copy file name to clipboardExpand all lines: gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/EchoStubSettings.golden
+2-6
Original file line number
Diff line number
Diff line change
@@ -180,9 +180,7 @@ public class EchoStubSettings extends StubSettings<EchoStubSettings> {
180
180
181
181
@Override
182
182
public Iterable<EchoResponse> extractResources(PagedExpandResponse payload) {
183
-
return payload.getResponsesList() == null
184
-
? ImmutableList.<EchoResponse>of()
185
-
: payload.getResponsesList();
183
+
return payload.getResponsesList();
186
184
}
187
185
};
188
186
@@ -216,9 +214,7 @@ public class EchoStubSettings extends StubSettings<EchoStubSettings> {
216
214
217
215
@Override
218
216
public Iterable<EchoResponse> extractResources(PagedExpandResponse payload) {
Copy file name to clipboardExpand all lines: gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/LoggingServiceV2StubSettings.golden
+3-9
Original file line number
Diff line number
Diff line change
@@ -168,9 +168,7 @@ public class LoggingServiceV2StubSettings extends StubSettings<LoggingServiceV2S
168
168
169
169
@Override
170
170
public Iterable<LogEntry> extractResources(ListLogEntriesResponse payload) {
171
-
return payload.getEntriesList() == null
172
-
? ImmutableList.<LogEntry>of()
173
-
: payload.getEntriesList();
171
+
return payload.getEntriesList();
174
172
}
175
173
};
176
174
@@ -217,9 +215,7 @@ public class LoggingServiceV2StubSettings extends StubSettings<LoggingServiceV2S
217
215
@Override
218
216
public Iterable<MonitoredResourceDescriptor> extractResources(
Copy file name to clipboardExpand all lines: gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/PublisherStubSettings.golden
+3-9
Original file line number
Diff line number
Diff line change
@@ -166,9 +166,7 @@ public class PublisherStubSettings extends StubSettings<PublisherStubSettings> {
166
166
167
167
@Override
168
168
public Iterable<Topic> extractResources(ListTopicsResponse payload) {
169
-
return payload.getTopicsList() == null
170
-
? ImmutableList.<Topic>of()
171
-
: payload.getTopicsList();
169
+
return payload.getTopicsList();
172
170
}
173
171
};
174
172
@@ -208,9 +206,7 @@ public class PublisherStubSettings extends StubSettings<PublisherStubSettings> {
208
206
209
207
@Override
210
208
public Iterable<String> extractResources(ListTopicSubscriptionsResponse payload) {
211
-
return payload.getSubscriptionsList() == null
212
-
? ImmutableList.<String>of()
213
-
: payload.getSubscriptionsList();
209
+
return payload.getSubscriptionsList();
214
210
}
215
211
};
216
212
@@ -247,9 +243,7 @@ public class PublisherStubSettings extends StubSettings<PublisherStubSettings> {
247
243
248
244
@Override
249
245
public Iterable<String> extractResources(ListTopicSnapshotsResponse payload) {
Copy file name to clipboardExpand all lines: gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpcrest/goldens/EchoStubSettings.golden
+2-6
Original file line number
Diff line number
Diff line change
@@ -186,9 +186,7 @@ public class EchoStubSettings extends StubSettings<EchoStubSettings> {
186
186
187
187
@Override
188
188
public Iterable<EchoResponse> extractResources(PagedExpandResponse payload) {
189
-
return payload.getResponsesList() == null
190
-
? ImmutableList.<EchoResponse>of()
191
-
: payload.getResponsesList();
189
+
return payload.getResponsesList();
192
190
}
193
191
};
194
192
@@ -222,9 +220,7 @@ public class EchoStubSettings extends StubSettings<EchoStubSettings> {
222
220
223
221
@Override
224
222
public Iterable<EchoResponse> extractResources(PagedExpandResponse payload) {
Copy file name to clipboardExpand all lines: showcase/gapic-showcase/src/main/resources/META-INF/native-image/com.google.showcase.v1beta1/reflect-config.json
Copy file name to clipboardExpand all lines: test/integration/goldens/apigeeconnect/src/com/google/cloud/apigeeconnect/v1/stub/ConnectionServiceStubSettings.java
+1-3
Original file line number
Diff line number
Diff line change
@@ -150,9 +150,7 @@ public String extractNextToken(ListConnectionsResponse payload) {
0 commit comments