public ApiParamDescription GetApiMethodParamFullDescription(string methodName, string paramName) { var result = new ApiParamDescription(); var methodInfo = typeof(T).GetMethod(methodName); var defaultName = "some"; if (methodInfo == null) { result.ParamDescription = new CommonDescription { Name = defaultName }; return(result); } var parameters = methodInfo.GetParameters() .Where(p => p.Name == paramName); if (parameters.Count() == 0) { result.ParamDescription = new CommonDescription(paramName); return(result); } var paramAttributesAndResult = GetAndCheckParamAttributes(parameters, result); var paramAttributes = paramAttributesAndResult.Item1; result = paramAttributesAndResult.Item2; result = SetParamDescription(paramAttributes, paramName, result); result = SetMaxAndMinValue(paramAttributes, result); return(result); }
public ApiParamDescription GetApiMethodParamFullDescription(string methodName, string paramName) { var result = new ApiParamDescription(); result.ParamDescription = new CommonDescription(paramName); var met = type.GetMethod(methodName); if (met != null && met.GetCustomAttributes(true).OfType <ApiMethodAttribute>().Any()) { var parameter = met.GetParameters().Where(param => param.Name == paramName); if (parameter.Any()) { var descriptionAttribute = parameter.First().GetCustomAttributes(true).OfType <ApiDescriptionAttribute>().FirstOrDefault(); if (descriptionAttribute != null) { result.ParamDescription.Description = descriptionAttribute.Description; } var intValidationAttribute = parameter.First().GetCustomAttributes(true).OfType <ApiIntValidationAttribute>().FirstOrDefault(); if (intValidationAttribute != null) { result.MinValue = intValidationAttribute.MinValue; result.MaxValue = intValidationAttribute.MaxValue; } var requiredAttribute = parameter.First().GetCustomAttributes(true).OfType <ApiRequiredAttribute>().FirstOrDefault(); if (requiredAttribute != null) { result.Required = requiredAttribute.Required; } } } return(result); }
private static void AssertParamDescriptionAreEquals(ApiParamDescription expected, ApiParamDescription actual) { Assert.AreEqual(expected?.MaxValue, actual?.MaxValue); Assert.AreEqual(expected?.MinValue, actual?.MinValue); Assert.AreEqual(expected?.Required, actual?.Required); AssertCommonDescriptionAreEquals(expected?.ParamDescription, actual?.ParamDescription); }
FillInApiParamDescription(ParameterInfo parameterInfo, ApiParamDescription paramDescription) { paramDescription.ParamDescription.Description = parameterInfo.GetCustomAttribute <ApiDescriptionAttribute>()?.Description; paramDescription.Required = parameterInfo.GetCustomAttribute <ApiRequiredAttribute>() != null && parameterInfo.GetCustomAttribute <ApiRequiredAttribute>().Required; paramDescription.MaxValue = parameterInfo.GetCustomAttribute <ApiIntValidationAttribute>()?.MaxValue; paramDescription.MinValue = parameterInfo.GetCustomAttribute <ApiIntValidationAttribute>()?.MinValue; }
public ApiParamDescription GetApiMethodParamFullDescription(string methodName, string paramName) { var paramWithName = GetApiMethodParamDesc(methodName, paramName); var result = new ApiParamDescription(); result.ParamDescription = new CommonDescription(paramName); if (paramWithName == null) { return(result); } FillInApiParamDescription(paramWithName, result); return(result); }
public ApiMethodDescription GetApiMethodFullDescription(string methodName) { var met = type.GetMethod(methodName); if (met == null || !met.GetCustomAttributes(true).OfType <ApiMethodAttribute>().Any()) { return(null); } var result = new ApiMethodDescription(); result.MethodDescription = new CommonDescription(methodName, GetApiMethodDescription(methodName)); result.ParamDescriptions = GetApiMethodParamNames(methodName).Select(param => GetApiMethodParamFullDescription(methodName, param)).ToArray(); var returnParameter = met.ReturnParameter; bool isNecessaryToSetReturnParameter = false; var returnParamDiscription = new ApiParamDescription(); returnParamDiscription.ParamDescription = new CommonDescription(); var descriptionAttribute = returnParameter.GetCustomAttributes(true).OfType <ApiDescriptionAttribute>().FirstOrDefault(); if (descriptionAttribute != null) { returnParamDiscription.ParamDescription.Description = descriptionAttribute.Description; isNecessaryToSetReturnParameter = true; } var intValidationAttribute = returnParameter.GetCustomAttributes(true).OfType <ApiIntValidationAttribute>().FirstOrDefault(); if (intValidationAttribute != null) { returnParamDiscription.MinValue = intValidationAttribute.MinValue; returnParamDiscription.MaxValue = intValidationAttribute.MaxValue; isNecessaryToSetReturnParameter = true; } var requiredAttribute = returnParameter.GetCustomAttributes(true).OfType <ApiRequiredAttribute>().FirstOrDefault(); if (requiredAttribute != null) { returnParamDiscription.Required = requiredAttribute.Required; isNecessaryToSetReturnParameter = true; } if (isNecessaryToSetReturnParameter) { result.ReturnDescription = returnParamDiscription; } return(result); }
public Tuple <IEnumerable <Attribute>, ApiParamDescription> GetAndCheckParamAttributes( IEnumerable <ParameterInfo> parameters, ApiParamDescription result) { var paramAttributes = parameters .First() .GetCustomAttributes(); if (paramAttributes.Contains(new ApiRequiredAttribute())) { result.Required = true; } else { result.Required = false; } return(Tuple.Create(paramAttributes, result)); }
public ApiParamDescription SetMaxAndMinValue( IEnumerable <Attribute> paramAttributes, ApiParamDescription result) { var intValidationAttributes = paramAttributes .OfType <ApiIntValidationAttribute>(); if (intValidationAttributes.Count() == 0) { result.MinValue = null; result.MaxValue = null; return(result); } var firstIntValidationAttribute = intValidationAttributes.First(); result.MaxValue = firstIntValidationAttribute.MaxValue; result.MinValue = firstIntValidationAttribute.MinValue; return(result); }
public ApiParamDescription GetApiMethodParamFullDescription(string methodName, string paramName) { var paramInfo = GetApiMethodParameterInfo(methodName, paramName); var description = new ApiParamDescription { ParamDescription = new CommonDescription { Name = paramInfo == null ? paramName : paramInfo.Name } }; if (paramInfo == null) { return(description); } var attrs = paramInfo.GetCustomAttributes(); foreach (var attr in attrs) { switch (attr) { case ApiIntValidationAttribute attribute: description.MinValue = attribute.MinValue; description.MaxValue = attribute.MaxValue; break; case ApiRequiredAttribute attribute: description.Required = attribute.Required; break; case ApiDescriptionAttribute attribute: description.ParamDescription.Description = attribute.Description; break; } } return(description); }
public ApiParamDescription SetParamDescription( IEnumerable <Attribute> paramAttributes, string paramName, ApiParamDescription result) { var paramAttributesDescription = paramAttributes .OfType <ApiDescriptionAttribute>(); string paramDescription; if (paramAttributesDescription.Count() != 0) { paramDescription = paramAttributesDescription .First() .Description; } else { paramDescription = null; } result.ParamDescription = new CommonDescription(paramName, paramDescription); return(result); }
public ApiParamDescription GetApiMethodParamFullDescription(string methodName, string paramName) { var method = typeof(T).GetMethod(methodName); var param = method ?.GetParameters() .FirstOrDefault(p => p.Name == paramName); var pd = new ApiParamDescription(); pd.ParamDescription = new CommonDescription(method == null? "some" : paramName, param? .GetCustomAttributes() .OfType <ApiDescriptionAttribute>() .FirstOrDefault() ?.Description); var required = param?.GetCustomAttributes().OfType <ApiRequiredAttribute>().FirstOrDefault(); pd.Required = required != null && required.Required; var validation = param?.GetCustomAttributes().OfType <ApiIntValidationAttribute>().FirstOrDefault(); pd.MaxValue = validation?.MaxValue; pd.MinValue = validation?.MinValue; return(pd); }
private ApiParamDescription GetApiMethodReturnParameter(string methodName) { var returnParameter = typeof(T) .GetMethod(methodName)? .ReturnParameter; if (returnParameter?.Name == null) { return(null); } var atrIntValidation = returnParameter .GetCustomAttribute(typeof(ApiIntValidationAttribute)) as ApiIntValidationAttribute; var atrRequired = returnParameter .GetCustomAttribute(typeof(ApiRequiredAttribute)) as ApiRequiredAttribute; var parameterDescription = new ApiParamDescription { MaxValue = atrIntValidation?.MaxValue, MinValue = atrIntValidation?.MinValue, ParamDescription = new CommonDescription { Description = GetApiMethodParamDescription(methodName, returnParameter.Name), Name = returnParameter.Name } }; if (atrRequired != null) { parameterDescription.Required = atrRequired.Required; } parameterDescription.ParamDescription.Name = parameterDescription.ParamDescription.Name == "" ? null : parameterDescription.ParamDescription.Name; return(parameterDescription); }
private static void WriteParamDescription(ApiParamDescription description) { if (description == null) { return; } if (!string.IsNullOrWhiteSpace(description.ParamDescription?.Description)) { WriteWithColor($"\t\t{description.ParamDescription?.Description}", ConsoleColor.DarkGreen); } WriteWithColor($"\t\tRequired - {description.Required}", ConsoleColor.DarkGreen); if (description.MinValue != null) { WriteWithColor($"\t\tMin - {description.MinValue}", ConsoleColor.DarkGreen); } if (description.MaxValue != null) { WriteWithColor($"\t\tMax - {description.MaxValue}", ConsoleColor.DarkGreen); } }
private ApiParamDescription CreateParamDescription(ParameterInfo parametr, string methodName, string paramName) { var atrIntValidation = parametr .GetCustomAttribute(typeof(ApiIntValidationAttribute)) as ApiIntValidationAttribute; var atrRequired = parametr .GetCustomAttribute(typeof(ApiRequiredAttribute)) as ApiRequiredAttribute; var parametrDescription = new ApiParamDescription { MaxValue = atrIntValidation?.MaxValue, MinValue = atrIntValidation?.MinValue, ParamDescription = new CommonDescription { Description = GetApiMethodParamDescription(methodName, paramName), Name = paramName } }; if (atrRequired != null) { parametrDescription.Required = atrRequired.Required; } return(parametrDescription); }