Exemplo n.º 1
0
		public static CsharpMethod Clone(CsharpMethod method)
		{
			return new CsharpMethod
			{
				Allow404 = method.Allow404,
				Path = method.Path,
				RequestType = method.RequestType,
				ReturnDescription = method.ReturnDescription,
				Arguments = method.Arguments,
				CallTypeGeneric = method.CallTypeGeneric,
				DescriptorType = method.DescriptorType,
				DescriptorTypeGeneric = method.DescriptorTypeGeneric,
				Documentation = method.Documentation,
				FullName = method.FullName,
				HttpMethod = method.HttpMethod,
				Parts = method.Parts,
				QueryStringParamName = method.QueryStringParamName,
				RequestTypeGeneric = method.RequestTypeGeneric,
				RequestTypeUnmapped = method.RequestTypeUnmapped,
				ReturnType = method.ReturnType,
				ReturnTypeGeneric = method.ReturnTypeGeneric,
				Unmapped = method.Unmapped,
				Url = method.Url,
				SkipInterface = method.SkipInterface
			};
		}
Exemplo n.º 2
0
 public static CsharpMethod Clone(CsharpMethod method)
 {
     return(new CsharpMethod
     {
         Allow404 = method.Allow404,
         Path = method.Path,
         RequestType = method.RequestType,
         ReturnDescription = method.ReturnDescription,
         Arguments = method.Arguments,
         CallTypeGeneric = method.CallTypeGeneric,
         DescriptorType = method.DescriptorType,
         DescriptorTypeGeneric = method.DescriptorTypeGeneric,
         Documentation = method.Documentation,
         FullName = method.FullName,
         HttpMethod = method.HttpMethod,
         Parts = method.Parts,
         QueryStringParamName = method.QueryStringParamName,
         RequestTypeGeneric = method.RequestTypeGeneric,
         RequestTypeUnmapped = method.RequestTypeUnmapped,
         ReturnType = method.ReturnType,
         ReturnTypeGeneric = method.ReturnTypeGeneric,
         Unmapped = method.Unmapped,
         Url = method.Url,
         SkipInterface = method.SkipInterface
     });
 }
Exemplo n.º 3
0
        //Patches a method name for the exceptions (IndicesStats needs better unique names for all the url endpoints)
        //or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
        public void PatchMethod(CsharpMethod method)
        {
            if (method == null)
            {
                return;
            }

            Func <string, bool> ms = s => method.FullName.StartsWith(s);
            Func <string, bool> pc = s => method.Path.Contains(s);

            if (ms("Indices") && !pc("{index}"))
            {
                method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");
            }

            if (ms("Nodes") && !pc("{node_id}"))
            {
                method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");
            }

            //remove duplicate occurance of the HTTP method name
            var m = method.HttpMethod.ToPascalCase();

            method.FullName =
                Regex.Replace(method.FullName, m, a => a.Index != method.FullName.IndexOf(m, StringComparison.Ordinal) ? "" : m);

            method = GetOverrides()?.PatchMethod(method) ?? method;

            var key = method.QueryStringParamName.Replace("RequestParameters", "");

            if (CodeConfiguration.ApiNameMapping.TryGetValue(RestSpecName, out var mapsApiMethodName))
            {
                method.QueryStringParamName = mapsApiMethodName + "RequestParameters";
            }
            else if (CodeConfiguration.MethodNameOverrides.TryGetValue(key, out var manualOverride))
            {
                method.QueryStringParamName = manualOverride + "RequestParameters";
            }

            method.DescriptorType = method.QueryStringParamName.Replace("RequestParameters", "Descriptor");
            method.RequestType    = method.QueryStringParamName.Replace("RequestParameters", "Request");
            if (CodeConfiguration.KnownRequests.TryGetValue("I" + method.RequestType, out var requestGeneric))
            {
                method.RequestTypeGeneric = requestGeneric;
            }
            else
            {
                method.RequestTypeUnmapped = true;
            }

            if (CodeConfiguration.KnownDescriptors.TryGetValue(method.DescriptorType, out var generic))
            {
                method.DescriptorTypeGeneric = generic;
            }
            else
            {
                method.Unmapped = true;
            }
        }
Exemplo n.º 4
0
        public static IEnumerable <Constructor> DescriptorConstructors(CsharpMethod method)
        {
            var m               = method.DescriptorType;
            var generic         = method.DescriptorTypeGeneric?.Replace("<", "").Replace(">", "").Split(",").First().Trim();
            var generateGeneric = !string.IsNullOrEmpty(method.DescriptorTypeGeneric);

            return(GenerateConstructors(method, true, generateGeneric, m, generic));
        }
Exemplo n.º 5
0
		//Patches a method name for the exceptions (IndicesStats needs better unique names for all the url endpoints)
		//or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
		public static void PatchMethod(CsharpMethod method)
		{
			Func<string, bool> ms = s => method.FullName.StartsWith(s);
			Func<string, bool> pc = s => method.Path.Contains(s);

			if (ms("Indices") && !pc("{index}"))
				method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");

			if (ms("Nodes") && !pc("{node_id}"))
				method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");

			//remove duplicate occurance of the HTTP method name
			var m = method.HttpMethod.ToPascalCase();
			method.FullName =
				Regex.Replace(method.FullName, m, a => a.Index != method.FullName.IndexOf(m, StringComparison.Ordinal) ? "" : m);

			foreach (var param in GlobalQueryParameters.Parameters)
			{
				if (!method.Url.Params.ContainsKey(param.Key))
					method.Url.Params.Add(param.Key, param.Value);
			}

			string manualOverride;
			var key = method.QueryStringParamName.Replace("RequestParameters", "");
			if (CodeConfiguration.MethodNameOverrides.TryGetValue(key, out manualOverride))
				method.QueryStringParamName = manualOverride + "RequestParameters";

			method.DescriptorType = method.QueryStringParamName.Replace("RequestParameters", "Descriptor");
			method.RequestType = method.QueryStringParamName.Replace("RequestParameters", "Request");
			string requestGeneric;
			if (CodeConfiguration.KnownRequests.TryGetValue("I" + method.RequestType, out requestGeneric))
				method.RequestTypeGeneric = requestGeneric;
			else method.RequestTypeUnmapped = true;

			method.Allow404 = ApiEndpointsThatAllow404.Endpoints.Contains(method.DescriptorType.Replace("Descriptor", ""));

			string generic;
			if (CodeConfiguration.KnownDescriptors.TryGetValue(method.DescriptorType, out generic))
				method.DescriptorTypeGeneric = generic;
			else method.Unmapped = true;

			try
			{
				IEnumerable<string> skipList = new List<string>();
				IDictionary<string, string> queryStringParamsRenameList = new Dictionary<string, string>();

				var typeName = "ApiGenerator.Overrides.Descriptors." + method.DescriptorType + "Overrides";
				var type = CodeConfiguration.Assembly.GetType(typeName);
				if (type != null)
				{
					var overrides = Activator.CreateInstance(type) as IDescriptorOverrides;
					if (overrides != null)
					{
						skipList = overrides.SkipQueryStringParams ?? skipList;
						queryStringParamsRenameList = overrides.RenameQueryStringParams ?? queryStringParamsRenameList;
						method = overrides.PatchMethod(method);
					}
				}

				var globalQueryStringRenames = new Dictionary<string, string>
				{
					{"_source", "source_enabled"},
					{"_source_include", "source_include"},
					{"_source_exclude", "source_exclude"},
					{"q", "query_on_query_string"},
				};

				foreach (var kv in globalQueryStringRenames)
					if (!queryStringParamsRenameList.ContainsKey(kv.Key))
						queryStringParamsRenameList[kv.Key] = kv.Value;

				var patchedParams = new Dictionary<string, ApiQueryParameters>();
				foreach (var kv in method.Url.Params)
				{
					if (kv.Value.OriginalQueryStringParamName.IsNullOrEmpty())
						kv.Value.OriginalQueryStringParamName = kv.Key;
					if (skipList.Contains(kv.Key))
						continue;

					string newName;
					if (!queryStringParamsRenameList.TryGetValue(kv.Key, out newName))
					{
						patchedParams.Add(kv.Key, kv.Value);
						continue;
					}

					patchedParams.Add(newName, kv.Value);
				}

				method.Url.Params = patchedParams;
			}
			// ReSharper disable once EmptyGeneralCatchClause
			catch
			{
			}

		}
Exemplo n.º 6
0
        //Patches a method name for the exceptions (IndicesStats needs better unique names for all the url endpoints)
        //or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
        public void PatchMethod(CsharpMethod method)
        {
            Func <string, bool> ms = s => method.FullName.StartsWith(s);
            Func <string, bool> pc = s => method.Path.Contains(s);

            if (ms("Indices") && !pc("{index}"))
            {
                method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");
            }

            if (ms("Nodes") && !pc("{node_id}"))
            {
                method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");
            }

            //remove duplicate occurance of the HTTP method name
            var m = method.HttpMethod.ToPascalCase();

            method.FullName =
                Regex.Replace(method.FullName, m, a => a.Index != method.FullName.IndexOf(m, StringComparison.Ordinal) ? "" : m);

            foreach (var param in RestApiSpec.CommonApiQueryParameters)
            {
                if (!method.Url.Params.ContainsKey(param.Key))
                {
                    method.Url.Params.Add(param.Key, param.Value);
                }
            }
            if (this.ObsoleteQueryParameters != null)
            {
                foreach (var param in this.ObsoleteQueryParameters)
                {
                    if (!method.Url.Params.ContainsKey(param.Key))
                    {
                        method.Url.Params.Add(param.Key, param.Value);
                    }
                }
            }

            string manualOverride;
            var    key = method.QueryStringParamName.Replace("RequestParameters", "");

            if (CodeConfiguration.MethodNameOverrides.TryGetValue(key, out manualOverride))
            {
                method.QueryStringParamName = manualOverride + "RequestParameters";
            }

            method.DescriptorType = method.QueryStringParamName.Replace("RequestParameters", "Descriptor");
            method.RequestType    = method.QueryStringParamName.Replace("RequestParameters", "Request");
            string requestGeneric;

            if (CodeConfiguration.KnownRequests.TryGetValue("I" + method.RequestType, out requestGeneric))
            {
                method.RequestTypeGeneric = requestGeneric;
            }
            else
            {
                method.RequestTypeUnmapped = true;
            }

            method.Allow404 = ApiEndpointsThatAllow404.Endpoints.Contains(method.DescriptorType.Replace("Descriptor", ""));

            string generic;

            if (CodeConfiguration.KnownDescriptors.TryGetValue(method.DescriptorType, out generic))
            {
                method.DescriptorTypeGeneric = generic;
            }
            else
            {
                method.Unmapped = true;
            }

            try
            {
                IEnumerable <string>         skipList = new List <string>();
                IDictionary <string, string> queryStringParamsRenameList = new Dictionary <string, string>();

                var typeName = "ApiGenerator.Overrides.Descriptors." + method.DescriptorType + "Overrides";
                var type     = CodeConfiguration.Assembly.GetType(typeName);
                if (type != null)
                {
                    var overrides = Activator.CreateInstance(type) as IDescriptorOverrides;
                    if (overrides != null)
                    {
                        skipList = overrides.SkipQueryStringParams ?? skipList;
                        queryStringParamsRenameList = overrides.RenameQueryStringParams ?? queryStringParamsRenameList;
                        method = overrides.PatchMethod(method);
                    }
                }

                var globalQueryStringRenames = new Dictionary <string, string>
                {
                    { "_source", "source_enabled" },
                    { "_source_include", "source_include" },
                    { "_source_exclude", "source_exclude" },
                    { "q", "query_on_query_string" },
                };

                foreach (var kv in globalQueryStringRenames)
                {
                    if (!queryStringParamsRenameList.ContainsKey(kv.Key))
                    {
                        queryStringParamsRenameList[kv.Key] = kv.Value;
                    }
                }

                var patchedParams = new Dictionary <string, ApiQueryParameters>();
                foreach (var kv in method.Url.Params)
                {
                    if (kv.Value.OriginalQueryStringParamName.IsNullOrEmpty())
                    {
                        kv.Value.OriginalQueryStringParamName = kv.Key;
                    }
                    if (skipList.Contains(kv.Key))
                    {
                        continue;
                    }

                    string newName;
                    if (!queryStringParamsRenameList.TryGetValue(kv.Key, out newName))
                    {
                        patchedParams.Add(kv.Key, kv.Value);
                        continue;
                    }

                    patchedParams.Add(newName, kv.Value);
                }

                method.Url.Params = patchedParams;
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch
            {
            }
        }
		public override CsharpMethod PatchMethod(CsharpMethod method)
		{
			var part = method.Parts.First(p => p.Name == "name");
			part.ClrTypeNameOverride = "Names";
			return method;
		}
		public virtual CsharpMethod PatchMethod(CsharpMethod method)
		{
			return method;
		}
		public override CsharpMethod PatchMethod(CsharpMethod method)
		{
			var part = method.Parts.First(p => p.Name == "index");
			part.Required = true;
			return method;
		}
Exemplo n.º 10
0
        private static IEnumerable <Constructor> GenerateConstructors(
            CsharpMethod method,
            bool inheritsFromPlainRequestBase,
            bool generateGeneric,
            string typeName, string generic
            )
        {
            var url   = method.Url;
            var ctors = new List <Constructor>();

            var paths = url.ExposedApiPaths.ToList();

            if (url.IsPartless)
            {
                return(ctors);
            }

            ctors.AddRange(from path in paths
                           let baseArgs = inheritsFromPlainRequestBase ? path.RequestBaseArguments : path.TypedSubClassBaseArguments
                                          let constParams = path.ConstructorArguments
                                                            let generated = $"public {typeName}({constParams}) : base({baseArgs})"
                                                                            select new Constructor
            {
                Parameterless = string.IsNullOrEmpty(constParams),
                Generated     = generated,
                Description   = path.GetXmlDocs(Indent),
                //Body = isDocumentApi ? $" => Q(\"routing\", new Routing(() => AutoRouteDocument()));" : string.Empty
                Body = string.Empty
            });

            if (generateGeneric && !string.IsNullOrWhiteSpace(generic))
            {
                ctors.AddRange(from path in paths.Where(path => path.HasResolvableArguments)
                               let baseArgs = path.AutoResolveBaseArguments(generic)
                                              let constructorArgs = path.AutoResolveConstructorArguments
                                                                    let baseOrThis = inheritsFromPlainRequestBase ? "this" : "base"
                                                                                     let generated = $"public {typeName}({constructorArgs}) : {baseOrThis}({baseArgs})"
                                                                                                     select new Constructor
                {
                    Parameterless = string.IsNullOrEmpty(constructorArgs),
                    Generated     = generated,
                    Description   = path.GetXmlDocs(Indent, skipResolvable: true),
                    Body          = string.Empty
                });

                if (url.TryGetDocumentApiPath(out var docPath))
                {
                    var docPathBaseArgs  = docPath.DocumentPathBaseArgument(generic);
                    var docPathConstArgs = docPath.DocumentPathConstructorArgument(generic);
                    ctors.Add(new Constructor
                    {
                        Parameterless  = string.IsNullOrEmpty(docPathConstArgs),
                        Generated      = $"public {typeName}({docPathConstArgs}) : this({docPathBaseArgs})",
                        AdditionalCode = $"partial void DocumentFromPath({generic} document);",
                        Description    = docPath.GetXmlDocs(Indent, skipResolvable: true, documentConstructor: true),
                        Body           = "=> DocumentFromPath(documentWithId);"
                    });
                }
            }
            var constructors = ctors.GroupBy(c => c.Generated.Split(new[] { ':' }, 2)[0]).Select(g => g.Last()).ToList();

            if (!constructors.Any(c => c.Parameterless))
            {
                constructors.Add(new Constructor
                {
                    Parameterless = true,
                    Generated     = $"internal {typeName}() : base()",
                    Description   =
                        $"///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>{Indent}[SerializationConstructor]",
                });
            }
            return(constructors);
        }
Exemplo n.º 11
0
        public static IEnumerable <Constructor> RequestConstructors(CsharpMethod method, bool inheritsFromPlainRequestBase)
        {
            var generic = method.RequestTypeGeneric?.Replace("<", "").Replace(">", "").Split(",").First().Trim();

            return(GenerateConstructors(method, inheritsFromPlainRequestBase, !inheritsFromPlainRequestBase, method.RequestType, generic));
        }