From 74dbafb1cd8808da8664b58400ada27efe92f0fa Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Wed, 28 Oct 2020 16:39:18 -0700 Subject: [PATCH] fix: use generic types in field instantiation in ServiceClientTest --- .../gapic/composer/DefaultValueComposer.java | 16 +++++-- .../composer/DefaultValueComposerTest.java | 4 +- .../goldens/SubscriberClientTest.golden | 48 ++++++++++--------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/DefaultValueComposer.java b/src/main/java/com/google/api/generator/gapic/composer/DefaultValueComposer.java index 1268cb7750..db69075920 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/DefaultValueComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/DefaultValueComposer.java @@ -77,10 +77,18 @@ static Expr createDefaultValue( } static Expr createDefaultValue(Field f) { + return createDefaultValue(f, false); + } + + static Expr createDefaultValue(Field f, boolean useExplicitInitTypeInGenerics) { if (f.isRepeated()) { - TypeNode newType = - TypeNode.withReference( - ConcreteReference.withClazz(f.isMap() ? HashMap.class : ArrayList.class)); + ConcreteReference.Builder refBuilder = + ConcreteReference.builder().setClazz(f.isMap() ? HashMap.class : ArrayList.class); + if (useExplicitInitTypeInGenerics) { + refBuilder = refBuilder.setGenerics(f.type().reference().generics().get(0)); + } + + TypeNode newType = TypeNode.withReference(refBuilder.build()); return NewObjectExpr.builder().setType(newType).setIsGeneric(true).build(); } @@ -238,7 +246,7 @@ static Expr createSimpleMessageBuilderExpr( .setReturnType(TypeNode.STRING) .build(); } else { - defaultExpr = createDefaultValue(field); + defaultExpr = createDefaultValue(field, true); } builderExpr = MethodInvocationExpr.builder() diff --git a/src/test/java/com/google/api/generator/gapic/composer/DefaultValueComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/DefaultValueComposerTest.java index 43f2507dad..0c397692b1 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/DefaultValueComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/DefaultValueComposerTest.java @@ -273,8 +273,8 @@ public void createSimpleMessage_containsRepeatedField() { message, typeStringsToResourceNames, messageTypes); expr.accept(writerVisitor); assertEquals( - "PagedExpandResponse.newBuilder().addAllResponses(new" - + " ArrayList<>()).setNextPageToken(\"next_page_token-1530815211\").build()", + "PagedExpandResponse.newBuilder().addAllResponses(new ArrayList())" + + ".setNextPageToken(\"next_page_token-1530815211\").build()", writerVisitor.write()); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/SubscriberClientTest.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/SubscriberClientTest.golden index 8a03a3ca8e..150a8926e5 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/SubscriberClientTest.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/SubscriberClientTest.golden @@ -81,7 +81,7 @@ public class SubscriberClientTest { .setPushConfig(PushConfig.newBuilder().build()) .setAckDeadlineSeconds(2135351438) .setRetainAckedMessages(true) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .setEnableMessageOrdering(true) .setExpirationPolicy(ExpirationPolicy.newBuilder().build()) .setFilter("filter-1274492040") @@ -140,7 +140,7 @@ public class SubscriberClientTest { .setPushConfig(PushConfig.newBuilder().build()) .setAckDeadlineSeconds(2135351438) .setRetainAckedMessages(true) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .setEnableMessageOrdering(true) .setExpirationPolicy(ExpirationPolicy.newBuilder().build()) .setFilter("filter-1274492040") @@ -199,7 +199,7 @@ public class SubscriberClientTest { .setPushConfig(PushConfig.newBuilder().build()) .setAckDeadlineSeconds(2135351438) .setRetainAckedMessages(true) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .setEnableMessageOrdering(true) .setExpirationPolicy(ExpirationPolicy.newBuilder().build()) .setFilter("filter-1274492040") @@ -258,7 +258,7 @@ public class SubscriberClientTest { .setPushConfig(PushConfig.newBuilder().build()) .setAckDeadlineSeconds(2135351438) .setRetainAckedMessages(true) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .setEnableMessageOrdering(true) .setExpirationPolicy(ExpirationPolicy.newBuilder().build()) .setFilter("filter-1274492040") @@ -317,7 +317,7 @@ public class SubscriberClientTest { .setPushConfig(PushConfig.newBuilder().build()) .setAckDeadlineSeconds(2135351438) .setRetainAckedMessages(true) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .setEnableMessageOrdering(true) .setExpirationPolicy(ExpirationPolicy.newBuilder().build()) .setFilter("filter-1274492040") @@ -366,7 +366,7 @@ public class SubscriberClientTest { .setPushConfig(PushConfig.newBuilder().build()) .setAckDeadlineSeconds(2135351438) .setRetainAckedMessages(true) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .setEnableMessageOrdering(true) .setExpirationPolicy(ExpirationPolicy.newBuilder().build()) .setFilter("filter-1274492040") @@ -415,7 +415,7 @@ public class SubscriberClientTest { .setPushConfig(PushConfig.newBuilder().build()) .setAckDeadlineSeconds(2135351438) .setRetainAckedMessages(true) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .setEnableMessageOrdering(true) .setExpirationPolicy(ExpirationPolicy.newBuilder().build()) .setFilter("filter-1274492040") @@ -781,7 +781,7 @@ public class SubscriberClientTest { @Test public void pullTest() throws Exception { PullResponse expectedResponse = - PullResponse.newBuilder().addAllReceivedMessages(new ArrayList<>()).build(); + PullResponse.newBuilder().addAllReceivedMessages(new ArrayList()).build(); mockSubscriber.addResponse(expectedResponse); SubscriptionName subscription = SubscriptionName.of("[PROJECT]", "[SUBSCRIPTION]"); @@ -823,7 +823,7 @@ public class SubscriberClientTest { @Test public void pullTest2() throws Exception { PullResponse expectedResponse = - PullResponse.newBuilder().addAllReceivedMessages(new ArrayList<>()).build(); + PullResponse.newBuilder().addAllReceivedMessages(new ArrayList()).build(); mockSubscriber.addResponse(expectedResponse); String subscription = "subscription341203229"; @@ -865,14 +865,16 @@ public class SubscriberClientTest { @Test public void streamingPullTest() throws Exception { StreamingPullResponse expectedResponse = - StreamingPullResponse.newBuilder().addAllReceivedMessages(new ArrayList<>()).build(); + StreamingPullResponse.newBuilder() + .addAllReceivedMessages(new ArrayList()) + .build(); mockSubscriber.addResponse(expectedResponse); StreamingPullRequest request = StreamingPullRequest.newBuilder() .setSubscription(SubscriptionName.of("[PROJECT]", "[SUBSCRIPTION]").toString()) - .addAllAckIds(new ArrayList<>()) - .addAllModifyDeadlineSeconds(new ArrayList<>()) - .addAllModifyDeadlineAckIds(new ArrayList<>()) + .addAllAckIds(new ArrayList()) + .addAllModifyDeadlineSeconds(new ArrayList()) + .addAllModifyDeadlineAckIds(new ArrayList()) .setStreamAckDeadlineSeconds(1875467245) .setClientId("client_id-1904089585") .setMaxOutstandingMessages(-1315266996) @@ -901,9 +903,9 @@ public class SubscriberClientTest { StreamingPullRequest request = StreamingPullRequest.newBuilder() .setSubscription(SubscriptionName.of("[PROJECT]", "[SUBSCRIPTION]").toString()) - .addAllAckIds(new ArrayList<>()) - .addAllModifyDeadlineSeconds(new ArrayList<>()) - .addAllModifyDeadlineAckIds(new ArrayList<>()) + .addAllAckIds(new ArrayList()) + .addAllModifyDeadlineSeconds(new ArrayList()) + .addAllModifyDeadlineAckIds(new ArrayList()) .setStreamAckDeadlineSeconds(1875467245) .setClientId("client_id-1904089585") .setMaxOutstandingMessages(-1315266996) @@ -1011,7 +1013,7 @@ public class SubscriberClientTest { Snapshot.newBuilder() .setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString()) .setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString()) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .build(); mockSubscriber.addResponse(expectedResponse); @@ -1051,7 +1053,7 @@ public class SubscriberClientTest { Snapshot.newBuilder() .setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString()) .setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString()) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .build(); mockSubscriber.addResponse(expectedResponse); @@ -1179,7 +1181,7 @@ public class SubscriberClientTest { Snapshot.newBuilder() .setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString()) .setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString()) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .build(); mockSubscriber.addResponse(expectedResponse); @@ -1222,7 +1224,7 @@ public class SubscriberClientTest { Snapshot.newBuilder() .setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString()) .setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString()) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .build(); mockSubscriber.addResponse(expectedResponse); @@ -1265,7 +1267,7 @@ public class SubscriberClientTest { Snapshot.newBuilder() .setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString()) .setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString()) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .build(); mockSubscriber.addResponse(expectedResponse); @@ -1308,7 +1310,7 @@ public class SubscriberClientTest { Snapshot.newBuilder() .setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString()) .setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString()) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .build(); mockSubscriber.addResponse(expectedResponse); @@ -1351,7 +1353,7 @@ public class SubscriberClientTest { Snapshot.newBuilder() .setName(SnapshotName.of("[PROJECT]", "[SNAPSHOT]").toString()) .setTopic(TopicName.ofProjectTopicName("[PROJECT]", "[TOPIC]").toString()) - .putAllLabels(new HashMap<>()) + .putAllLabels(new HashMap()) .build(); mockSubscriber.addResponse(expectedResponse);