Skip to content

Commit 6917f74

Browse files
committed
feat(generator): expose REST composer helpers and add path prefix support
Expose package-private helpers on HttpJsonServiceStubClassComposer and add an overloaded getRequestFormatterExpr that supports path prefixes for reuse by resumable upload REST stubs.
1 parent 39fdadf commit 6917f74

2 files changed

Lines changed: 62 additions & 15 deletions

File tree

‎sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposer.java‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@
7979
import java.util.function.Predicate;
8080
import java.util.stream.Collectors;
8181
import org.jspecify.annotations.NullMarked;
82+
import org.jspecify.annotations.Nullable;
8283

8384
@NullMarked
8485
public class HttpJsonServiceStubClassComposer extends AbstractTransportServiceStubClassComposer {
8586
private static final HttpJsonServiceStubClassComposer INSTANCE =
8687
new HttpJsonServiceStubClassComposer();
8788

88-
private static final TypeStore FIXED_REST_TYPESTORE = createStaticTypes();
89-
private static final VariableExpr TYPE_REGISTRY_VAR_EXPR =
89+
static final TypeStore FIXED_REST_TYPESTORE = createStaticTypes();
90+
static final VariableExpr TYPE_REGISTRY_VAR_EXPR =
9091
VariableExpr.builder()
9192
.setVariable(
9293
Variable.builder()
@@ -266,7 +267,7 @@ protected List<MethodDefinition> createGetMethodDescriptorsMethod(
266267
.build());
267268
}
268269

269-
private BiFunction<String, List<Expr>, Function<MethodInvocationExpr, MethodInvocationExpr>>
270+
static BiFunction<String, List<Expr>, Function<MethodInvocationExpr, MethodInvocationExpr>>
270271
getMethodMaker() {
271272
return (mName, argExpr) ->
272273
(m) ->
@@ -277,7 +278,23 @@ protected List<MethodDefinition> createGetMethodDescriptorsMethod(
277278
.build();
278279
}
279280

280-
private List<Expr> getRequestFormatterExpr(Method protoMethod, boolean restNumericEnumsEnabled) {
281+
private static String prependPathPrefix(String path, @Nullable String pathPrefix) {
282+
if (pathPrefix == null) {
283+
return path;
284+
}
285+
String normalizedPrefix = pathPrefix.startsWith("/") ? pathPrefix : "/" + pathPrefix;
286+
if (normalizedPrefix.endsWith("/")) {
287+
normalizedPrefix = normalizedPrefix.substring(0, normalizedPrefix.length() - 1);
288+
}
289+
return normalizedPrefix + (path.startsWith("/") ? path : "/" + path);
290+
}
291+
292+
static List<Expr> getRequestFormatterExpr(Method protoMethod, boolean restNumericEnumsEnabled) {
293+
return getRequestFormatterExpr(protoMethod, restNumericEnumsEnabled, null);
294+
}
295+
296+
static List<Expr> getRequestFormatterExpr(
297+
Method protoMethod, boolean restNumericEnumsEnabled, @Nullable String pathPrefix) {
281298
BiFunction<String, List<Expr>, Function<MethodInvocationExpr, MethodInvocationExpr>>
282299
methodMaker = getMethodMaker();
283300

@@ -296,14 +313,15 @@ private List<Expr> getRequestFormatterExpr(Method protoMethod, boolean restNumer
296313
.setGenerics(TypeNode.STRING.reference(), TypeNode.STRING.reference())
297314
.build());
298315

316+
String pathPattern =
317+
prependPathPrefix(protoMethod.httpBindings().lowerCamelPattern(), pathPrefix);
318+
299319
expr =
300320
methodMaker
301321
.apply(
302322
"setPath",
303323
Arrays.asList(
304-
ValueExpr.withValue(
305-
StringObjectValue.withValue(
306-
protoMethod.httpBindings().lowerCamelPattern())),
324+
ValueExpr.withValue(StringObjectValue.withValue(pathPattern)),
307325
createFieldsExtractorClassInstance(
308326
protoMethod,
309327
extractorVarType,
@@ -318,7 +336,10 @@ private List<Expr> getRequestFormatterExpr(Method protoMethod, boolean restNumer
318336
.apply(
319337
"setAdditionalPaths",
320338
protoMethod.httpBindings().lowerCamelAdditionalPatterns().stream()
321-
.map(a -> ValueExpr.withValue(StringObjectValue.withValue(a)))
339+
.map(
340+
a ->
341+
ValueExpr.withValue(
342+
StringObjectValue.withValue(prependPathPrefix(a, pathPrefix))))
322343
.collect(Collectors.toList()))
323344
.apply(expr);
324345
}
@@ -371,7 +392,7 @@ private List<Expr> getRequestFormatterExpr(Method protoMethod, boolean restNumer
371392
return Collections.singletonList(expr);
372393
}
373394

374-
private List<Expr> setResponseParserExpr(Method protoMethod) {
395+
static List<Expr> setResponseParserExpr(Method protoMethod) {
375396
BiFunction<String, List<Expr>, Function<MethodInvocationExpr, MethodInvocationExpr>>
376397
methodMaker = getMethodMaker();
377398

@@ -726,7 +747,7 @@ private List<Expr> setPollingRequestFactoryExpr(
726747
.build());
727748
}
728749

729-
private Expr createBodyFieldsExtractorClassInstance(
750+
private static Expr createBodyFieldsExtractorClassInstance(
730751
Method method,
731752
TypeNode extractorReturnType,
732753
Set<HttpBinding> httpBindingFieldNames,
@@ -839,7 +860,7 @@ private Expr createBodyFieldsExtractorClassInstance(
839860
.build();
840861
}
841862

842-
private Expr createFieldsExtractorClassInstance(
863+
private static Expr createFieldsExtractorClassInstance(
843864
Method method,
844865
TypeNode extractorReturnType,
845866
Set<HttpBinding> httpBindingFieldNames,
@@ -993,7 +1014,7 @@ private Expr createFieldsExtractorClassInstance(
9931014
}
9941015

9951016
@VisibleForTesting
996-
String getBindingFieldMethodName(
1017+
static String getBindingFieldMethodName(
9971018
HttpBinding httpBindingField, int descendantFieldsLengths, int index, String currFieldName) {
9981019
if (index == descendantFieldsLengths - 1) {
9991020
if (httpBindingField.isRepeated()) {
@@ -1006,13 +1027,13 @@ String getBindingFieldMethodName(
10061027
return String.format("get%s", currFieldName);
10071028
}
10081029

1009-
private List<Expr> getHttpMethodTypeExpr(Method protoMethod) {
1030+
static List<Expr> getHttpMethodTypeExpr(Method protoMethod) {
10101031
return Collections.singletonList(
10111032
ValueExpr.withValue(
10121033
StringObjectValue.withValue(protoMethod.httpBindings().httpVerb().toString())));
10131034
}
10141035

1015-
private List<Expr> getMethodTypeExpr(Method protoMethod) {
1036+
static List<Expr> getMethodTypeExpr(Method protoMethod) {
10161037
MethodType methodType;
10171038
switch (protoMethod.stream()) {
10181039
case NONE:
@@ -1042,6 +1063,10 @@ private List<Expr> getMethodTypeExpr(Method protoMethod) {
10421063
return Collections.singletonList(expr);
10431064
}
10441065

1066+
static String getProtoRpcName(Service protoService, Method protoMethod) {
1067+
return INSTANCE.getProtoRpcFullMethodName(protoService, protoMethod);
1068+
}
1069+
10451070
@Override
10461071
protected List<Expr> createOperationsStubInitExpr(
10471072
GapicContext context,
@@ -1365,7 +1390,8 @@ private List<MethodDefinition> createInvalidClassMethods(Service service) {
13651390
.setType(FIXED_TYPESTORE.get("UnsupportedOperationException"))
13661391
.setMessageExpr(
13671392
String.format(
1368-
"Not implemented: %s(). %s transport is not implemented for this method yet.",
1393+
"Not implemented: %s(). %s transport is not implemented for"
1394+
+ " this method yet.",
13691395
callableName, getTransportContext().transport()))
13701396
.build())))
13711397
.build());

‎sdk-platform-java/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposerTest.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import com.google.api.CustomHttpPattern;
2020
import com.google.api.Http;
2121
import com.google.api.HttpRule;
22+
import com.google.api.generator.engine.ast.Expr;
2223
import com.google.api.generator.engine.ast.TypeNode;
2324
import com.google.api.generator.engine.writer.JavaWriterVisitor;
2425
import com.google.api.generator.gapic.model.Field;
2526
import com.google.api.generator.gapic.model.GapicClass;
2627
import com.google.api.generator.gapic.model.GapicContext;
2728
import com.google.api.generator.gapic.model.HttpBindings.HttpBinding;
29+
import com.google.api.generator.gapic.model.Method;
2830
import com.google.api.generator.gapic.model.Service;
2931
import com.google.api.generator.test.framework.Assert;
3032
import com.google.api.generator.test.framework.GoldenFileWriter;
@@ -223,4 +225,23 @@ void generateHttpJsonServiceStubClass_clientResourceNameExtractor() {
223225
Assert.assertGoldenClass(this.getClass(), clazz, "HttpJsonResourceNameExtractorStub.golden");
224226
Assert.assertEmptySamples(clazz.samples());
225227
}
228+
229+
@Test
230+
void getRequestFormatterExpr_withPathPrefix_prependsPrefix() {
231+
GapicContext context = RestTestProtoLoader.instance().parseShowcaseResumableUpload();
232+
Service service = context.services().get(0);
233+
Method uploadMethod =
234+
service.methods().stream().filter(Method::isResumableUpload).findFirst().get();
235+
236+
List<Expr> exprs =
237+
HttpJsonServiceStubClassComposer.getRequestFormatterExpr(
238+
uploadMethod, context.restNumericEnumsEnabled(), "/resumable/upload");
239+
240+
JavaWriterVisitor visitor = new JavaWriterVisitor();
241+
for (Expr expr : exprs) {
242+
expr.accept(visitor);
243+
}
244+
String code = visitor.write();
245+
Truth.assertThat(code).contains("/resumable/upload/v1beta1/files:upload");
246+
}
226247
}

0 commit comments

Comments
 (0)