Swashbuckle 将 Type 替换为其他 Type

Swashbuckle replace Type with other Type

我的 asp.net-core web-api 项目中有这个控制器:

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    [HttpGet]
    public ActionResult<List<ActualModel>> Get()
    {
        return new List<ActualModel>()
        {
            new ActualModel { StringValue1 = "SomeString" },
            new ActualModel { StringValue1 = "MoreString" }
        };
    }
}

此控制器中的唯一方法 return 是 "ActualModel" 的列表。它的定义如下所示:

public class ActualModel
{
    public string StringValue1 { get; set; }
}

而我的 swashbuckle 生成的 Swagger-UI 显示如下:

但我希望 Swashbuckle 生成 Swagger-Specification,以便它不包含 "ActualModel" 而是包含以下模型:

public class OtherModel
{
    public string StringValue2 { get; set; }
    public int IntValue1 { get; set; }
}

我注意到我认为我可以使用一个函数:

services.AddSwaggerGen(c =>
{
    c.MapType<ActualModel>(() => new OpenApiSchema { Type = "string" });

但我无法弄清楚如何将 "OtherModel" 生成为 OpenApiSchema 以便它替换 "ActualModel",也找不到有关它的信息或示例.

我试着给它完整的 class 名称,带命名空间和不带命名空间,我用谷歌搜索了 "Swashbuckle replace Type with other Type" 和 "Swashbuckle MapType" 但我找不到任何示例来说明如何简单地替换一种类型与应用程序中定义的另一种类型。

背景: 以上当然只是一个例子。在一个 "real" 项目中,我有通用的和分层的 class-定义,我想在我的控制器中 return 但我在控制器 returns 和之间的值之间有一个序列化程序实际的网络输出将数据结构更改为另一个更简单的结构,以便在 Typescript 端使用。这就是为什么我想替换 swashbuckle 生成的模型定义。

好吧,你可以 告诉 大摇大摆地展示你正在制作的东西,例如,如果它由于模板或层次结构而不明显:

[HttpGet]
[ProducesResponseType(typeof(OtherModel[]), 200)]             // <--- This line
public ActionResult<List<ActualModel>> Get()
{
    return new List<ActualModel>()
    {
        new ActualModel { StringValue1 = "SomeString" },
        new ActualModel { StringValue1 = "MoreString" }
    };
}

但是,您的工作是确保它们实际上兼容,没有检查。