Skip to content

Commit

Permalink
feat: update swagger extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnkwlp committed Sep 13, 2023
1 parent c913ba4 commit 3af0fb6
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 46 deletions.
57 changes: 56 additions & 1 deletion src/SwaggerExtensions/SwaggerExtensions/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Swashbuckle.AspNetCore.SwaggerGen
{
internal static class Extensions
Expand All @@ -23,5 +27,56 @@ public static string RemovePreFix(this string value, string prefix)

return value;
}

public static string ToSimpleTypeString(this Type type, bool trimArgCount = true)
{
if (type.IsGenericType)
{
var genericArgs = type.GetGenericArguments().ToList();

return HandleTypeArguments(type, trimArgCount, genericArgs);
}

return type.Name;
}

private static string HandleTypeArguments(Type t, bool trimArgCount, List<Type> availableArguments)
{
if (t.IsGenericType)
{
string value = t.Name;
if (trimArgCount && value.IndexOf("`") > -1)
{
value = value.Substring(0, value.IndexOf("`"));
}

if (t.DeclaringType != null)
{
// This is a nested type, build the nesting type first
value = HandleTypeArguments(t.DeclaringType, trimArgCount, availableArguments) + "+" + value;
}

// Build the type arguments (if any)
string argString = "";
var thisTypeArgs = t.GetGenericArguments();
for (int i = 0; i < thisTypeArgs.Length && availableArguments.Count > 0; i++)
{
if (i != 0) argString += ", ";

argString += ToSimpleTypeString(availableArguments[0], trimArgCount);
availableArguments.RemoveAt(0);
}

// If there are type arguments, add them with < >
if (argString.Length > 0)
{
value += "<" + argString + ">";
}

return value;
}

return t.Name;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -54,7 +54,9 @@ public void Apply(OpenApiSchema schema, SchemaFilterContext context)
["values"] = enumKeyValue,
}
);

schema.Description = string.Join("<br/> ", names.Select(x => $"{Convert.ToInt32(Enum.Parse(type, x))}:{x}"));
}
}
}
}
}
111 changes: 69 additions & 42 deletions src/SwaggerExtensions/SwaggerExtensions/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;

namespace Swashbuckle.AspNetCore.SwaggerGen
{
public static class SwaggerGenOptionsExtensions
{
public static void GenerateSchemaIdAndOperationId(this SwaggerGenOptions options)
private static readonly Dictionary<string, int> OperationIds = new Dictionary<string, int>();
private static readonly Dictionary<ApiDescription, string> OperationIdCache = new Dictionary<ApiDescription, string>();

public static void GenerateSchemaIdAndOperationId(this SwaggerGenOptions options, bool removeDtoFix = true, bool allowDuplicateOperationId = false)
{
//
options.SchemaFilter<SwaggerEnumDescriptions>();
//
options.CustomSchemaIds(type =>
{
if (type.IsGenericType)
{
string part1 = type.FullName.Substring(0, type.FullName.IndexOf("`")).RemovePostFix("Dto");
string part2 = string.Concat(type.GetGenericArguments().Select(x => x.Name.RemovePostFix("Dto")));
var typeString = type.ToSimpleTypeString(true);

if (part1.EndsWith("ListResult") || part1.EndsWith("PagedResult"))
{
string temp1 = part1.Substring(0, part1.LastIndexOf("."));
string temp2 = part1.Substring(part1.LastIndexOf(".") + 1);
return $"{temp1}.{part2}{temp2}";
}
if (typeString.Contains("<"))
{
var wrapType = typeString.Substring(0, typeString.IndexOf("<"));
var argType = typeString.Substring(typeString.IndexOf("<") + 1, typeString.Length - wrapType.Length - 2);

return $"{part1}.{part2}";
typeString = argType.Replace(", ", null) + wrapType;
}

return type.FullName.RemovePostFix("Dto").Replace("Dto+", null);
if (removeDtoFix)
return typeString.Replace("Dto", null);

return typeString;
});
//
options.CustomOperationIds(e =>
Expand All @@ -36,44 +38,69 @@ public static void GenerateSchemaIdAndOperationId(this SwaggerGenOptions options
string controller = e.ActionDescriptor.RouteValues["controller"];
string method = e.HttpMethod;

if (action == "GetList")
return $"Get{controller}List";
if (OperationIdCache.ContainsKey(e))
{
return OperationIdCache[e];
}

var operationId = GenerateApiOperationId(method, controller, action);

if (!allowDuplicateOperationId)
{
if (OperationIds.ContainsKey(operationId))
{
OperationIds[operationId]++;

operationId = operationId + "_" + OperationIds[operationId];
}
else
{
OperationIds[operationId] = 0;
}
}

if (action == "GetAllList")
return $"GetAll{controller}List";
OperationIdCache[e] = operationId;

if (action.StartsWith("GetAll"))
return $"GetAll{controller}{action.RemovePreFix("GetAll")}";
return operationId;
});
}

if (action == "Get" || action == "Create" || action == "Update" || action == "Delete")
return action + controller;
private static string GenerateApiOperationId(string method, string controller, string action)
{
if (action == "GetList")
return $"Get{controller}List";

if (action.StartsWith("Get"))
return $"Get{controller}{action.RemovePreFix("Get")}";
if (action == "GetAllList")
return $"GetAll{controller}List";

if (action.StartsWith("Create"))
return $"Create{controller}{action.RemovePreFix("Create")}";
if (action.StartsWith("GetAll"))
return $"GetAll{controller}{action.RemovePreFix("GetAll")}";
if (action == "Get" || action == "Create" || action == "Update" || action == "Delete")
return action + controller;
if (action.StartsWith("Get"))
return $"Get{controller}{action.RemovePreFix("Get")}";
if (action.StartsWith("Create"))
return $"Create{controller}{action.RemovePreFix("Create")}";

if (action.StartsWith("Update"))
return $"Update{controller}{action.RemovePreFix("Update")}";
if (action.StartsWith("Update"))
return $"Update{controller}{action.RemovePreFix("Update")}";

if (action.StartsWith("Delete"))
return $"Delete{controller}{action.RemovePreFix("Delete")}";
if (action.StartsWith("Delete"))
return $"Delete{controller}{action.RemovePreFix("Delete")}";

if (action.StartsWith("BatchCreate"))
return $"BatchCreate{controller}{action.RemovePreFix("BatchCreate")}";
if (action.StartsWith("BatchCreate"))
return $"BatchCreate{controller}{action.RemovePreFix("BatchCreate")}";

if (action.StartsWith("BatchUpdate"))
return $"BatchUpdate{controller}{action.RemovePreFix("BatchUpdate")}";
if (action.StartsWith("BatchUpdate"))
return $"BatchUpdate{controller}{action.RemovePreFix("BatchUpdate")}";

if (action.StartsWith("BatchDelete"))
return $"BatchDelete{controller}{action.RemovePreFix("BatchDelete")}";
if (action.StartsWith("BatchDelete"))
return $"BatchDelete{controller}{action.RemovePreFix("BatchDelete")}";

if (method == "HttpGet")
return action + controller;
else
return controller + action;
});
if (method == "HttpGet")
return action + controller;

return controller + action;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageProjectUrl>https://github.com/jxnkwlp/Passingwind.CommonLibs</PackageProjectUrl>
<RepositoryUrl>https://github.com/jxnkwlp/Passingwind.CommonLibs</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<PackageVersion>0.2.0</PackageVersion>
<PackageVersion>0.3.0</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 3af0fb6

Please sign in to comment.