public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, SchemaFilterContext schemaFilterContext)
    {
        if (schema.Properties.Count == 0)
        {
            return;
        }
        const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
        var memberList = schemaFilterContext.SystemType
                         .GetFields(bindingFlags).Cast <MemberInfo>()
                         .Concat(schemaFilterContext.SystemType
                                 .GetProperties(bindingFlags));
        var excludedList = memberList.Where(m =>
                                            m.GetCustomAttribute <SwaggerIgnoreAttribute>()
                                            != null)
                           .Select(m => {
            return(m.GetCustomAttribute <JsonPropertyAttribute>()
                   ?.PropertyName
                   ?? m.Name);
        });

        foreach (var excludedName in excludedList)
        {
            if (schema.Properties.ContainsKey(excludedName))
            {
                schema.Properties.Remove(excludedName);
            }
        }
    }
Beispiel #2
0
        private List <OpenApiOperationParam> GetBodyParam(Microsoft.OpenApi.Models.OpenApiSchema schema, string key, string parentName, List <ParameterTree> tree, int node, bool isReq = false)
        {
            var paramLst = new List <OpenApiOperationParam>();
            var param    = new OpenApiOperationParam()
            {
                Name        = key,
                Type        = schema.Type,
                Description = schema.Description,
                IsRequired  = isReq
            };

            if (schema.Enum != null && schema.Enum.Count > 0)
            {
                foreach (var val in schema.Enum)
                {
                    param.Values.Add((val as Microsoft.OpenApi.Any.OpenApiString).Value);
                }
            }

            if (!string.IsNullOrEmpty(key))
            {
                tree.Add(new ParameterTree()
                {
                    Name       = param.Name,
                    Type       = param.Type,
                    ObjectName = parentName[0] == '_' ? parentName.Substring(1) : parentName,
                    Node       = node,
                    Position   = "body",
                    Values     = param.Values
                });
            }

            if (schema.Properties != null && schema.Properties.Count > 0)
            {
                foreach (var prop in schema.Properties)
                {
                    param.Property.AddRange(GetBodyParam(prop.Value, prop.Key, string.Format("{0}_{1}", key, prop.Key), tree, node + 1, schema.Required.Any(x => x.Equals(prop.Key))));
                }
            }

            if (schema.Type == "array" && schema.Items != null)
            {
                //param.Type = schema.Items.Properties.Count == 0 ? "string" : "object"; // hard code

                if (schema.Items.Properties != null && schema.Items.Properties.Count > 0)
                {
                    foreach (var prop in schema.Items.Properties)
                    {
                        param.Property.AddRange(GetBodyParam(prop.Value, prop.Key, string.Format("{0}_{1}", key, prop.Key), tree, node + 1, schema.Required.Any(x => x.Equals(key))));
                    }
                }
                else
                {
                    param.Type  = schema.Type;
                    param.Type += " of " + schema.Items.Type;
                }
            }

            paramLst.Add(param);

            return(paramLst);
        }