mdeuser commented on a change in pull request #2966: WIP: Path parameter
support for API GW
URL:
https://github.com/apache/incubator-openwhisk/pull/2966#discussion_r155074743
##########
File path: core/routemgmt/common/apigw-utils.js
##########
@@ -24,6 +24,175 @@ var _ = require('lodash');
const ApimgmtUserAgent = "OpenWhisk-apimgmt/1.0.0";
var UserAgent = ApimgmtUserAgent;
+/**
+ * Helper method for the validateFinalSwagger function. Generates a map of
operationId to target-url strings so we
+ * can validate that each operationId we find that has a parameter in the path
also has its target-url appended with
+ * $(request.path)
+ *
+ * @param ibmConfig Required. The 'x-ibm-configuration' portion of the
swaggerApi.
+ * @return A map of operationId->target-url pairs for checking.
+ */
+function generateTargetUrlMap(ibmConfig) {
+ var targetUrls = {};
+ ibmConfig['assembly']['execute'].forEach(function(exec) {
+ if (exec['operation-switch'] && exec['operation-switch']['case']) {
+ exec['operation-switch']['case'].forEach(function(element) {
+ var operations = element['operations'];
+ var isOperationWereLookingFor = false;
+ var ops = [];
+ if (operations) {
+ operations.forEach(function(op){
+ ops.push(op);
+ });
+ }
+ ops.forEach(function(op){
+ element['execute'].forEach(function(exec) {
+ targetUrls[op] = exec['invoke']['target-url'];
+ });
+ });
+ });
+ }
+
+ });
+ return targetUrls;
+}
+
+/**
+ * Helper function that just validates whether a relative path meets the
following conditions:
+ * 1. It has not path parameters
+ * 2. If it has path parameters, that the parameters are well formed (i.e.
each param is surrounded by {}).
+ *
+ * @param relativePath Required. The relative path we are checking.
+ * @return True if the path is valid, false otherwise.
+ */
+function isValidRelativePath(relativePath) {
+ if (isParameterizedPath(relativePath)) {
+ var open = relativePath.indexOf('{', 0);
+ var close = relativePath.indexOf('}', open);
+ while (open != -1 || close != -1) {
+ if (open == -1 || close == -1) {
+ //Could not find a matching close brace
+ return false;
+ }
+ open = relativePath.indexOf('{', close);
+ close = open == -1 ? relativePath.indexOf('}', close + 1) :
relativePath.indexOf('}', open);
+ }
+ }
+ return true;
+}
+
+/**
+ * Simple function to get the name of each path parameter defined in the path.
+ *
+ * @param path Required. The path we are checking.
+ * @return An array that contains each named path parameter, or an empty list
if none are found.
+ */
+function getPathParameters(relativePath) {
+ var params = [];
+ if (isParameterizedPath(relativePath)) {
+ for (var open = relativePath.indexOf('{', 0); open != -1 ;) {
+ var close = relativePath.indexOf('}', open);
+ params.push(relativePath.substring(open+1, close));
+ open = relativePath.indexOf('{', close);
+ }
+ }
+ return params;
+}
+
+/**
+ * Simple function to determine whether this path contains parameters
+ *
+ * @param path Required. The path we are checking.
+ * @return True if the path contains parameters, false otherwise.
+ */
+function isParameterizedPath(path) {
+ return path != null && ((path.indexOf('{') >= 0) || path.indexOf('}') >= 0);
Review comment:
picky nit. indexOf() will return -1 if no match... so `!= -1`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services