public class Request {
[Required]
public string Id {get;set;}
}
When i set Required attribute in complex model that mapped from query, the Required is set to false in generated swagger doc.
I have made a pull request for adding a operation filter that handles the Required attribute (and other data annotations such as range, regular expression and so forth):
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/357
I am also missing the support for the required annotation on query string parameters as well. A custom IOperationFilter was a workaround, but not a nice one because I had to assume the correct ApiParameterDescription from the OperationFilterContext based on the parameter name from Operation within the Apply method.
A code snippet of what I've done for now is very simular to the PR suggestion of @rthunbo :
public void Apply(Operation operation, OperationFilterContext context)
{
foreach (var parameter in operation.Parameters)
{
if (!(parameter is NonBodyParameter))
continue;
if (!parameter.In.Equals("query", StringComparison.Ordinal))
continue;
var apiParameterDescription = context.ApiDescription.ParameterDescriptions.Single(p =>
p.Name == parameter.Name
&& p.ModelMetadata?.ContainerType != null
&& p.ModelMetadata?.PropertyName != null);
var metadata = apiParameterDescription.ModelMetadata;
var propertyInfo = metadata.ContainerType.GetTypeInfo().GetProperty(metadata.PropertyName);
if (propertyInfo == null)
continue;
var attributes = propertyInfo.GetCustomAttributes();
if (attributes.Any(a => a.GetType() == typeof(RequiredAttribute)))
parameter.Required = true;
}
}
Unfortunately, as soon as I need to decorate headers as required too, I could end up assuming the wrong attributes. This is because I could potentially have the same name for both the query string parameter as the header. If only one of them has the Required annotation, this will lead to unexpected behavior. I've also mentioned this within the PR in #357.
I don't mind to update the PR, but I'm not really sure what the proper way of fixing this would be. Maybe we could have a reference to the ApiParameterDescription within the Parameter that is returned by CreateParameter()?
How about BindRequired and BindingBehavior?
Care to explain, @astef? BindRequired does result in an invalid model state but the generated swagger still does not decorate the parameter as required. And that is exactly what I was trying to accomplish.
"Are they also should be processed the same as RequiredAttribute?" — I mean
...or instead of, may be?
BTW, @Erikvl87
Thanks for your solution. It works but I need to change a line with Single() to FirstOrDefault() and return if null, so it doesnt fail in my case
@domaindrivendev what's the status of this issue, any plans to support [Required] attribute on complex objects in GET request?
Should be supported out of the box as of v1.1.0
Most helpful comment
I have made a pull request for adding a operation filter that handles the Required attribute (and other data annotations such as range, regular expression and so forth):
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/357