forked from loafle/openapi-generator-original
Added support for controller action parameter validation into the ValidateModelStateAttribute (#7156)
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace IO.Swagger.Attributes
|
||||
{
|
||||
@@ -14,10 +18,44 @@ namespace IO.Swagger.Attributes
|
||||
/// <param name="context"></param>
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
// Per https://blog.markvincze.com/how-to-validate-action-parameters-with-dataannotation-attributes/
|
||||
var descriptor = context.ActionDescriptor as ControllerActionDescriptor;
|
||||
if (descriptor != null)
|
||||
{
|
||||
foreach (var parameter in descriptor.MethodInfo.GetParameters())
|
||||
{
|
||||
object args = null;
|
||||
if (context.ActionArguments.ContainsKey(parameter.Name))
|
||||
{
|
||||
args = context.ActionArguments[parameter.Name];
|
||||
}
|
||||
|
||||
ValidateAttributes(parameter, args, context.ModelState);
|
||||
}
|
||||
}
|
||||
|
||||
if (!context.ModelState.IsValid)
|
||||
{
|
||||
context.Result = new BadRequestObjectResult(context.ModelState);
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateAttributes(ParameterInfo parameter, object args, ModelStateDictionary modelState)
|
||||
{
|
||||
foreach (var attributeData in parameter.CustomAttributes)
|
||||
{
|
||||
var attributeInstance = parameter.GetCustomAttribute(attributeData.AttributeType);
|
||||
|
||||
var validationAttribute = attributeInstance as ValidationAttribute;
|
||||
if (validationAttribute != null)
|
||||
{
|
||||
var isValid = validationAttribute.IsValid(args);
|
||||
if (!isValid)
|
||||
{
|
||||
modelState.AddModelError(parameter.Name, validationAttribute.FormatErrorMessage(parameter.Name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ namespace IO.Swagger.Filters
|
||||
/// <summary>
|
||||
/// BasePath Document Filter sets BasePath property of Swagger and removes it from the individual URL paths
|
||||
/// </summary>
|
||||
public class BasePathDocumentFilter : IDocumentFilter
|
||||
public class BasePathFilter : IDocumentFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="basePath">BasePath to remove from Operations</param>
|
||||
public BasePathDocumentFilter(string basePath)
|
||||
public BasePathFilter(string basePath)
|
||||
{
|
||||
BasePath = basePath;
|
||||
}
|
||||
@@ -7,9 +7,9 @@ using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
namespace IO.Swagger.Filters
|
||||
{
|
||||
/// <summary>
|
||||
/// Path Parameter Validation Filter
|
||||
/// Path Parameter Validation Rules Filter
|
||||
/// </summary>
|
||||
public class PathParameterValidationFilter : IOperationFilter
|
||||
public class GeneratePathParamsValidationFilter : IOperationFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
@@ -80,9 +80,11 @@ namespace IO.Swagger
|
||||
c.DescribeAllEnumsAsStrings();
|
||||
c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{_hostingEnv.ApplicationName}.xml");
|
||||
// Sets the basePath property in the Swagger document generated
|
||||
c.DocumentFilter<BasePathDocumentFilter>("/v2");
|
||||
// Do validation of path parameters w. DataAnnotation attributes
|
||||
c.OperationFilter<PathParameterValidationFilter>();
|
||||
c.DocumentFilter<BasePathFilter>("/v2");
|
||||
|
||||
// Include DataAnnotation attributes on Controller Action parameters as Swagger validation rules (e.g required, pattern, ..)
|
||||
// Use [ValidateModelState] on Actions to actually validate it in C# as well!
|
||||
c.OperationFilter<GeneratePathParamsValidationFilter>();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user