internal static OperationGenerationMode GetOperationGenerationMode(IOperationNameGenerator operationNameGenerator)
        {
            if (operationNameGenerator is MultipleClientsFromOperationIdOperationNameGenerator)
            {
                return(OperationGenerationMode.MultipleClientsFromOperationId);
            }

            if (operationNameGenerator is MultipleClientsFromPathSegmentsOperationNameGenerator)
            {
                return(OperationGenerationMode.MultipleClientsFromPathSegments);
            }

            if (operationNameGenerator is MultipleClientsFromFirstTagAndPathSegmentsOperationNameGenerator)
            {
                return(OperationGenerationMode.MultipleClientsFromFirstTagAndPathSegments);
            }

            if (operationNameGenerator is MultipleClientsFromFirstTagAndOperationIdGenerator)
            {
                return(OperationGenerationMode.MultipleClientsFromFirstTagAndOperationId);
            }

            if (operationNameGenerator is SingleClientFromOperationIdOperationNameGenerator)
            {
                return(OperationGenerationMode.SingleClientFromOperationId);
            }

            if (operationNameGenerator is SingleClientFromPathSegmentsOperationNameGenerator)
            {
                return(OperationGenerationMode.SingleClientFromPathSegments);
            }

            return(OperationGenerationMode.MultipleClientsFromOperationId);
        }
Esempio n. 2
0
        private static string InjectWrapperClass(string csharpCode, string className, SwaggerDocument document, IOperationNameGenerator nameGenerator, out bool isSinlgeClient, out string clientName)
        {
            var clients = document.Operations.Select(o => nameGenerator.GetClientName(document, o.Path, o.Method, o.Operation)).Distinct().ToArray();

            if (clients.Length == 1)
            {
                isSinlgeClient = true;
                clientName     = clients[0] + "Client";
                return(csharpCode);
            }
            else
            {
                isSinlgeClient = false;
                clientName     = className;
                var properties = from c in clients
                                 let name                       = ConversionUtilities.ConvertToUpperCamelCase(c, true)
                                                       let type = name + "Client"
                                                                  select $"public {type} {(string.IsNullOrEmpty(name) ? "Root" : name)} {{ get; set; }}";
                var wrapperClass     = $@"    public class {className}
{{
    {ConversionUtilities.Tab(string.Join("\n", properties), 1)}
}}".Replace("\r\n", "\n");
                var namespaceClosing = csharpCode.LastIndexOf('}');
                return(csharpCode.Insert(namespaceClosing, ConversionUtilities.Tab(wrapperClass, 1) + "\n"));
            }
        }