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"