/// <summary> /// Changes paginated method signatures to return Page type. /// </summary> /// <param name="serviceClient"></param> /// <param name="pageClasses"></param> public virtual void NormalizePaginatedMethods(ServiceClient serviceClient, IDictionary <KeyValuePair <string, string>, string> pageClasses) { if (serviceClient == null) { throw new ArgumentNullException("serviceClient"); } var convertedTypes = new Dictionary <IType, CompositeType>(); foreach (var method in serviceClient.Methods.Where(m => m.Extensions.ContainsKey(AzureExtensions.PageableExtension))) { string nextLinkString; string pageClassName = GetPagingSetting(method.Extensions, pageClasses, out nextLinkString); if (string.IsNullOrEmpty(pageClassName)) { continue; } var pageTypeFormat = "{0}<{1}>"; var ipageTypeFormat = "IPage<{0}>"; if (string.IsNullOrWhiteSpace(nextLinkString)) { ipageTypeFormat = "IEnumerable<{0}>"; } foreach (var responseStatus in method.Responses .Where(r => r.Value.Body is CompositeType).Select(s => s.Key).ToArray()) { var compositType = (CompositeType)method.Responses[responseStatus].Body; var sequenceType = compositType.Properties.Select(p => p.Type).FirstOrDefault(t => t is SequenceType) as SequenceType; // if the type is a wrapper over page-able response if (sequenceType != null) { var pagableTypeName = string.Format(CultureInfo.InvariantCulture, pageTypeFormat, pageClassName, sequenceType.ElementType.Name); var ipagableTypeName = string.Format(CultureInfo.InvariantCulture, ipageTypeFormat, sequenceType.ElementType.Name); CompositeType pagedResult = new CompositeType { Name = pagableTypeName }; pagedResult.Extensions[AzureExtensions.ExternalExtension] = true; pagedResult.Extensions[AzureExtensions.PageableExtension] = ipagableTypeName; convertedTypes[method.Responses[responseStatus].Body] = pagedResult; method.Responses[responseStatus] = new Response(pagedResult, method.Responses[responseStatus].Headers); } } if (convertedTypes.ContainsKey(method.ReturnType.Body)) { method.ReturnType = new Response(convertedTypes[method.ReturnType.Body], method.ReturnType.Headers); } } AzureExtensions.RemoveUnreferencedTypes(serviceClient, convertedTypes.Keys.Cast <CompositeType>().Select(t => t.Name)); }
/// <summary> /// Changes paginated method signatures to return Page type. /// </summary> /// <param name="serviceClient"></param> private void NormalizePaginatedMethods(ServiceClient serviceClient) { if (serviceClient == null) { throw new ArgumentNullException("serviceClient"); } var convertedTypes = new Dictionary <IType, Response>(); foreach (var method in serviceClient.Methods.Where(m => m.Extensions.ContainsKey(AzureExtensions.PageableExtension))) { foreach (var responseStatus in method.Responses.Where(r => r.Value.Body is CompositeType).Select(s => s.Key)) { var compositType = (CompositeType)method.Responses[responseStatus].Body; var sequenceType = compositType.Properties.Select(p => p.Type).FirstOrDefault(t => t is SequenceType) as SequenceType; // if the type is a wrapper over page-able response if (sequenceType != null) { string pagableTypeName = GetPagingSetting(method.Extensions, sequenceType.ElementType.Name); CompositeType pagedResult = new CompositeType { Name = pagableTypeName }; convertedTypes[compositType] = new Response(pagedResult, null); method.Responses[responseStatus] = convertedTypes[compositType]; break; } } if (convertedTypes.ContainsKey(method.ReturnType.Body)) { method.ReturnType = convertedTypes[method.ReturnType.Body]; } } AzureExtensions.RemoveUnreferencedTypes(serviceClient, convertedTypes.Keys.Cast <CompositeType>().Select(t => t.Name)); }