Search code examples
c#asp.netswagger

How to mark a property as required in Swagger, without ASP.NET model validation?


I am creating a public API that uses multiple private APIs (can not be accessed from outside). Business validations have been written for the private APIs and I do not want to re-write them for the public API. But I do want the swagger documentation to be the same.

That is why I wonder if I can mark property as mandatory, without using the Required attribute of ASP.NET. But that the swagger documentation indicates that it is mandatory. Is this possible?


Solution

  • Thanks to Mohsin, I solved my problem. The following I came up with, I created an attribute called SwaggerRequired. This attribute can be placed on any model. The AddSwaggerRequiredSchemaFilter then ensures that the Swagger documentation is modified. See below the code I wrote for this

    A random model:

    public class Foo
    {
        [SwaggerRequired]
        public string FooBar{ get; set; }
    }
    

    The SwaggerRequiredAttribute:

    [AttributeUsage(AttributeTargets.Property)] 
    public class SwaggerRequiredAttribute : Attribute
    {
    }
    

    And the AddSwaggerRequiredSchemaFilter to get it working:

    public class AddSwaggerRequiredSchemaFilter : ISchemaFilter
    {
        public void Apply(Swashbuckle.Swagger.Schema schema, SchemaRegistry schemaRegistry, Type type)
        {
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                var attribute = property.GetCustomAttribute(typeof(SwaggerRequiredAttribute));
    
                if (attribute != null)
                {
                    var propertyNameInCamelCasing = char.ToLowerInvariant(property.Name[0]) + property.Name.Substring(1);
    
                    if (schema.required == null)
                    {
                        schema.required = new List<string>()
                        {
                            propertyNameInCamelCasing
                        };
                    }
                    else
                    {
                        schema.required.Add(propertyNameInCamelCasing);
                    }
                }
            }
        }
    }