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

3

18

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);
}
Trudy answered 16/11, 2016 at 23:18 Comment(0)
T
42

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;
}
Trudy answered 18/11, 2016 at 18:45 Comment(3)
This is a very helpful answer.Wilton
A quick inline solution could be c.CustomSchemaIds(type => type.Name.EndsWith("DTO") ? type.Name.Replace("DTO", string.Empty) : type.Name);Phonon
Gave me ideas on how I can remove the "tbl" from EFCore-generated classes. Thanks @ultravelocity!Esposito
C
2

The answer from @ultravelocity didnt quite work for me. The error was like "'Response`1' is already used". I dont know what was the exact reason, but i guess it was something about inheritance / generics (since i returned a paged responses).

Based on the question and answer from @ultravelocity I developed a possible solution for .net 5 (maybe for asp.net core 2.c/3.d also) which tackles this problem.

Steps:

  1. Add customSchemaId-Strategy like @ultravelocity did
a.CustomSchemaIds(schemaIdStrategy);
  1. Add your custom strategy function
private static string schemaIdStrategy(Type currentClass)
{
    string customSuffix = "Response";
    var tmpDisplayName = currentClass.ShortDisplayName().Replace("<", "").Replace(">", "");
    var removedSuffix = tmpDisplayName.EndsWith(customSuffix) ? tmpDisplayName.Substring(0, tmpDisplayName.Length - customSuffix.Length) : tmpDisplayName;
    return removedSuffix;
}
Carlynne answered 2/5, 2021 at 8:13 Comment(1)
In the accepted answer, there is obviously a problem with generics. Also, where does ShortDisplayName come from?Commandment
H
0

If anyone is interested in just changing the default schema ids, they can use this implementation:

private static string DefaultSchemaIdSelector(Type modelType)
{
    if (!modelType.IsConstructedGenericType) return modelType.Name.Replace("[]", "Array");

    var prefix = modelType.GetGenericArguments()
        .Select(genericArg => DefaultSchemaIdSelector(genericArg))
        .Aggregate((previous, current) => previous + current);

    return prefix + modelType.Name.Split('`').First();
}

and then proceed to build on it like so

options.CustomSchemaIds(type => DefaultSchemaIdSelector(type).Replace("DTO", string.Empty));
Homogenous answered 20/10, 2023 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.