Example #1
0
        /// <summary>
        /// Try to bind the idenfier as operation import (function import or action import) segment,
        /// Append it into path.
        /// </summary>
        private static bool TryBindOperationImport(string identifier, string parenthesisExpressions,
                                                   IEdmModel model, IList <PathSegment> path, PathParserSettings settings)
        {
            // split the parameter key/value pair
            parenthesisExpressions.ExtractKeyValuePairs(out IDictionary <string, string> parameters, out string remaining);
            IList <string> parameterNames = parameters == null ? null : parameters.Keys.ToList();

            IEdmOperationImport operationImport = OperationHelper.ResolveOperationImports(identifier, parameterNames, model, settings.EnableCaseInsensitive);

            if (operationImport != null)
            {
                operationImport.TryGetStaticEntitySet(model, out IEdmEntitySetBase entitySetBase);
                path.Add(new OperationImportSegment(operationImport, entitySetBase));
                if (remaining != null && operationImport.IsFunctionImport())
                {
                    IEdmFunction function = (IEdmFunction)operationImport.Operation;
                    if (function.IsComposable)
                    {
                        if (TryBindKeySegment(parenthesisExpressions, path))
                        {
                            return(true);
                        }
                    }
                }

                return(true);
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FunctionImportSegmentTemplate" /> class.
        /// </summary>
        /// <param name="segment">The input function import segment.</param>
        public FunctionImportSegmentTemplate(OperationImportSegment segment)
        {
            if (segment == null)
            {
                throw Error.ArgumentNull(nameof(segment));
            }

            IEdmOperationImport operationImport = segment.OperationImports.First();

            if (!operationImport.IsFunctionImport())
            {
                throw new ODataException(Error.Format(SRResources.SegmentShouldBeKind, "FunctionImport", "FunctionImportSegmentTemplate"));
            }

            FunctionImport = (IEdmFunctionImport)operationImport;

            NavigationSource = segment.EntitySet;

            ParameterMappings = OperationHelper.BuildParameterMappings(segment.Parameters, operationImport.Name);

            // join the parameters as p1={p1}
            Literal = FunctionImport.Name + "(" + string.Join(",", ParameterMappings.Select(a => $"{a.Key}={{{a.Value}}}")) + ")";

            IsSingle = FunctionImport.Function.ReturnType.TypeKind() != EdmTypeKind.Collection;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FunctionImportSegmentTemplate" /> class.
        /// </summary>
        /// <param name="segment">The input function import segment.</param>
        public FunctionImportSegmentTemplate(OperationImportSegment segment)
        {
            if (segment == null)
            {
                throw Error.ArgumentNull(nameof(segment));
            }

            IEdmOperationImport operationImport = segment.OperationImports.First();

            if (!operationImport.IsFunctionImport())
            {
                throw new ODataException(Error.Format(SRResources.SegmentShouldBeKind, "FunctionImport", "FunctionImportSegmentTemplate"));
            }

            FunctionImport = (IEdmFunctionImport)operationImport;

            NavigationSource = segment.EntitySet;

            ParameterMappings = OperationHelper.BuildParameterMappings(segment.Parameters, operationImport.Name);

            // join the parameters as p1={p1}
            string routeKey     = ParameterMappings.BuildRouteKey();
            string parameterStr = ParameterMappings.Count == 0 ? "()" : $"({{{routeKey}}})";

            Literal = FunctionImport.Name + parameterStr;

            IsSingle = FunctionImport.Function.ReturnType.TypeKind() != EdmTypeKind.Collection;
        }
Example #4
0
        /// <summary>
        /// Get the Uri Swagger path for Edm operation import.
        /// </summary>
        /// <param name="operationImport">The Edm operation import.</param>
        /// <returns>The <see cref="System.String"/> path represents the related Edm operation import.</returns>
        public static string GetPathForOperationImport(IEdmOperationImport operationImport)
        {
            if (operationImport == null)
            {
                return(String.Empty);
            }

            string swaggerOperationImportPath = "/" + operationImport.Name + "(";

            if (operationImport.IsFunctionImport())
            {
                foreach (var parameter in operationImport.Operation.Parameters)
                {
                    swaggerOperationImportPath += parameter.Name + "=" + "{" + parameter.Name + "},";
                }
            }
            if (swaggerOperationImportPath.EndsWith(",", StringComparison.Ordinal))
            {
                swaggerOperationImportPath = swaggerOperationImportPath.Substring(0,
                                                                                  swaggerOperationImportPath.Length - 1);
            }
            swaggerOperationImportPath += ")";

            return(swaggerOperationImportPath);
        }
        /// <summary>
        /// Get the Uri Swagger path for Edm operation import.
        /// </summary>
        /// <param name="operationImport">The Edm operation import.</param>
        /// <returns>
        /// The <see cref="string" /> path represents the related Edm operation import.
        /// </returns>
        public static string GetPathForOperationImport(IEdmOperationImport operationImport)
        {
            Contract.Requires(operationImport != null);

            var swaggerOperationImportPath = operationImport.Name;

            if (operationImport.IsFunctionImport())
            {
                swaggerOperationImportPath += "(";
                swaggerOperationImportPath  = operationImport.Operation?.Parameters?.Aggregate(swaggerOperationImportPath, (current, parameter) => current + parameter.Name + "=" + "{" + parameter.Name + "},");
            }
            Contract.Assume(swaggerOperationImportPath != null);
            if (swaggerOperationImportPath.EndsWith(",", StringComparison.Ordinal))
            {
                swaggerOperationImportPath = swaggerOperationImportPath.Substring(0, swaggerOperationImportPath.Length - 1);
            }
            if (operationImport.IsFunctionImport())
            {
                swaggerOperationImportPath += ")";
            }

            return(swaggerOperationImportPath);
        }
Example #6
0
        /// <inheritdoc/>
        public override bool TryMatch(ODataPathSegment pathSegment, IDictionary <string, object> values)
        {
            OperationImportSegment other = pathSegment as OperationImportSegment;

            if (other == null)
            {
                return(false);
            }

            IEdmOperationImport operationImport = Segment.OperationImports.First();
            IEdmOperationImport otherImport     = other.OperationImports.First();

            // for unbound action, just compare the action import
            if (operationImport.IsActionImport() && otherImport.IsActionImport())
            {
                return(operationImport == otherImport);
            }
            else if (operationImport.IsFunctionImport() && otherImport.IsFunctionImport())
            {
                // but for unbound function, we should compare the parameter names and
                // process the parameter values into odata routes.
                if (operationImport.Name != otherImport.Name)
                {
                    return(false);
                }

                IDictionary <string, object> parameterValues = new Dictionary <string, object>();
                foreach (var parameter in other.Parameters)
                {
                    object value = other.GetParameterValue(parameter.Name);
                    parameterValues[parameter.Name] = value;
                }

                if (RoutingConventionHelpers.TryMatch(ParameterMappings, parameterValues, values))
                {
                    foreach (var operationSegmentParameter in other.Parameters)
                    {
                        string name  = operationSegmentParameter.Name;
                        object value = parameterValues[name];

                        RoutingConventionHelpers.AddFunctionParameters((IEdmFunction)otherImport.Operation, name,
                                                                       value, values, values, ParameterMappings);
                    }

                    return(true);
                }
            }

            return(false);
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OperationImportSegmentTemplate"/> class.
        /// </summary>
        /// <param name="segment">The operation import segment</param>
        public OperationImportSegmentTemplate(OperationImportSegment segment)
        {
            if (segment == null)
            {
                throw Error.ArgumentNull("segment");
            }

            Segment = segment;

            IEdmOperationImport operation = Segment.OperationImports.First();

            if (operation.IsFunctionImport())
            {
                ParameterMappings = RoutingConventionHelpers.BuildParameterMappings(segment.Parameters, operation.Name);
            }
        }
Example #8
0
        static string GetPathForOperationImport(IEdmOperationImport operationImport)
        {
            string swaggerOperationImportPath = "/" + operationImport.Name + "(";

            if (operationImport.IsFunctionImport())
            {
                foreach (var parameter in operationImport.Operation.Parameters)
                {
                    swaggerOperationImportPath += parameter.Name + "=" + "{" + parameter.Name + "},";
                }
            }
            if (swaggerOperationImportPath.EndsWith(","))
            {
                swaggerOperationImportPath = swaggerOperationImportPath.Substring(0,
                                                                                  swaggerOperationImportPath.Length - 1);
            }
            swaggerOperationImportPath += ")";

            return(swaggerOperationImportPath);
        }
Example #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FunctionImportSegmentTemplate" /> class.
        /// </summary>
        /// <param name="segment">The input function import segment.</param>
        public FunctionImportSegmentTemplate(OperationImportSegment segment)
        {
            if (segment == null)
            {
                throw Error.ArgumentNull(nameof(segment));
            }

            IEdmOperationImport operationImport = segment.OperationImports.First();

            if (!operationImport.IsFunctionImport())
            {
                throw new ODataException(Error.Format(SRResources.SegmentShouldBeKind, "FunctionImport", "FunctionImportSegmentTemplate"));
            }

            FunctionImport = (IEdmFunctionImport)operationImport;

            NavigationSource = segment.EntitySet;

            ParameterMappings = segment.Parameters.BuildParameterMappings(operationImport.Name);
        }
        /// <summary>
        /// Get the Uri Swagger path for Edm operation import.
        /// </summary>
        /// <param name="routePrefix">The route prefix.</param>
        /// <param name="operationImport">The Edm operation import.</param>
        /// <returns>
        /// The <see cref="string" /> path represents the related Edm operation import.
        /// </returns>
        public static string GetPathForOperationImport(string routePrefix, IEdmOperationImport operationImport)
        {
            if (operationImport == null)
            {
                return(string.Empty);
            }

            var swaggerOperationImportPath = routePrefix.AppendPathSegment(operationImport.Name) + "(";

            if (operationImport.IsFunctionImport())
            {
                swaggerOperationImportPath = operationImport.Operation.Parameters.Aggregate(swaggerOperationImportPath, (current, parameter) => current + parameter.Name + "=" + "{" + parameter.Name + "},");
            }
            if (swaggerOperationImportPath.EndsWith(",", StringComparison.Ordinal))
            {
                swaggerOperationImportPath = swaggerOperationImportPath.Substring(0, swaggerOperationImportPath.Length - 1);
            }
            swaggerOperationImportPath += ")";

            return(swaggerOperationImportPath);
        }
Example #11
0
File: Program.cs Project: TomDu/lab
        static string GetPathForOperationImport(IEdmOperationImport operationImport)
        {
            string swaggerOperationImportPath = "/" + operationImport.Name + "(";
            if (operationImport.IsFunctionImport())
            {
                foreach (var parameter in operationImport.Operation.Parameters)
                {
                    swaggerOperationImportPath += parameter.Name + "=" + "{" + parameter.Name + "},";
                }
            }
            if (swaggerOperationImportPath.EndsWith(","))
            {
                swaggerOperationImportPath = swaggerOperationImportPath.Substring(0,
                    swaggerOperationImportPath.Length - 1);
            }
            swaggerOperationImportPath += ")";

            return swaggerOperationImportPath;
        }
        /// <summary>
        /// Get the Uri Swagger path for Edm operation import.
        /// </summary>
        /// <param name="operationImport">The Edm operation import.</param>
        /// <returns>The <see cref="System.String"/> path represents the related Edm operation import.</returns>
        public static string GetPathForOperationImport(IEdmOperationImport operationImport)
        {
            if (operationImport == null)
            {
                return String.Empty;
            }

            string swaggerOperationImportPath = "/" + operationImport.Name + "(";
            if (operationImport.IsFunctionImport())
            {
                foreach (var parameter in operationImport.Operation.Parameters)
                {
                    swaggerOperationImportPath += parameter.Name + "=" + "{" + parameter.Name + "},";
                }
            }
            if (swaggerOperationImportPath.EndsWith(",", StringComparison.Ordinal))
            {
                swaggerOperationImportPath = swaggerOperationImportPath.Substring(0,
                    swaggerOperationImportPath.Length - 1);
            }
            swaggerOperationImportPath += ")";

            return swaggerOperationImportPath;
        }