gpt4 book ai didi

asp.net - 使用 Swashbuckle 自定义生成模型文档

转载 作者:行者123 更新时间:2023-12-01 13:18:55 25 4
gpt4 key购买 nike

我正在使用 Swashbuckle 为我正在编写的 ASP.NET 核心 API 生成 swagger 文档。在我的 API 中,我使用了很多自定义 Json 转换器,因此我接收和返回的 json 看起来并不完全像 C# 类的定义方式。例如,我可能有一个这样的类:

public class MyModel
{
private MyClass complicatedField;
}

这将被序列化为
{
"complicatedField": "String representation of the object"
}

但是,Swashbuckle 将其记录为
{
"complicatedField": {/*Json object of all the fields MyClass has*/}
}

我如何告诉 Swashbuckle 我的模型是如何序列化和反序列化的?

最佳答案

您可以使用 MapType因为您正在将类型更改为原始类型。否则你会使用 SchemaFilter .

.Net 框架 在启动时的某个地方调用扩展方法,即。程序.cs

httpConfiguration.EnableSwagger(c =>
{
c.MapType<MyClass>(() => new Schema { type = "string" }); // add additional schema info here
// other configs
});

.Net核心放置在startup.cs中的配置服务方法中
services.AddSwaggerGen(c =>
{
c.MapType<MyClass>(() => new Schema { Type = "string" });// add additional schema info here
// other configs
});

关于asp.net - 使用 Swashbuckle 自定义生成模型文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51728999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com