You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.2 KiB
38 lines
1.2 KiB
namespace DealerSelection.Common.Helpers;
|
|
|
|
public class SwashbuckleSchemaHelper
|
|
{
|
|
private readonly Dictionary<string, int> _schemaNameRepetition = new ();
|
|
|
|
/// <summary>
|
|
/// Eliminate the case where more than one method returns the same type in different controllers
|
|
/// swagger documentation to fail
|
|
/// </summary>
|
|
/// <param name="modelType"></param>
|
|
/// <returns></returns>
|
|
public string GetSchemaId(Type modelType)
|
|
{
|
|
string id = DefaultSchemaIdSelector (modelType);
|
|
|
|
if(!_schemaNameRepetition.ContainsKey(id))
|
|
_schemaNameRepetition.Add(id, 0);
|
|
|
|
int count = _schemaNameRepetition[id] + 1;
|
|
_schemaNameRepetition[id] = count;
|
|
|
|
return $"{id}{(count > 1 ? count.ToString() : "")}";
|
|
}
|
|
|
|
private string DefaultSchemaIdSelector(Type modelType)
|
|
{
|
|
if (!modelType.IsConstructedGenericType)
|
|
return modelType.Name.Replace("[]", "Array");
|
|
|
|
string prefix = modelType.GetGenericArguments()
|
|
.Select(genericArg => DefaultSchemaIdSelector(genericArg))
|
|
.Aggregate((previous, current) => previous + current);
|
|
|
|
return prefix + modelType.Name.Split('`').First();
|
|
}
|
|
|
|
}
|