Rename model in Swashbuckle 6 (Swagger) with ASP.NET Core Web API

回眸只為那壹抹淺笑 提交于 2019-12-06 02:06:16

问题


I'm using Swashbuckle 6 (Swagger) with ASP.NET Core Web API. My models have DTO as a suffix, e.g.,

public class TestDTO {
    public int Code { get; set; }
    public string Message { get; set; }
}

How do I rename it to just "Test" in the generated documentation? I've tried adding a DataContract attribute with a name, but that didn't help.

[HttpGet]
public IActionResult Get() {
  //... create List<TestDTO>
  return Ok(list);
}

回答1:


Figured it out... similar to the answer here: Swashbuckle rename Data Type in Model

The only difference was the property is now called CustomSchemaIds instead of SchemaId:

options.CustomSchemaIds(schemaIdStrategy);

Instead of looking at the DataContract attribute, I just have it remove "DTO":

private static string schemaIdStrategy(Type currentClass) {
    string returnedValue = currentClass.Name;
    if (returnedValue.EndsWith("DTO"))
        returnedValue = returnedValue.Replace("DTO", string.Empty);
    return returnedValue;
}


来源:https://stackoverflow.com/questions/40644052/rename-model-in-swashbuckle-6-swagger-with-asp-net-core-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!