17
17
import com .google .api .AnnotationsProto ;
18
18
import com .google .api .HttpRule ;
19
19
import com .google .api .HttpRule .PatternCase ;
20
+ import com .google .api .RoutingParameter ;
21
+ import com .google .api .RoutingProto ;
22
+ import com .google .api .RoutingRule ;
20
23
import com .google .api .generator .gapic .model .Field ;
21
24
import com .google .api .generator .gapic .model .HttpBindings ;
22
25
import com .google .api .generator .gapic .model .HttpBindings .HttpBinding ;
30
33
import com .google .protobuf .DescriptorProtos .MethodOptions ;
31
34
import com .google .protobuf .Descriptors .MethodDescriptor ;
32
35
import java .util .Collections ;
36
+ import java .util .HashMap ;
33
37
import java .util .Map ;
34
38
import java .util .Optional ;
35
39
import java .util .Set ;
@@ -47,20 +51,28 @@ public static HttpBindings parse(
47
51
48
52
HttpRule httpRule = methodOptions .getExtension (AnnotationsProto .http );
49
53
54
+ RoutingRule routingRule = null ;
55
+ if (methodOptions .hasExtension (RoutingProto .routing )) {
56
+ routingRule = methodOptions .getExtension (RoutingProto .routing );
57
+ }
58
+
50
59
// Body validation.
51
60
if (!Strings .isNullOrEmpty (httpRule .getBody ()) && !httpRule .getBody ().equals (ASTERISK )) {
52
61
checkHttpFieldIsValid (httpRule .getBody (), inputMessage , true );
53
62
}
54
63
55
- return parseHttpRuleHelper (httpRule , Optional .of (inputMessage ), messageTypes );
64
+ return parseHttpRuleHelper (httpRule , routingRule , Optional .of (inputMessage ), messageTypes );
56
65
}
57
66
58
67
public static HttpBindings parseHttpRule (HttpRule httpRule ) {
59
- return parseHttpRuleHelper (httpRule , Optional .empty (), Collections .emptyMap ());
68
+ return parseHttpRuleHelper (httpRule , null , Optional .empty (), Collections .emptyMap ());
60
69
}
61
70
62
71
private static HttpBindings parseHttpRuleHelper (
63
- HttpRule httpRule , Optional <Message > inputMessageOpt , Map <String , Message > messageTypes ) {
72
+ HttpRule httpRule ,
73
+ RoutingRule routingRule ,
74
+ Optional <Message > inputMessageOpt ,
75
+ Map <String , Message > messageTypes ) {
64
76
// Get pattern.
65
77
String pattern = getHttpVerbPattern (httpRule );
66
78
ImmutableSet .Builder <String > bindingsBuilder = getPatternBindings (pattern );
@@ -94,31 +106,50 @@ private static HttpBindings parseHttpRuleHelper(
94
106
Sets .difference (inputMessageOpt .get ().fieldMap ().keySet (), bodyBinidngsUnion );
95
107
}
96
108
109
+ Map <String , String > fieldToAliasMap = null ;
110
+ if (routingRule != null ) {
111
+ fieldToAliasMap = new HashMap <>();
112
+ for (RoutingParameter routingParameter : routingRule .getRoutingParametersList ()) {
113
+ Set <String > params = getPatternBindings (routingParameter .getPathTemplate ()).build ();
114
+ if (params .size () == 1 ) {
115
+ fieldToAliasMap .put (routingParameter .getField (), params .iterator ().next ());
116
+ }
117
+ }
118
+ }
119
+
97
120
Message message = inputMessageOpt .orElse (null );
98
121
return HttpBindings .builder ()
99
122
.setHttpVerb (HttpBindings .HttpVerb .valueOf (httpRule .getPatternCase ().toString ()))
100
123
.setPattern (pattern )
101
124
.setPathParameters (
102
- validateAndConstructHttpBindings (pathParamNames , message , messageTypes , true ))
125
+ validateAndConstructHttpBindings (
126
+ pathParamNames , fieldToAliasMap , message , messageTypes , true ))
103
127
.setQueryParameters (
104
- validateAndConstructHttpBindings (queryParamNames , message , messageTypes , false ))
128
+ validateAndConstructHttpBindings (queryParamNames , null , message , messageTypes , false ))
105
129
.setBodyParameters (
106
- validateAndConstructHttpBindings (bodyParamNames , message , messageTypes , false ))
130
+ validateAndConstructHttpBindings (bodyParamNames , null , message , messageTypes , false ))
107
131
.setIsAsteriskBody (body .equals (ASTERISK ))
108
132
.build ();
109
133
}
110
134
111
135
private static Set <HttpBinding > validateAndConstructHttpBindings (
112
136
Set <String > paramNames ,
137
+ Map <String , String > fieldToAliasMap ,
113
138
Message inputMessage ,
114
139
Map <String , Message > messageTypes ,
115
140
boolean isPath ) {
141
+
142
+ if (fieldToAliasMap != null ) {
143
+ paramNames = fieldToAliasMap .keySet ();
144
+ }
145
+
116
146
ImmutableSortedSet .Builder <HttpBinding > httpBindings = ImmutableSortedSet .naturalOrder ();
117
147
for (String paramName : paramNames ) {
118
148
// Handle foo.bar cases by descending into the subfields.
119
149
String [] subFields = paramName .split ("\\ ." );
120
150
if (inputMessage == null ) {
121
- httpBindings .add (HttpBinding .create (paramName , false ));
151
+ httpBindings .add (
152
+ HttpBinding .create (paramName , false , getAlias (paramName , fieldToAliasMap )));
122
153
continue ;
123
154
}
124
155
Message nestedMessage = inputMessage ;
@@ -138,13 +169,23 @@ private static Set<HttpBinding> validateAndConstructHttpBindings(
138
169
checkHttpFieldIsValid (subFieldName , nestedMessage , !isPath );
139
170
}
140
171
Field field = nestedMessage .fieldMap ().get (subFieldName );
141
- httpBindings .add (HttpBinding .create (paramName , field .isProto3Optional ()));
172
+ httpBindings .add (
173
+ HttpBinding .create (
174
+ paramName , field .isProto3Optional (), getAlias (paramName , fieldToAliasMap )));
142
175
}
143
176
}
144
177
}
145
178
return httpBindings .build ();
146
179
}
147
180
181
+ private static String getAlias (String paramName , Map <String , String > fieldToAliasMap ) {
182
+ if (fieldToAliasMap != null && fieldToAliasMap .containsKey (paramName )) {
183
+ return fieldToAliasMap .get (paramName );
184
+ } else {
185
+ return paramName ;
186
+ }
187
+ }
188
+
148
189
private static String getHttpVerbPattern (HttpRule httpRule ) {
149
190
PatternCase patternCase = httpRule .getPatternCase ();
150
191
switch (patternCase ) {
0 commit comments