Skip to content

Commit a12bec0

Browse files
committed
Senparc.CO2NET.WebApi [2024-10-07] v2.0.0-beta2
1. Add UseLowerCaseApiName to WebApiEngineOptions 2. Add unique WebApi name to duplicate method name
1 parent 2fdb743 commit a12bec0

File tree

3 files changed

+75
-33
lines changed

3 files changed

+75
-33
lines changed

src/Senparc.CO2NET.WebApi/Senparc.CO2NET.WebApi.csproj

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>netstandard2.1;net8.0</TargetFrameworks>
4-
<Version>2.0.0-beta1</Version>
4+
<Version>2.0.0-beta2</Version>
55
<LangVersion>latest</LangVersion>
66
<AssemblyName>Senparc.CO2NET.WebApi</AssemblyName>
77
<RootNamespace>Senparc.CO2NET.WebApi</RootNamespace>
@@ -22,27 +22,30 @@
2222
<RepositoryUrl>https://github.com/Senparc/Senparc.CO2NET</RepositoryUrl>
2323
<Configurations>Debug;Release;Test</Configurations>
2424
<PackageReleaseNotes>
25-
v0.1 Genesis
26-
v0.2 Completed WebApiEngine first generation core version
27-
v0.2.3 Completed WebApiEngine second generation core version, fully supports dynamic API integration and corresponding XML generation
28-
v0.2.4 Added additional injectable classes or methods
29-
v0.2.5 Optimized asynchronous thread execution
30-
v0.2.5.7 Added ForbiddenExternalAccess parameter to set whether external access is allowed
31-
v0.2.6 Added WebApiEngineOptions
32-
v0.2.8 Provided .NET Standard 2.1 version
33-
v1.1 Provided the ability to synchronize parameter attributes to dynamic APIs
34-
v1.1.2 Optimized document extraction regular expressions
35-
v1.1.3 Added AddApiControllerAttribute option, default is true
36-
v1.3 Provided .NET 7.0 support
37-
v1.4.1 Used [ApiBind(Ignore = false)], added complete API generation ignore for the entire class
38-
v1.5.2.1 Organized log format
39-
v1.6.0
40-
1. Removed .NET 7.0 TargetFramework; added .NET 8.0 TargetFramework
41-
2. Removed reference to Microsoft.AspNetCore.Mvc.Core
42-
2. Referenced the latest Senparc.CO2NET.AspNet, .NET 6.0 and .NET 8.0 assemblies no longer depend on Microsoft.AspNetCore.Hosting.Abstractions and Microsoft.AspNetCore.Http.Abstractions
43-
[2024-09-11] v1.6.3 Updated Cache, removed InsertToCache(), added Count(prefix)
44-
[2024-10-07] v1.7.0 Stopped support for .NET 6.0
45-
</PackageReleaseNotes>
25+
v0.1 Genesis
26+
v0.2 Completed WebApiEngine first generation core version
27+
v0.2.3 Completed WebApiEngine second generation core version, fully supports dynamic API integration and corresponding XML generation
28+
v0.2.4 Added additional injectable classes or methods
29+
v0.2.5 Optimized asynchronous thread execution
30+
v0.2.5.7 Added ForbiddenExternalAccess parameter to set whether external access is allowed
31+
v0.2.6 Added WebApiEngineOptions
32+
v0.2.8 Provided .NET Standard 2.1 version
33+
v1.1 Provided the ability to synchronize parameter attributes to dynamic APIs
34+
v1.1.2 Optimized document extraction regular expressions
35+
v1.1.3 Added AddApiControllerAttribute option, default is true
36+
v1.3 Provided .NET 7.0 support
37+
v1.4.1 Used [ApiBind(Ignore = false)], added complete API generation ignore for the entire class
38+
v1.5.2.1 Organized log format
39+
v1.6.0
40+
1. Removed .NET 7.0 TargetFramework; added .NET 8.0 TargetFramework
41+
2. Removed reference to Microsoft.AspNetCore.Mvc.Core
42+
2. Referenced the latest Senparc.CO2NET.AspNet, .NET 6.0 and .NET 8.0 assemblies no longer depend on Microsoft.AspNetCore.Hosting.Abstractions and Microsoft.AspNetCore.Http.Abstractions
43+
[2024-09-11] v1.6.3 Updated Cache, removed InsertToCache(), added Count(prefix)
44+
[2024-10-07] v1.7.0 Stopped support for .NET 6.0
45+
[2024-10-07] v2.0.0-beta2
46+
1. Add UseLowerCaseApiName to WebApiEngineOptions
47+
2. Add unique WebApi name to duplicate method name
48+
</PackageReleaseNotes>
4649
</PropertyGroup>
4750
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
4851
<OutputPath>..\BuildOutPut</OutputPath>

src/Senparc.CO2NET.WebApi/WebApiEngines/WebApiEngine.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
1010
Modification Identifier: Senparc - 20211122
1111
Modification Description: v1.1 provides the ability to synchronize parameter attributes to dynamic APIs
12+
13+
Modification Identifier: Senparc - 20241108
14+
Modification Description: v2.0.0-beta2 1. Add UseLowerCaseApiName to WebApiEngineOptions
15+
2. Add unique WebApi name to duplicate method name
1216
1317
----------------------------------------------------------------*/
1418

@@ -57,6 +61,7 @@ public partial class WebApiEngine
5761
private Type _typeOfApiBind = typeof(ApiBindAttribute);
5862
private Type _baseApiControllerType;
5963
private bool _addApiControllerAttribute = true;
64+
private bool _useLowerCaseApiName = false;
6065

6166
public bool BuildXml => DocXmlPath != null;
6267

@@ -79,6 +84,7 @@ public WebApiEngine(Action<WebApiEngineOptions> options = null)
7984
TaskCount = opt.TaskCount;
8085
_showDetailApiLog = opt.ShowDetailApiLog;
8186
_addApiControllerAttribute = opt.AddApiControllerAttribute;
87+
_useLowerCaseApiName = opt.UseLowerCaseApiName;
8288
Register.ForbiddenExternalAccess = opt.ForbiddenExternalAccess;
8389
WebApiEngine.AdditionalAttributeFunc = opt.AdditionalAttributeFunc;
8490
}
@@ -252,14 +258,6 @@ private async Task BuildApiMethodForOneThread(IGrouping<string, KeyValuePair<str
252258
var indexOfApiGroupDot = apiBindInfo.Value.GlobalName.IndexOf(".");
253259
var apiName = apiBindInfo.Value.GlobalName.Substring(indexOfApiGroupDot + 1, apiBindInfo.Value.GlobalName.Length - indexOfApiGroupDot - 1);
254260

255-
//Ensure the name is not duplicated
256-
while (apiMethodName.ContainsKey(methodName))
257-
{
258-
methodName += "0";
259-
apiName += "0";
260-
}
261-
apiMethodName[methodName] = apiName;
262-
263261
//Current API's MethodInfo
264262
MethodInfo apiMethodInfo = apiBindInfo.Value.MethodInfo;
265263
//All parameter information of the current API
@@ -272,6 +270,18 @@ private async Task BuildApiMethodForOneThread(IGrouping<string, KeyValuePair<str
272270
WriteLog(apiLog, true);
273271
WriteLog($"-> {methodName} - Parameters: {parameters.Count()}".PadLeft(prefixIndex), true);
274272

273+
Func<string> getMethodUniqueNo = () => parameters.Sum(z => z.Name.Length + z.ParameterType.Name.Length).ToString();
274+
275+
//Ensure the name is not duplicated
276+
while (apiMethodName.ContainsKey(methodName))
277+
{
278+
//开发过程中可能会因为接口增加,导致重复名称的后缀改变,因此使用相对差异更大的方式增加后缀(将所有参数名、类型的字符串长度相加)
279+
//TODO:这种做法仍然无法解决第一个名称的命名问题(需要转回去修改)
280+
methodName += "_"+ getMethodUniqueNo;
281+
apiName += "_" + getMethodUniqueNo;
282+
}
283+
apiMethodName[methodName] = apiName;
284+
275285
//Add static method marker
276286
string showStaticApiState = null;//$"{(apiMethodInfo.IsStatic ? "_StaticApi" : "_NonStaticApi")}";
277287

@@ -298,6 +308,13 @@ private async Task BuildApiMethodForOneThread(IGrouping<string, KeyValuePair<str
298308
var apiBindGroupNamePath = apiBindName.Replace(":", "_");
299309
var apiNamePath = apiName.Replace(":", "_");
300310
var apiPath = $"/api/{keyName}/{apiBindGroupNamePath}/{apiNamePath}{showStaticApiState}";
311+
312+
//强制所有名称小写
313+
if (_useLowerCaseApiName)
314+
{
315+
apiPath = apiPath.ToLower();
316+
}
317+
301318
var routeAttrBuilder = new CustomAttributeBuilder(t2_4.GetConstructor(new Type[] { typeof(string) }),
302319
new object[] { apiPath }/*, new[] { t2_2.GetProperty("Name") }, new[] { routeName }*/);
303320
setPropMthdBldr.SetCustomAttribute(routeAttrBuilder);

src/Senparc.CO2NET.WebApi/WebApiEngines/WebApiEngineOptions.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
using System;
1+
/*----------------------------------------------------------------
2+
Copyright (C) 2023 Senparc
3+
4+
FileName: WebApiEngineOptions.cs
5+
File Function Description: WebApiEngine configuration parameters
6+
7+
8+
Creation Identifier: Senparc - 20210923
9+
10+
Modification Identifier: Senparc - 20241108
11+
Modification Description: v2.0.0-beta2 1. Add UseLowerCaseApiName to WebApiEngineOptions
12+
2. Add unique WebApi name to duplicate method name
13+
14+
----------------------------------------------------------------*/
15+
16+
17+
using System;
218
using System.Collections.Generic;
319
using System.Reflection.Emit;
420
using System.Reflection;
@@ -20,7 +36,11 @@ public class WebApiEngineOptions
2036
/// <param name="additionalAttributeFunc">Additional attributes to bind</param>
2137
/// <param name="forbiddenExternalAccess">Whether to allow external access, default is false, only local access to auto-generated WebApi is allowed</param>
2238
/// <param name="addApiControllerAttribute">Whether to automatically add [ApiController] tag to auto-generated interface classes (Controller)</param>
23-
public WebApiEngineOptions(string docXmlPath = null, ApiRequestMethod defaultRequestMethod = ApiRequestMethod.Post, Type baseApiControllerType = null, bool copyCustomAttributes = true, int taskCount = 4, bool showDetailApiLog = false, Func<MethodInfo, IEnumerable<CustomAttributeBuilder>> additionalAttributeFunc = null, bool forbiddenExternalAccess = true, bool addApiControllerAttribute = true)
39+
/// <param name="useLowerCaseApiName">Create lower-case api names</param>
40+
public WebApiEngineOptions(string docXmlPath = null, ApiRequestMethod defaultRequestMethod = ApiRequestMethod.Post,
41+
Type baseApiControllerType = null, bool copyCustomAttributes = true, int taskCount = 4, bool showDetailApiLog = false,
42+
Func<MethodInfo, IEnumerable<CustomAttributeBuilder>> additionalAttributeFunc = null, bool forbiddenExternalAccess = true,
43+
bool addApiControllerAttribute = true, bool useLowerCaseApiName = false)
2444
{
2545
DocXmlPath = docXmlPath;
2646
DefaultRequestMethod = defaultRequestMethod;
@@ -31,6 +51,7 @@ public WebApiEngineOptions(string docXmlPath = null, ApiRequestMethod defaultReq
3151
AdditionalAttributeFunc = additionalAttributeFunc;
3252
ForbiddenExternalAccess = forbiddenExternalAccess;
3353
AddApiControllerAttribute = addApiControllerAttribute;
54+
UseLowerCaseApiName = useLowerCaseApiName;
3455
}
3556

3657
/// <summary>
@@ -65,6 +86,7 @@ public WebApiEngineOptions(string docXmlPath = null, ApiRequestMethod defaultReq
6586
/// Whether to allow external access, default is false, only local access to auto-generated WebApi is allowed
6687
/// </summary>
6788
public bool ForbiddenExternalAccess { get; set; }
68-
public bool AddApiControllerAttribute { get; }
89+
public bool AddApiControllerAttribute { get; set; }
90+
public bool UseLowerCaseApiName { get; set; }
6991
}
7092
}

0 commit comments

Comments
 (0)