From 78d51fe56bd5fe308d3d9eae33a608808553ac81 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Fri, 18 Sep 2020 23:32:28 -0700 Subject: [PATCH] [ggj][codegen] fix: init remaining resname tokenVars to null (#329) * feat: add protobuf comment parser util * fix: add basic proto build rules * feat: add header comments to ServiceClient * fix: build protoc at test time * fix!: wrap protobuf location and process comments * feat: add comment parsing to methods and fields * fix: test * feat: add protobuf comments to ServiceClient * fix: solidify codegen method order with TypeNode/MethodArg and Comparable * fix: clean up tests * fix: ServiceClient member variables and method calls * fix: ServiceStubSettings builder type * fix: ServiceSettings Builder construction * fix: ServiceStub callable types * feat: java_gapic_library rule impl * fix: remove debugging comments * feat: add gradle assembly Bazel rules * feat: add java_gapic_test Bazel rule * fix: use Java packages for resname codegen * fix: build resnames separately and extract into proto/ dir * fix: remove debug printf * feat: add ServiceClient.MethodPagedResponse inner class * feat: add ServiceClient.MethodPage inner class * feat: add ServiceClient.MethodFixedSizeCollection innser class * fix: clean up resname codegen, lower_snake varnames * fix: init remaining resname tokenVars to null --- .../composer/ResourceNameHelperClassComposer.java | 12 ++++++++++++ .../ResourceNameHelperClassComposerTest.java | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposer.java index a9b3275bb9..652c8488d6 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposer.java @@ -312,6 +312,18 @@ private static List createConstructorMethods( .setValueExpr(checkNotNullExpr) .build()); } + // Initialize the rest to null. + ValueExpr nullExpr = ValueExpr.withValue(NullObjectValue.create()); + for (String token : getTokenSet(tokenHierarchies)) { + if (tokens.contains(token)) { + continue; + } + bodyExprs.add( + AssignmentExpr.builder() + .setVariableExpr(patternTokenVarExprs.get(token)) + .setValueExpr(nullExpr) + .build()); + } if (hasVariants) { AssignmentExpr pathTemplateAssignExpr = diff --git a/src/test/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposerTest.java index cfcfa1bd63..75911fcffb 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ResourceNameHelperClassComposerTest.java @@ -245,6 +245,8 @@ public void generateResourceNameClass_testingSessionOnePattern() { + " private FoobarName(Builder builder) {\n" + " project = Preconditions.checkNotNull(builder.getProject());\n" + " foobar = Preconditions.checkNotNull(builder.getFoobar());\n" + + " variant = null;\n" + + " barFoo = null;\n" + " pathTemplate = PROJECT_FOOBAR;\n" + " }\n" + "\n" @@ -252,17 +254,23 @@ public void generateResourceNameClass_testingSessionOnePattern() { + " project = Preconditions.checkNotNull(builder.getProject());\n" + " variant = Preconditions.checkNotNull(builder.getVariant());\n" + " foobar = Preconditions.checkNotNull(builder.getFoobar());\n" + + " barFoo = null;\n" + " pathTemplate = PROJECT_VARIANT_FOOBAR;\n" + " }\n" + "\n" + " private FoobarName(FoobarBuilder builder) {\n" + " foobar = Preconditions.checkNotNull(builder.getFoobar());\n" + + " project = null;\n" + + " variant = null;\n" + + " barFoo = null;\n" + " pathTemplate = FOOBAR;\n" + " }\n" + "\n" + " private FoobarName(BarFooFoobarBuilder builder) {\n" + " barFoo = Preconditions.checkNotNull(builder.getBarFoo());\n" + " foobar = Preconditions.checkNotNull(builder.getFoobar());\n" + + " project = null;\n" + + " variant = null;\n" + " pathTemplate = BAR_FOO_FOOBAR;\n" + " }\n" + "\n"