You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 14:21:19