-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Check additional_binding's custom FieldsExtractor #1565
Closed
Closed
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7db8f57
chore: Add additional bindings logic
lqiu96 fe94626
chore: Update showcase goldens
lqiu96 d6d6914
chore: Fix additional bindings logic
lqiu96 a693d25
chore: Update tests
lqiu96 3efafb1
chore: Update operations to use additional bindings mapping logic
lqiu96 747255f
chore: Update proto message request formatter
lqiu96 e46e3c4
chore: Remove unused method
lqiu96 f8e831e
chore: Clean up ProtoMessageRequestFormatter
lqiu96 f66fe66
chore: Clean up the files
lqiu96 1bf0a9c
chore: Update ProtoMessageRequestFormatter
lqiu96 357be41
chore: Add back public method
lqiu96 e18d8b8
chore: Address code smell
lqiu96 2dd4eb7
chore: Resolve merge conflicts
lqiu96 6beb873
chore: Fix tests
lqiu96 c0d5c23
Merge branch 'main' into main-http_additional_bindings_part_2
lqiu96 570683b
chore: Add back public methods
lqiu96 48751ff
chore: Add test case for additionalBindings
lqiu96 f49db68
chore: Fix tests
lqiu96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,11 +32,11 @@ | |
import com.google.api.core.BetaApi; | ||
import com.google.api.core.InternalApi; | ||
import com.google.api.pathtemplate.PathTemplate; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.protobuf.Message; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** Creates parts of a HTTP request from a protobuf message. */ | ||
public class ProtoMessageRequestFormatter<RequestT extends Message> | ||
|
@@ -50,35 +50,36 @@ public class ProtoMessageRequestFormatter<RequestT extends Message> | |
private final String rawPath; | ||
private final PathTemplate pathTemplate; | ||
private final FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor; | ||
private final List<String> additionalRawPaths; | ||
private final List<PathTemplate> additionalPathTemplates; | ||
private final Map<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>> | ||
additionalPathsExtractorMap; | ||
|
||
private ProtoMessageRequestFormatter( | ||
FieldsExtractor<RequestT, String> requestBodyExtractor, | ||
FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor, | ||
String rawPath, | ||
PathTemplate pathTemplate, | ||
FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor, | ||
List<String> additionalRawPaths, | ||
List<PathTemplate> additionalPathTemplates) { | ||
Map<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>> | ||
additionalPathsExtractorMap) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a private constructor which should give us some flexibility to update the parameters. |
||
this.requestBodyExtractor = requestBodyExtractor; | ||
this.queryParamsExtractor = queryParamsExtractor; | ||
this.rawPath = rawPath; | ||
this.pathTemplate = pathTemplate; | ||
this.pathVarsExtractor = pathVarsExtractor; | ||
this.additionalRawPaths = additionalRawPaths; | ||
this.additionalPathTemplates = additionalPathTemplates; | ||
// RPCs may not define additional bindings -- Set as an empty map | ||
this.additionalPathsExtractorMap = | ||
additionalPathsExtractorMap == null ? ImmutableMap.of() : additionalPathsExtractorMap; | ||
} | ||
|
||
public static <RequestT extends Message> | ||
ProtoMessageRequestFormatter.Builder<RequestT> newBuilder() { | ||
return new Builder<RequestT>().setAdditionalPaths(); | ||
return new Builder<>(); | ||
} | ||
|
||
public Builder<RequestT> toBuilder() { | ||
return new Builder<RequestT>() | ||
.setPath(rawPath, pathVarsExtractor) | ||
.setAdditionalPaths(additionalRawPaths.toArray(new String[] {})) | ||
.setAdditionalPathsExtractorMap(additionalPathsExtractorMap) | ||
.setQueryParamsExtractor(queryParamsExtractor) | ||
.setRequestBodyExtractor(requestBodyExtractor); | ||
} | ||
|
@@ -110,8 +111,11 @@ public String getPath(RequestT apiMessage) { | |
if (pathTemplate.matches(path)) { | ||
return path; | ||
} | ||
for (PathTemplate additionalPathTemplate : additionalPathTemplates) { | ||
String additionalPath = additionalPathTemplate.instantiate(pathVarsMap); | ||
for (Map.Entry<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>> entrySet : | ||
additionalPathsExtractorMap.entrySet()) { | ||
PathTemplate additionalPathTemplate = entrySet.getKey(); | ||
FieldsExtractor<RequestT, Map<String, String>> pathExtractor = entrySet.getValue(); | ||
String additionalPath = additionalPathTemplate.instantiate(pathExtractor.extract(apiMessage)); | ||
if (additionalPathTemplate.matches(additionalPath)) { | ||
return additionalPath; | ||
} | ||
|
@@ -124,7 +128,7 @@ public String getPath(RequestT apiMessage) { | |
@BetaApi | ||
@Override | ||
public List<PathTemplate> getAdditionalPathTemplates() { | ||
return additionalPathTemplates; | ||
return ImmutableList.copyOf(additionalPathsExtractorMap.keySet()); | ||
} | ||
|
||
/* {@inheritDoc} */ | ||
|
@@ -140,7 +144,8 @@ public static class Builder<RequestT extends Message> { | |
private FieldsExtractor<RequestT, Map<String, List<String>>> queryParamsExtractor; | ||
private String rawPath; | ||
private FieldsExtractor<RequestT, Map<String, String>> pathVarsExtractor; | ||
private List<String> rawAdditionalPaths; | ||
private Map<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>> | ||
additionalPathsExtractorMap; | ||
|
||
public Builder<RequestT> setRequestBodyExtractor( | ||
FieldsExtractor<RequestT, String> requestBodyExtractor) { | ||
|
@@ -161,9 +166,10 @@ public Builder<RequestT> setPath( | |
return this; | ||
} | ||
|
||
@BetaApi | ||
public Builder<RequestT> setAdditionalPaths(String... rawAdditionalPaths) { | ||
this.rawAdditionalPaths = Arrays.asList(rawAdditionalPaths); | ||
public Builder<RequestT> setAdditionalPathsExtractorMap( | ||
Map<PathTemplate, FieldsExtractor<RequestT, Map<String, String>>> | ||
additionalPathsExtractorMap) { | ||
this.additionalPathsExtractorMap = additionalPathsExtractorMap; | ||
lqiu96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this; | ||
} | ||
|
||
|
@@ -186,8 +192,7 @@ public ProtoMessageRequestFormatter<RequestT> build() { | |
rawPath, | ||
PathTemplate.create(rawPath), | ||
pathVarsExtractor, | ||
rawAdditionalPaths, | ||
rawAdditionalPaths.stream().map(PathTemplate::create).collect(Collectors.toList())); | ||
additionalPathsExtractorMap); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setAdditionalPatterns
above just gets the HttpVerb associated with theadditional_bindings
list.setAdditionalBindings
recursively calls this function to get the nestedadditional_bindings
.