using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using Kreta.Ellenorzo.Dto.VN; using Kreta.Ellenorzo.Dto.VN.Adatszotar; using Kreta.Ellenorzo.Dto.VN.Documentation; using Swashbuckle.Examples; namespace Kreta.Ellenorzo.WebApi.VN.Documentation { /// /// Author: Kovács Kornél (DevKornél), Madách Ferenc Created On: 2019.06. /// public class DummyEnumExampleProvider : IExamplesProvider { #pragma warning disable S3253 // Constructor and destructor declarations should not be redundant public DummyEnumExampleProvider() { } #pragma warning restore S3253 // Constructor and destructor declarations should not be redundant public object GetExamples() { IEnumerable methodInfos = Assembly.GetExecutingAssembly().GetTypes() .Where(x => x.Namespace == Constant.WebApiDefaultNamespace) .SelectMany(y => y.GetMethods()); IEnumerable returnTypeNames = methodInfos.Where(x => x.ReturnType.AssemblyQualifiedName != null && Regex.IsMatch(x.ReturnType.AssemblyQualifiedName, @"(Kreta.*)\=null")) .Select(x => Regex.Match(x.ReturnType.AssemblyQualifiedName, @"(Kreta.*)\=null").Value); IEnumerable requestParameterNames = methodInfos.Select(x => x.GetParameters()) .SelectMany(x => x.Select(y => y.ParameterType.AssemblyQualifiedName) .Where(z => z != null && Regex.IsMatch(z, $@"\w+{Constant.ResponseSuffix}"))); var osztalyokNestedNelkul = returnTypeNames.Union(requestParameterNames).Select(x => Type.GetType(x)).ToList(); var osztalyok = new List(); foreach (Type item in osztalyokNestedNelkul) { GetNestedDtoTypes(osztalyok, item); } return ConvertErtekByEnumToDictionary(osztalyokNestedNelkul.Union(osztalyok)); } private static Dictionary ConvertErtekByEnumToDictionary(IEnumerable osztalyok) { var enumokEsErtekeik = new Dictionary(); IEnumerable<(string TypeName, string TypeQualifiedName)> query = from osztaly in osztalyok.Distinct() from property in osztaly.GetProperties() where property.PropertyType.Name == typeof(AdatszotarResponseDto<>).Name let propertyGenericArgument = property.PropertyType.GenericTypeArguments[0] select ( TypeName: propertyGenericArgument.FullName, TypeQualifiedName: propertyGenericArgument.AssemblyQualifiedName); foreach ((string typeName, string typeQualifiedName) in query.ToList()) { string[] typeNameStringArray = typeName.Split('.'); if (!enumokEsErtekeik.ContainsKey(typeNameStringArray[typeNameStringArray.Length - 1])) { var enumErtekek = Type.GetType(typeQualifiedName).GetEnumValues().OfType().ToList(); var enumErtekekIndexekkel = new List<(int, Enum)>(); foreach (var ertek in enumErtekek) { enumErtekekIndexekkel.Add((Convert.ToInt32(ertek), ertek)); } enumokEsErtekeik.Add(typeNameStringArray[typeNameStringArray.Length - 1], string.Join(",", enumErtekekIndexekkel)); } } return enumokEsErtekeik; } private void GetNestedDtoTypes(List osztalyok, Type type) { foreach (PropertyInfo property in type.GetProperties().Where(property => property.PropertyType.FullName.Contains(Constant.ResponseSuffix) && !property.PropertyType.FullName.Contains(typeof(DocumentationExampleDto).Name)).Select(property => property)) { osztalyok.Add(GetNestedType(property)); GetNestedDtoTypes(osztalyok, GetNestedType(property)); } } private static Type GetNestedType(PropertyInfo property) => property.PropertyType.FullName.Contains(typeof(AdatszotarResponseDto<>).Name) ? Type.GetType(Regex.Match(property.PropertyType.FullName, @"\[(Kreta.*)\=null").Value.Remove(0, 1)) : Type.GetType(property.PropertyType.AssemblyQualifiedName); } }