自定义OpenApiResponseWithBody组合属性实现失败,求解决方案
自定义OpenApi响应属性在Azure函数应用中失效问题
我需要在Azure函数应用中创建一个自定义属性,将重复的OpenApiResponseWithBody属性整合为一个,避免每个函数都重复添加如下属性标记:
[OpenApiResponseWithBody(HttpStatusCode.OK, "application/json", typeof(SampleResponseDto))] [OpenApiResponseWithBody(HttpStatusCode.InternalServerError, "application/json", typeof(SampleErrorResponseDto))] [OpenApiResponseWithBody(HttpStatusCode.Unauthorized, "application/json", typeof(ErrorResponseDto))] [OpenApiResponseWithBody(HttpStatusCode.Forbidden, "application/json", typeof(ErrorResponseDto))]
我期望用一个自定义属性(比如OpenApiDefaultsAttribute)替代这些冗余代码,示例实现如下:
public class OpenApiDefaultsAttribute : Attribute, IMetadataAttribute { public Attribute[] Process() { var attributes = new Attribute[] { new OpenApiResponseWithBodyAttribute(HttpStatusCode.Unauthorized, "application/json", typeof(ErrorResponseDto)), new OpenApiResponseWithBodyAttribute(HttpStatusCode.Forbidden, "application/json", typeof(ErrorResponseDto)) }; return attributes; } }
但按照合并属性的方式实现后,这个自定义属性在函数应用场景下并未生效,特此寻求解决办法。
内容的提问来源于stack exchange,提问作者Sahil Mishra
相关产品推荐
相关产品推荐

