/// <summary>
        /// Apply the filter.
        /// </summary>
        /// <param name="parameter"><see cref="OpenApiParameter"/>.</param>
        /// <param name="context"><see cref="ParameterFilterContext"/>.</param>
        public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
        {
            if (!this._applyFiler)
            {
                return;
            }

            var typeInfo = context.ParameterInfo?.ParameterType ?? context.PropertyInfo?.PropertyType;

            if (typeInfo == null)
            {
                return;
            }

            var enumsArray             = new OpenApiArray();
            var enumsDescriptionsArray = new OpenApiArray();

            if (typeInfo.IsEnum)
            {
                var names = Enum.GetNames(typeInfo).Select(name => new OpenApiString(name));
                enumsArray.AddRange(names);
                if (!parameter.Extensions.ContainsKey("x-enumNames") && enumsArray.Any())
                {
                    parameter.Extensions.Add("x-enumNames", enumsArray);
                }

                if (this._includeXEnumDescriptions)
                {
                    enumsDescriptionsArray.AddRange(EnumTypeExtensions.GetEnumValuesDescription(typeInfo, this._descriptionSources, this._xmlNavigators, this._includeXEnumRemarks));
                    if (!parameter.Extensions.ContainsKey("x-enumDescriptions") && enumsDescriptionsArray.Any())
                    {
                        parameter.Extensions.Add("x-enumDescriptions", enumsDescriptionsArray);
                    }
                }
            }
            else if (typeInfo.IsGenericType && !parameter.Extensions.ContainsKey("x-enumNames"))
            {
                foreach (var genericArgumentType in typeInfo.GetGenericArguments())
                {
                    if (genericArgumentType.IsEnum)
                    {
                        var names = Enum.GetNames(genericArgumentType).Select(name => new OpenApiString(name));
                        enumsArray.AddRange(names);
                        if (!parameter.Extensions.ContainsKey("x-enumNames") && enumsArray.Any())
                        {
                            parameter.Extensions.Add("x-enumNames", enumsArray);
                        }

                        if (this._includeXEnumDescriptions)
                        {
                            enumsDescriptionsArray.AddRange(EnumTypeExtensions.GetEnumValuesDescription(genericArgumentType, this._descriptionSources, this._xmlNavigators, this._includeXEnumRemarks));
                            if (!parameter.Extensions.ContainsKey("x-enumDescriptions") && enumsDescriptionsArray.Any())
                            {
                                parameter.Extensions.Add("x-enumDescriptions", enumsDescriptionsArray);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
        {
            var typeInfo   = context.ParameterInfo?.ParameterType ?? context.PropertyInfo.PropertyType;
            var enumsArray = new OpenApiArray();

            if (typeInfo.IsEnum)
            {
                var names = Enum.GetNames(typeInfo).Select(name => new OpenApiString(name));
                enumsArray.AddRange(names);
                if (!parameter.Extensions.ContainsKey("x-enumNames") && enumsArray.Any())
                {
                    parameter.Extensions.Add("x-enumNames", enumsArray);
                }
            }
            else if (typeInfo.IsGenericType && !parameter.Extensions.ContainsKey("x-enumNames"))
            {
                foreach (var genericArgumentType in typeInfo.GetGenericArguments())
                {
                    if (genericArgumentType.IsEnum)
                    {
                        var names = Enum.GetNames(genericArgumentType).Select(name => new OpenApiString(name));
                        enumsArray.AddRange(names);
                        if (!parameter.Extensions.ContainsKey("x-enumNames") && enumsArray.Any())
                        {
                            parameter.Extensions.Add("x-enumNames", enumsArray);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            var typeInfo   = context.Type.GetTypeInfo();
            var enumsArray = new OpenApiArray();

            if (typeInfo.IsEnum)
            {
                var names = Enum.GetNames(context.Type).Select(name => new OpenApiString(name)).ToList();
                enumsArray.AddRange(names);
                if (!schema.Extensions.ContainsKey("x-enumNames") && enumsArray.Any())
                {
                    schema.Extensions.Add("x-enumNames", enumsArray);
                }

                return;
            }

            // add "x-enumNames" for schema with generic types
            if (typeInfo.IsGenericType && !schema.Extensions.ContainsKey("x-enumNames"))
            {
                foreach (var genericArgumentType in typeInfo.GetGenericArguments())
                {
                    if (genericArgumentType.IsEnum)
                    {
                        if (schema.Properties?.Count > 0)
                        {
                            foreach (var schemaProperty in schema.Properties)
                            {
                                var schemaPropertyValue = schemaProperty.Value;
                                var propertySchema      = context.SchemaRepository.Schemas.FirstOrDefault(s => schemaPropertyValue.AllOf.FirstOrDefault(a => a.Reference.Id == s.Key) != null).Value;
                                if (propertySchema != null)
                                {
                                    var names = Enum.GetNames(genericArgumentType).Select(name => new OpenApiString(name));
                                    enumsArray.AddRange(names);
                                    if (!schemaPropertyValue.Extensions.ContainsKey("x-enumNames") && enumsArray.Any())
                                    {
                                        schemaPropertyValue.Extensions.Add("x-enumNames", enumsArray);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Apply the filter.
        /// </summary>
        /// <param name="schema"><see cref="OpenApiSchema"/>.</param>
        /// <param name="context"><see cref="SchemaFilterContext"/>.</param>
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (!this._applyFiler)
            {
                return;
            }

            var typeInfo               = context.Type.GetTypeInfo();
            var enumsArray             = new OpenApiArray();
            var enumsDescriptionsArray = new OpenApiArray();

            if (typeInfo.IsEnum)
            {
                var names = Enum.GetNames(context.Type).Select(name => new OpenApiString(name)).ToList();
                enumsArray.AddRange(names);
                if (!schema.Extensions.ContainsKey("x-enumNames") && enumsArray.Any())
                {
                    schema.Extensions.Add("x-enumNames", enumsArray);
                }

                if (this._includeXEnumDescriptions)
                {
                    enumsDescriptionsArray.AddRange(EnumTypeExtensions.GetEnumValuesDescription(context.Type, this._descriptionSources, this._xmlNavigators, this._includeXEnumRemarks));
                    if (!schema.Extensions.ContainsKey("x-enumDescriptions") && enumsDescriptionsArray.Any())
                    {
                        schema.Extensions.Add("x-enumDescriptions", enumsDescriptionsArray);
                    }
                }
                return;
            }

            // add "x-enumNames" for schema with generic types
            if (typeInfo.IsGenericType && !schema.Extensions.ContainsKey("x-enumNames"))
            {
                foreach (var genericArgumentType in typeInfo.GetGenericArguments())
                {
                    if (genericArgumentType.IsEnum)
                    {
                        if (schema.Properties?.Count > 0)
                        {
                            foreach (var schemaProperty in schema.Properties)
                            {
                                var schemaPropertyValue = schemaProperty.Value;
                                var propertySchema      = context.SchemaRepository.Schemas.FirstOrDefault(s => schemaPropertyValue.AllOf.FirstOrDefault(a => a.Reference.Id == s.Key) != null).Value;
                                if (propertySchema != null)
                                {
                                    var names = Enum.GetNames(genericArgumentType).Select(name => new OpenApiString(name));
                                    enumsArray.AddRange(names);
                                    if (!schemaPropertyValue.Extensions.ContainsKey("x-enumNames") && enumsArray.Any())
                                    {
                                        schemaPropertyValue.Extensions.Add("x-enumNames", enumsArray);
                                    }

                                    if (this._includeXEnumDescriptions)
                                    {
                                        enumsDescriptionsArray.AddRange(EnumTypeExtensions.GetEnumValuesDescription(genericArgumentType, this._descriptionSources, this._xmlNavigators, this._includeXEnumRemarks));
                                        if (!schemaPropertyValue.Extensions.ContainsKey("x-enumDescriptions") && enumsDescriptionsArray.Any())
                                        {
                                            schemaPropertyValue.Extensions.Add("x-enumDescriptions", enumsDescriptionsArray);
                                        }
                                    }

                                    var description = propertySchema.AddEnumValuesDescription(this._includeXEnumDescriptions);
                                    if (description != null)
                                    {
                                        if (schemaPropertyValue.Description == null)
                                        {
                                            schemaPropertyValue.Description = description;
                                        }
                                        else if (!schemaPropertyValue.Description.Contains(description))
                                        {
                                            schemaPropertyValue.Description += description;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (schema.Properties?.Count > 0)
            {
                foreach (var schemaProperty in schema.Properties)
                {
                    var schemaPropertyValue = schemaProperty.Value;
                    var propertySchema      = context.SchemaRepository.Schemas.FirstOrDefault(s => schemaPropertyValue.AllOf.FirstOrDefault(a => a.Reference.Id == s.Key) != null).Value;
                    var description         = propertySchema?.AddEnumValuesDescription(this._includeXEnumDescriptions);
                    if (description != null)
                    {
                        if (schemaPropertyValue.Description == null)
                        {
                            schemaPropertyValue.Description = description;
                        }
                        else if (!schemaPropertyValue.Description.Contains(description))
                        {
                            schemaPropertyValue.Description += description;
                        }
                    }
                }
            }
        }