Hello,
I’m using OpenApi feature and trying the json output into the swagger editor (https://editor.swagger.io/).
I got the same DTO for GET, POST, PUT and DELETE. ServiceStack only allows me to set the required attribute on a property but it’s not HTTP verb specific …
I noticed that in OpenApi specs you set Required parameter by operation (verb + dto). I created a HttpRequired attribute to achieve the purpose. Through the OpenApiFeature config, I’m able to add the open api Require operation attribute. It’s a bit of a hack because I don’t have the original type in OpenApi feature config callbacks …
Would you have another way to achieve this?
Thanks in advance,
Olivier
The attribute
public class HttpRequired : AttributeBase
{
public string Verbs { get; set; }
public HttpRequired(string verbs)
{
Verbs = verbs;
}
}
OpenApiFeature config
_plugin = new OpenApiFeature()
{
OperationFilter = (name, op) =>
{
var tag = op.Tags.FirstOrDefault(t => t.StartsWith("type:"));
if(tag == null)
return;
var type = Type.GetType("MyAssembly."+ tag.TrimStart("type:".ToCharArray()) + ",MyAssembly");
if(type == null)
return;
foreach (var openApiParameter in op.Parameters)
{
var property = type.GetProperty(openApiParameter.Name);
if(property == null)
return;
var verb = property.GetCustomAttribute<HttpRequired>();
if (verb == null) continue;
if (verb.Verbs.Replace(" ", "").ToUpper().Split(',').Contains(name))
openApiParameter.Required = true;
}
}
};
The dto
[Tag("type:" + nameof(MyDTO))]
public class MyDTO: IBelonger
{
[HttpRequired("GET")]
public Guid Id { get; set; }
}