|
19 | 19 | import com.google.api.HttpRule.PatternCase;
|
20 | 20 | import com.google.api.generator.gapic.model.Field;
|
21 | 21 | import com.google.api.generator.gapic.model.HttpBindings;
|
| 22 | +import com.google.api.generator.gapic.model.HttpBindings.HttpBinding; |
22 | 23 | import com.google.api.generator.gapic.model.Message;
|
23 | 24 | import com.google.api.pathtemplate.PathTemplate;
|
24 | 25 | import com.google.common.base.Preconditions;
|
@@ -70,63 +71,79 @@ private static HttpBindings parseHttpRuleHelper(
|
70 | 71 | }
|
71 | 72 | }
|
72 | 73 |
|
73 |
| - Set<String> bindings = bindingsBuilder.build(); |
74 |
| - |
75 |
| - // Binding validation. |
76 |
| - for (String binding : bindings) { |
77 |
| - // Handle foo.bar cases by descending into the subfields. |
78 |
| - String[] descendantBindings = binding.split("\\."); |
79 |
| - Optional<Message> containingMessageOpt = inputMessageOpt; |
80 |
| - for (int i = 0; i < descendantBindings.length; i++) { |
81 |
| - String subField = descendantBindings[i]; |
82 |
| - if (!containingMessageOpt.isPresent()) { |
83 |
| - continue; |
84 |
| - } |
85 |
| - |
86 |
| - if (i < descendantBindings.length - 1) { |
87 |
| - Field field = containingMessageOpt.get().fieldMap().get(subField); |
88 |
| - containingMessageOpt = Optional.of(messageTypes.get(field.type().reference().fullName())); |
89 |
| - Preconditions.checkNotNull( |
90 |
| - containingMessageOpt.get(), |
91 |
| - String.format( |
92 |
| - "No containing message found for field %s with type %s", |
93 |
| - field.name(), field.type().reference().simpleName())); |
94 |
| - } else { |
95 |
| - checkHttpFieldIsValid(subField, containingMessageOpt.get(), false); |
96 |
| - } |
97 |
| - } |
98 |
| - } |
| 74 | + Set<String> pathParamNames = bindingsBuilder.build(); |
99 | 75 |
|
100 | 76 | // TODO: support nested message fields bindings
|
101 | 77 | String body = httpRule.getBody();
|
102 |
| - Set<String> bodyParameters; |
103 |
| - Set<String> queryParameters; |
| 78 | + Set<String> bodyParamNames; |
| 79 | + Set<String> queryParamNames; |
104 | 80 | if (!inputMessageOpt.isPresent()) {
|
105 | 81 | // Must be a mixin, do not support full HttpRuleBindings for now
|
106 |
| - bodyParameters = ImmutableSet.of(); |
107 |
| - queryParameters = ImmutableSet.of(); |
| 82 | + bodyParamNames = ImmutableSet.of(); |
| 83 | + queryParamNames = ImmutableSet.of(); |
108 | 84 | } else if (Strings.isNullOrEmpty(body)) {
|
109 |
| - bodyParameters = ImmutableSet.of(); |
110 |
| - queryParameters = Sets.difference(inputMessageOpt.get().fieldMap().keySet(), bindings); |
| 85 | + bodyParamNames = ImmutableSet.of(); |
| 86 | + queryParamNames = Sets.difference(inputMessageOpt.get().fieldMap().keySet(), pathParamNames); |
111 | 87 | } else if (body.equals(ASTERISK)) {
|
112 |
| - bodyParameters = Sets.difference(inputMessageOpt.get().fieldMap().keySet(), bindings); |
113 |
| - queryParameters = ImmutableSet.of(); |
| 88 | + bodyParamNames = Sets.difference(inputMessageOpt.get().fieldMap().keySet(), pathParamNames); |
| 89 | + queryParamNames = ImmutableSet.of(); |
114 | 90 | } else {
|
115 |
| - bodyParameters = ImmutableSet.of(body); |
116 |
| - Set<String> bodyBinidngsUnion = Sets.union(bodyParameters, bindings); |
117 |
| - queryParameters = |
| 91 | + bodyParamNames = ImmutableSet.of(body); |
| 92 | + Set<String> bodyBinidngsUnion = Sets.union(bodyParamNames, pathParamNames); |
| 93 | + queryParamNames = |
118 | 94 | Sets.difference(inputMessageOpt.get().fieldMap().keySet(), bodyBinidngsUnion);
|
119 | 95 | }
|
120 | 96 |
|
| 97 | + Message message = inputMessageOpt.orElse(null); |
121 | 98 | return HttpBindings.builder()
|
122 | 99 | .setHttpVerb(HttpBindings.HttpVerb.valueOf(httpRule.getPatternCase().toString()))
|
123 | 100 | .setPattern(pattern)
|
124 |
| - .setPathParameters(ImmutableSortedSet.copyOf(bindings)) |
125 |
| - .setQueryParameters(ImmutableSortedSet.copyOf(queryParameters)) |
126 |
| - .setBodyParameters(ImmutableSortedSet.copyOf(bodyParameters)) |
| 101 | + .setPathParameters( |
| 102 | + validateAndConstructHttpBindings(pathParamNames, message, messageTypes, true)) |
| 103 | + .setQueryParameters( |
| 104 | + validateAndConstructHttpBindings(queryParamNames, message, messageTypes, false)) |
| 105 | + .setBodyParameters( |
| 106 | + validateAndConstructHttpBindings(bodyParamNames, message, messageTypes, false)) |
127 | 107 | .build();
|
128 | 108 | }
|
129 | 109 |
|
| 110 | + private static Set<HttpBinding> validateAndConstructHttpBindings( |
| 111 | + Set<String> paramNames, |
| 112 | + Message inputMessage, |
| 113 | + Map<String, Message> messageTypes, |
| 114 | + boolean isPath) { |
| 115 | + ImmutableSortedSet.Builder<HttpBinding> httpBindings = ImmutableSortedSet.naturalOrder(); |
| 116 | + for (String paramName : paramNames) { |
| 117 | + // Handle foo.bar cases by descending into the subfields. |
| 118 | + String[] subFields = paramName.split("\\."); |
| 119 | + if (inputMessage == null) { |
| 120 | + httpBindings.add(HttpBinding.create(paramName, false)); |
| 121 | + continue; |
| 122 | + } |
| 123 | + Message nestedMessage = inputMessage; |
| 124 | + for (int i = 0; i < subFields.length; i++) { |
| 125 | + String subFieldName = subFields[i]; |
| 126 | + if (i < subFields.length - 1) { |
| 127 | + Field field = nestedMessage.fieldMap().get(subFieldName); |
| 128 | + nestedMessage = messageTypes.get(field.type().reference().fullName()); |
| 129 | + Preconditions.checkNotNull( |
| 130 | + nestedMessage, |
| 131 | + String.format( |
| 132 | + "No containing message found for field %s with type %s", |
| 133 | + field.name(), field.type().reference().simpleName())); |
| 134 | + |
| 135 | + } else { |
| 136 | + if (isPath) { |
| 137 | + checkHttpFieldIsValid(subFieldName, nestedMessage, !isPath); |
| 138 | + } |
| 139 | + Field field = nestedMessage.fieldMap().get(subFieldName); |
| 140 | + httpBindings.add(HttpBinding.create(paramName, field.isProto3Optional())); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + return httpBindings.build(); |
| 145 | + } |
| 146 | + |
130 | 147 | private static String getHttpVerbPattern(HttpRule httpRule) {
|
131 | 148 | PatternCase patternCase = httpRule.getPatternCase();
|
132 | 149 | switch (patternCase) {
|
|
0 commit comments