The basic idea how it can be done is to use OperationFilter
. As second parameter it consumes OpenApiOperation
which has RequestType
property. You can find your type by this RequestType
and find all properties which has [MaxLength]
attribute via reflection. Then iterate through Parameters
of operation and set MaxLength
property for all parameters which has this property.
new OpenApiFeature{
OperationFilter = (verb, op) => {
string requestTypeName = op.RequestType;
//get type by its string name (you can use any other approach, for example holding types in dictionary)
Assembly asm = typeof(Project.ServiceModel).Assembly;
Type type = asm.GetType(requestTypeName);
//find properties with [MaxLength] Attribute
var properties = ....
//set max length in Open API parameters
foreach(var prop in properties)
{
var attrValue = ... //get attribute value from property
op.Parameters.FirstOrDefault(p => p.Name == prop.Name)?.MaxLength = attrValue;
}
}
};