7979import java .util .function .Predicate ;
8080import java .util .stream .Collectors ;
8181import org .jspecify .annotations .NullMarked ;
82+ import org .jspecify .annotations .Nullable ;
8283
8384@ NullMarked
8485public 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 ());
0 commit comments