ApplyCustomSchemaFilters.cs 3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace MEU.API
{
    public class ApplyCustomSchemaFilters : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
    {
       

        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {

            if (schema?.Properties == null)
            {
                return; 
            }

            Dictionary<string, string> dcValue = new Dictionary<string, string>();
            foreach (var property in context.Type.GetProperties())
            {
                if (property.Name.StartsWith("BODY_PART"))
                {
                    //"System.Collections.Generic.List`1[[MEU.API.Models.working_shift_time"
                    string fullName = property.PropertyType.FullName.Split(',')[0];
                    string name = "";
                    //"MEU.API.Models.working_shift"
                    if (fullName.Contains("System.Collections.Generic.List"))
                    {
                        string[] names = fullName.Split('.');
                        name = "list_" + names.LastOrDefault().Replace("\"", "");
                    }
                    else
                    {
                        string[] names = fullName.Split('.');
                        name = names.LastOrDefault().Replace("\"", "");
                    }
                    dcValue.Add(property.Name.ToUpper(), name);
                }
              
            }

            List<KeyValuePair<string, OpenApiSchema>> lstRemove = new List<KeyValuePair<string, OpenApiSchema>>();
            List<KeyValuePair<string, OpenApiSchema>> lstAdd = new List<KeyValuePair<string, OpenApiSchema>>();
            foreach (KeyValuePair<string, OpenApiSchema> item in schema.Properties)
            {
                if (item.Key.ToUpper().StartsWith("BODY_PART"))
                {
                    if (dcValue.ContainsKey(item.Key.ToUpper()))
                    {
                        KeyValuePair<string, OpenApiSchema> ab = new KeyValuePair<string, OpenApiSchema>(dcValue[item.Key.ToUpper()], item.Value);
                        lstRemove.Add(item);
                        lstAdd.Insert(0,ab);
                    }
                    
                }
            }

            foreach (var item in lstRemove)
            {
                schema.Properties.Remove(item);
            }
            foreach (var item in lstAdd)
            {
                schema.Properties.Add(item);
            }

            var excludedProperties = context.Type.GetProperties().Where(c=>c.GetMethod.IsVirtual).ToArray();
            foreach (PropertyInfo excludedProperty in excludedProperties)
            {
                if (schema.Properties.ContainsKey(excludedProperty.Name))
                {
                    schema.Properties.Remove(excludedProperty.Name);
                }
              
            }
        }
    }
}