diff --git a/Controllers/SimApiAuthController.cs b/Controllers/SimApiAuthController.cs
index 7bddf55..523866f 100644
--- a/Controllers/SimApiAuthController.cs
+++ b/Controllers/SimApiAuthController.cs
@@ -1,5 +1,4 @@
-using System.Collections.Generic;
-using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc;
using SimApi.Attributes;
using SimApi.Communications;
using SimApi.Helpers;
diff --git a/Controllers/SimApiBaseController.cs b/Controllers/SimApiBaseController.cs
index 6fc7093..c28722e 100644
--- a/Controllers/SimApiBaseController.cs
+++ b/Controllers/SimApiBaseController.cs
@@ -1,5 +1,4 @@
-using System.Diagnostics.CodeAnalysis;
-using SimApi.Communications;
+using SimApi.Communications;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
@@ -38,74 +37,4 @@ public class SimApiBaseController : Controller
break;
}
}
-
-
- ///
- /// 错误返回
- ///
- /// 错误代码
- /// 错误描述(若是常规错误,代码可自动带取描述)
- ///
- protected static void Error(int code = 500, string message = "")
- {
- throw new SimApiException(code, message);
- }
-
- ///
- /// 检测条件,根据条件返回报错 如果condition是ture报错
- ///
- /// 检测条件
- /// 错误代码
- /// 错误描述
- protected static void ErrorWhen([DoesNotReturnIf(true)] bool condition, int code = 400, string message = "")
- {
- if (condition)
- {
- Error(code, message);
- }
- }
-
-
- ///
- /// 检测条件,根据条件返回报错 如果condition是ture报错
- ///
- /// 检测条件
- /// 错误代码
- /// 错误描述
- protected static void ErrorWhenTrue([DoesNotReturnIf(true)] bool condition, int code = 400, string message = "")
- {
- ErrorWhen(condition, code, message);
- }
-
-
- ///
- /// 如果condition是false 报错
- ///
- ///
- ///
- ///
- protected static void ErrorWhenFalse([DoesNotReturnIf(false)] bool condition, int code = 400, string message = "")
- {
- ErrorWhen(!condition, code, message);
- }
-
- ///
- /// 检测给定的变量是否为NUll
- ///
- /// 检测条件
- /// 错误代码
- /// 错误描述
- protected static void ErrorWhenNull([NotNull] object? condition, int code = 404, string message = "请求的资源不存在")
- {
- ErrorWhen(condition == null, code, message);
- }
-
- ///
- /// 上传文件
- ///
- ///
- protected SimApiBaseResponse UploadFile()
- {
- return new SimApiBaseResponse();
- }
}
\ No newline at end of file
diff --git a/GlobalUsing.cs b/GlobalUsing.cs
new file mode 100644
index 0000000..1c83965
--- /dev/null
+++ b/GlobalUsing.cs
@@ -0,0 +1 @@
+global using static SimApiErrorExtension;
\ No newline at end of file
diff --git a/SimApiErrorExtension.cs b/SimApiErrorExtension.cs
new file mode 100644
index 0000000..0075a4c
--- /dev/null
+++ b/SimApiErrorExtension.cs
@@ -0,0 +1,70 @@
+using System.Diagnostics.CodeAnalysis;
+using SimApi.Exceptions;
+
+
+public static class SimApiErrorExtension
+{
+ ///
+ /// 错误返回
+ ///
+ /// 错误代码
+ /// 错误描述(若是常规错误,代码可自动带取描述)
+ ///
+ public static void Error(int code = 500, string message = "")
+ {
+ throw new SimApiException(code, message);
+ }
+
+ ///
+ /// 检测条件,根据条件返回报错 如果condition是ture报错
+ ///
+ /// 检测条件
+ /// 错误代码
+ /// 错误描述
+ public static void ErrorWhen([DoesNotReturnIf(true)] bool condition, int code = 400,
+ string message = "")
+ {
+ if (condition)
+ {
+ Error(code, message);
+ }
+ }
+
+
+ ///
+ /// 检测条件,根据条件返回报错 如果condition是ture报错
+ ///
+ /// 检测条件
+ /// 错误代码
+ /// 错误描述
+ public static void ErrorWhenTrue([DoesNotReturnIf(true)] bool condition, int code = 400,
+ string message = "")
+ {
+ ErrorWhen(condition, code, message);
+ }
+
+
+ ///
+ /// 如果condition是false 报错
+ ///
+ ///
+ ///
+ ///
+ public static void ErrorWhenFalse([DoesNotReturnIf(false)] bool condition, int code = 400,
+ string message = "")
+ {
+ ErrorWhen(!condition, code, message);
+ }
+
+ ///
+ /// 检测给定的变量是否为NUll
+ ///
+ /// 检测条件
+ /// 错误代码
+ /// 错误描述
+ public static void ErrorWhenNull([NotNull] object? condition, int code = 404,
+ string message = "请求的资源不存在")
+ {
+ ErrorWhen(condition == null, code, message);
+ }
+}
\ No newline at end of file
diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs
index 6457889..def77e4 100644
--- a/SimApiExtensions.cs
+++ b/SimApiExtensions.cs
@@ -1,5 +1,4 @@
using System;
-using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
@@ -136,14 +135,14 @@ public static class SimApiExtensions
if (t.IsArray)
{
var elementType = t.GetElementType();
- return $"{GetSimpleTypeName(elementType, depth + 1)}[]";
+ return $"{GetSimpleTypeName(elementType!, depth + 1)}[]";
}
// 处理可空类型
if (Nullable.GetUnderlyingType(t) != null)
{
var underlyingType = Nullable.GetUnderlyingType(t);
- return GetSimpleTypeName(underlyingType, depth + 1);
+ return GetSimpleTypeName(underlyingType!, depth + 1);
}
// 处理泛型类型(递归解析嵌套泛型)
diff --git a/SwaggerFilters/AesBodyOperationFilter.cs b/SwaggerFilters/AesBodyOperationFilter.cs
index 62822c5..5fedda0 100644
--- a/SwaggerFilters/AesBodyOperationFilter.cs
+++ b/SwaggerFilters/AesBodyOperationFilter.cs
@@ -22,11 +22,11 @@ public class AesBodyOperationFilter : IOperationFilter
.GetCustomAttribute() != null;
if (!hasAesBodyAttr) continue;
// 1. 移除默认的 Query 参数描述(如果存在)
- var queryParam = operation.Parameters
+ var queryParam = operation.Parameters!
.FirstOrDefault(p => p.Name == parameter.Name);
if (queryParam != null)
{
- operation.Parameters.Remove(queryParam);
+ operation.Parameters!.Remove(queryParam);
}
// 2. 添加 Body 参数描述
diff --git a/SwaggerFilters/GlobalDynamicObjectSchemaFilter.cs b/SwaggerFilters/GlobalDynamicObjectSchemaFilter.cs
index e344da7..c44b6f7 100644
--- a/SwaggerFilters/GlobalDynamicObjectSchemaFilter.cs
+++ b/SwaggerFilters/GlobalDynamicObjectSchemaFilter.cs
@@ -13,7 +13,7 @@ public class GlobalDynamicObjectSchemaFilter : ISchemaFilter
{
if (!IsDynamicObjectType(context.Type)) return;
var oaSchema = schema as OpenApiSchema;
- oaSchema.AdditionalPropertiesAllowed = true;
+ oaSchema!.AdditionalPropertiesAllowed = true;
oaSchema.AdditionalProperties = new OpenApiSchema
{
Type = JsonSchemaType.Object, // 表示 value 可以是任意类型(兼容所有类型)
diff --git a/SwaggerFilters/RemoveEmptyTagsFilter.cs b/SwaggerFilters/RemoveEmptyTagsFilter.cs
index 5029f98..87975bf 100644
--- a/SwaggerFilters/RemoveEmptyTagsFilter.cs
+++ b/SwaggerFilters/RemoveEmptyTagsFilter.cs
@@ -10,16 +10,16 @@ public class RemoveEmptyTagsFilter : IDocumentFilter
{
// 步骤1:收集所有有接口的 Tag 名称
var tagsWithOperations = swaggerDoc.Paths.Values
- .SelectMany(path => path.Operations.Values)
- .SelectMany(op => op.Tags.Select(t => t.Name))
+ .SelectMany(path => path.Operations!.Values)
+ .SelectMany(op => op.Tags!.Select(t => t.Name))
.Distinct()
.ToList();
// 步骤2:移除无接口的空 Tag
- var emptyTags = swaggerDoc.Tags.Where(t => !tagsWithOperations.Contains(t.Name)).ToList();
+ var emptyTags = swaggerDoc.Tags!.Where(t => !tagsWithOperations.Contains(t.Name)).ToList();
foreach (var tag in emptyTags)
{
- swaggerDoc.Tags.Remove(tag);
+ swaggerDoc.Tags!.Remove(tag);
}
}
}
\ No newline at end of file
diff --git a/SwaggerFilters/SimApiResponseOperationFilter.cs b/SwaggerFilters/SimApiResponseOperationFilter.cs
index d57d851..54be673 100644
--- a/SwaggerFilters/SimApiResponseOperationFilter.cs
+++ b/SwaggerFilters/SimApiResponseOperationFilter.cs
@@ -46,7 +46,7 @@ public class SimApiResponseOperationFilter : IOperationFilter
var schema = context.SchemaGenerator.GenerateSchema(wrappedType, context.SchemaRepository);
// 5. 替换 Swagger 文档中的响应类型(只保留 200 OK 的响应,匹配过滤器逻辑)
- operation.Responses.Clear(); // 清除默认响应(如 200 返回原始类型)
+ operation.Responses!.Clear(); // 清除默认响应(如 200 返回原始类型)
operation.Responses.Add("200", new OpenApiResponse
{
Description = "请求成功",
diff --git a/csharp-simapi-coding-style.md b/csharp-simapi-coding-style.md
index b86bfbf..ff6f935 100644
--- a/csharp-simapi-coding-style.md
+++ b/csharp-simapi-coding-style.md
@@ -323,7 +323,7 @@ string appId
### 7.7 错误处理
-使用基类的 `ErrorWhen` 系列方法,**不手动 throw、不返回错误码**:
+使用基类的 `ErrorWhen` 系列方法,**不手动 throw、不返回错误码**:(全局扩展,任何地方都可以这样抛出异常)
```csharp
ErrorWhenNull(entity); // null 则报错(默认 404)