第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何組合 Swashbuckle 過濾器?

如何組合 Swashbuckle 過濾器?

C#
30秒到達戰(zhàn)場 2022-12-24 10:27:29
我需要的是使用一些條件來隱藏或顯示模型中模型的某些屬性|Swagger UI 中響應(yīng)的示例值。這怎么可能實現(xiàn)呢?我的條件基于 api 操作的屬性和 DTO 的屬性。所以,fe,如果一個動作提供了一個屬性,那么我們應(yīng)該只在 Swagger UI 中看到標記的屬性。
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻1833條經(jīng)驗 獲得超4個贊

解決了。您只需要實施IOperationFilter并注冊它。這個東西允許您為同一模型顯示定制的不同示例。


數(shù)據(jù)傳輸協(xié)議


public class MyDTO

{

    public int Id { get; set; }


    [ShortModelMember]

    public string Name { get; set; }

    ...

}   

API控制器中的方法


[HttpGet]

[ReturnShortModel]

public MyDTO GetSmthg()

{

    return MyDTO.GetExample();

}   

Swagger的自定義操作過濾器


public class SwaggerExcludeFilter : IOperationFilter

{

    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)

    {

        if (!apiDescription.GetControllerAndActionAttributes<ReturnShortModelAttribute>().Any())

        {

            return;

        }


        var responseType = apiDescription.ResponseDescription.DeclaredType;

        var description = $"OK (uses a short model of {responseType})";

        var props = responseType

                    .GetProperties()

                    .Where(p => p.GetCustomAttributes(typeof(ShortModelMemberAttribute)).Any())

                    .ToDictionary(p => p.Name, p.PropertyType.Name);

        }


        operation.responses.Clear();

        operation.responses.Add("200", new Response

        {

            description = description,

            schema = new Schema

            {

                example = props,

            },

        });

    }

}   

最后


c.OperationFilter<SwaggerExcludeFilter>();   


查看完整回答
反對 回復(fù) 2022-12-24
  • 1 回答
  • 0 關(guān)注
  • 81 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號