예제 #1
0
        /// <summary>
        /// Adds ListNext() method for each List method with x-ms-pageable extension.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="codeNamer"></param>
        public static void AddPageableMethod(ServiceClient serviceClient, CodeNamer codeNamer)
        {
            if (codeNamer == null)
            {
                throw new ArgumentNullException("codeNamer");
            }
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }

            foreach (var method in serviceClient.Methods.ToArray())
            {
                if (method.Extensions.ContainsKey(PageableExtension))
                {
                    var pageableExtension = JsonConvert.DeserializeObject <PageableExtension>(method.Extensions[PageableExtension].ToString());
                    if (string.IsNullOrWhiteSpace(pageableExtension.NextLinkName))
                    {
                        continue;
                    }

                    Method nextLinkMethod = null;
                    if (!string.IsNullOrEmpty(pageableExtension.OperationName))
                    {
                        nextLinkMethod = serviceClient.Methods.FirstOrDefault(m =>
                                                                              pageableExtension.OperationName.Equals(m.SerializedName, StringComparison.OrdinalIgnoreCase));
                        if (nextLinkMethod != null)
                        {
                            nextLinkMethod.Extensions["nextLinkMethod"] = true;
                        }
                    }

                    if (nextLinkMethod == null)
                    {
                        nextLinkMethod = (Method)method.Clone();

                        if (!string.IsNullOrEmpty(pageableExtension.OperationName))
                        {
                            nextLinkMethod.Name = codeNamer.GetMethodName(SwaggerModeler.GetMethodName(
                                                                              new Rest.Modeler.Swagger.Model.Operation {
                                OperationId = pageableExtension.OperationName
                            }));
                            nextLinkMethod.Group = codeNamer.GetMethodGroupName(SwaggerModeler.GetMethodGroup(
                                                                                    new Rest.Modeler.Swagger.Model.Operation {
                                OperationId = pageableExtension.OperationName
                            }));
                        }
                        else
                        {
                            nextLinkMethod.Name = nextLinkMethod.Name + "Next";
                        }
                        method.Extensions["nextMethodName"]         = nextLinkMethod.Name;
                        method.Extensions["nextMethodGroup"]        = nextLinkMethod.Group;
                        nextLinkMethod.Extensions["nextLinkMethod"] = true;
                        nextLinkMethod.Parameters.Clear();
                        nextLinkMethod.Url           = "{nextLink}";
                        nextLinkMethod.IsAbsoluteUrl = true;
                        var nextLinkParameter = new Parameter
                        {
                            Name           = "nextPageLink",
                            SerializedName = "nextLink",
                            Type           = new PrimaryType(KnownPrimaryType.String),
                            Documentation  = "The NextLink from the previous successful call to List operation.",
                            IsRequired     = true,
                            Location       = ParameterLocation.Path
                        };
                        nextLinkParameter.Extensions[SkipUrlEncodingExtension] = true;
                        nextLinkMethod.Parameters.Add(nextLinkParameter);

                        // Need copy all the header parameters from List method to ListNext method
                        foreach (var param in method.Parameters.Where(p => p.Location == ParameterLocation.Header))
                        {
                            nextLinkMethod.Parameters.Add((Parameter)param.Clone());
                        }

                        // Copy all grouped parameters that only contain header parameters
                        nextLinkMethod.InputParameterTransformation.Clear();
                        method.InputParameterTransformation.GroupBy(t => t.ParameterMappings[0].InputParameter)
                        .ForEach(grouping => {
                            if (grouping.All(t => t.OutputParameter.Location == ParameterLocation.Header))
                            {
                                // All grouped properties were header parameters, reuse data type
                                nextLinkMethod.Parameters.Add(grouping.Key);
                                grouping.ForEach(t => nextLinkMethod.InputParameterTransformation.Add(t));
                            }
                            else if (grouping.Any(t => t.OutputParameter.Location == ParameterLocation.Header))
                            {
                                // Some grouped properties were header parameters, creating new data types
                                var headerGrouping = grouping.Where(t => t.OutputParameter.Location == ParameterLocation.Header);
                                headerGrouping.ForEach(t => nextLinkMethod.InputParameterTransformation.Add((ParameterTransformation)t.Clone()));
                                var newGroupingParam = CreateParameterFromGrouping(headerGrouping, nextLinkMethod, serviceClient);
                                nextLinkMethod.Parameters.Add(newGroupingParam);
                                //grouping.Key.Name = newGroupingParam.Name;
                                var inputParameter  = (Parameter)nextLinkMethod.InputParameterTransformation.First().ParameterMappings[0].InputParameter.Clone();
                                inputParameter.Name = codeNamer.GetParameterName(newGroupingParam.Name);
                                nextLinkMethod.InputParameterTransformation.ForEach(t => t.ParameterMappings[0].InputParameter = inputParameter);
                            }
                        });

                        serviceClient.Methods.Add(nextLinkMethod);
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Adds ListNext() method for each List method with x-ms-pageable extension.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="codeNamer"></param>
        public static void AddPageableMethod(ServiceClient serviceClient, CodeNamer codeNamer)
        {
            if (codeNamer == null)
            {
                throw new ArgumentNullException("codeNamer");
            }
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }

            foreach (var method in serviceClient.Methods.ToArray())
            {
                if (method.Extensions.ContainsKey(PageableExtension))
                {
                    var pageableExtension = JsonConvert.DeserializeObject <PageableExtension>(method.Extensions[PageableExtension].ToString());
                    if (string.IsNullOrWhiteSpace(pageableExtension.NextLinkName))
                    {
                        continue;
                    }

                    Method nextLinkMethod = null;
                    if (!string.IsNullOrEmpty(pageableExtension.OperationName))
                    {
                        nextLinkMethod = serviceClient.Methods.FirstOrDefault(m =>
                                                                              pageableExtension.OperationName.Equals(m.SerializedName, StringComparison.OrdinalIgnoreCase));
                    }

                    if (nextLinkMethod == null)
                    {
                        nextLinkMethod = (Method)method.Clone();

                        if (!string.IsNullOrEmpty(pageableExtension.OperationName))
                        {
                            nextLinkMethod.Name = codeNamer.GetMethodName(SwaggerModeler.GetMethodName(
                                                                              new Rest.Modeler.Swagger.Model.Operation {
                                OperationId = pageableExtension.OperationName
                            }));
                            nextLinkMethod.Group = codeNamer.GetMethodGroupName(SwaggerModeler.GetMethodGroup(
                                                                                    new Rest.Modeler.Swagger.Model.Operation {
                                OperationId = pageableExtension.OperationName
                            }));
                        }
                        else
                        {
                            nextLinkMethod.Name = nextLinkMethod.Name + "Next";
                        }
                        nextLinkMethod.Parameters.Clear();
                        nextLinkMethod.Url           = "{nextLink}";
                        nextLinkMethod.IsAbsoluteUrl = true;
                        var nextLinkParameter = new Parameter
                        {
                            Name           = "nextPageLink",
                            SerializedName = "nextLink",
                            Type           = PrimaryType.String,
                            Documentation  = "The NextLink from the previous successful call to List operation.",
                            IsRequired     = true,
                            Location       = ParameterLocation.Path
                        };
                        nextLinkParameter.Extensions[SkipUrlEncodingExtension] = true;
                        nextLinkMethod.Parameters.Add(nextLinkParameter);

                        // Need copy all the header parameters from List method to ListNext method
                        foreach (var param in method.Parameters.Where(p => p.Location == ParameterLocation.Header))
                        {
                            nextLinkMethod.Parameters.Add((Parameter)param.Clone());
                        }

                        serviceClient.Methods.Add(nextLinkMethod);
                    }
                }
            }
        }