C# .NetCore Swagger ISchemaFilter Long 转 String 防精度丢失

目录

解决方案

[方法1:使用 SchemaFilter](#方法1:使用 SchemaFilter)

[‌1、创建自定义 SchemaFilter‌](#‌1、创建自定义 SchemaFilter‌)

[‌2、在 Swagger 配置中使用该 Filter‌](#‌2、在 Swagger 配置中使用该 Filter‌)

[方法2:使用 OperationFilter 或 DocumentFilter](#方法2:使用 OperationFilter 或 DocumentFilter)

[1、‌创建自定义 OperationFilter](#1、‌创建自定义 OperationFilter)

[2、在 Swagger 配置中使用该 Filter](#2、在 Swagger 配置中使用该 Filter)

总结


如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

在使用 Swagger 与 C# 一起工作时,特别是在使用诸如 Swashbuckle.AspNetCore 这样的库时,确保数据类型正确映射非常重要,尤其是在处理像 long 这样的数值类型时。如果你需要将 long 类型转换为 string 类型以避免精度丢失问题,你可以通过自定义 Swagger 生成逻辑来实现这一点。

解决方案

方法1:使用 SchemaFilter

你可以创建一个自定义的 ISchemaFilter 来改变属性的 Schema 类型。例如,如果你想将某个 long 类型的属性转换为 string 类型,可以创建一个自定义的 SchemaFilter。

‌1、创建自定义 SchemaFilter‌

首先,创建一个实现 ISchemaFilter 的类:

using Swashbuckle.AspNetCore.SwaggerGen;

using Microsoft.OpenApi.Models;

using System.Collections.Generic;

using System.Linq;

public class LongToStringSchemaFilter : ISchemaFilter

{

public void Apply(OpenApiSchema schema, SchemaFilterContext context)

{

if (context.Type == typeof(long))

{

schema.Type = "string";

schema.Format = "int64"; // 如果需要保持数值格式,可以指定为 "int64"

}

}

}

‌2、在 Swagger 配置中使用该 Filter‌

在配置 Swagger 时,使用这个过滤器:

services.AddSwaggerGen(c =>

{

c.SchemaFilter<LongToStringSchemaFilter>(); // 应用自定义过滤器

// 其他配置...

});

方法2:使用 OperationFilter 或 DocumentFilter

如果你需要更细粒度的控制,比如在特定操作上转换类型,可以使用 OperationFilter 或 DocumentFilter。例如,使用 OperationFilter:

1、‌创建自定义 OperationFilter

using Swashbuckle.AspNetCore.SwaggerGen;

using Microsoft.OpenApi.Models;

using System.Collections.Generic;

using System.Linq;

public class LongToStringOperationFilter : IOperationFilter

{

public void Apply(OpenApiOperation operation, OperationFilterContext context)

{

var parametersToChange = context.MethodInfo.GetParameters()

.Where(p => p.ParameterType == typeof(long))

.Select(p => p.Name)

.ToList();

foreach (var param in operation.Parameters)

{

if (parametersToChange.Contains(param.Name))

{

param.Schema.Type = "string";

param.Schema.Format = "int64"; // 如果需要保持数值格式,可以指定为 "int64"

}

}

}

}

2、在 Swagger 配置中使用该 Filter

services.AddSwaggerGen(c =>

{

c.OperationFilter<LongToStringOperationFilter>(); // 应用自定义过滤器

// 其他配置...

});

总结

通过上述任一方法,你可以在 Swagger 中将 long 类型的参数或属性转换为 string 类型,从而避免精度丢失问题。选择哪种方法取决于你的具体需求,比如是否需要全局转换还是仅在特定操作上转换。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关推荐
Jeff-Tsai3 小时前
一个不用注册方法的 JSON-RPC 包,怎么决定哪些方法能被调用?
c#·.net·asp.net core·json-rpc
rockey6271 天前
AScript正式推出Template模板引擎
c#
沐言Agent1 天前
做工业上位机,这个 C# 开源框架把 PLC 通讯、流程控制和 MES 对接都备好了
c#·工业上位机
投研帮1 天前
C#文档管理系统开发实战:技术选型、全文检索与并发控制
c#·.net·文档管理系统
2395730492 天前
在 Avalonia 12 上用纯 .NET 重写 Monaco 编辑器:TKCode.Arc CodeEditor / DiffEditor / LSP 深度架构与实战
c#·.net·lsp·avalonia·monaco·codeeditor·tkcode.arc
新*2 天前
在 .NET 里嵌入 IronPython:内存只涨不降的真相与正确的封装姿势
c#·.net·内存优化·ironpython·dlr
2501_930707782 天前
使用C#从 PowerPoint 演示文稿中提取图片
开发语言·c#·powerpoint
zhanghaha13142 天前
C# 系列教程_15 类 Class 保姆级系统教程(面向新手,大量示例 + 底层机理)
java·开发语言·c#
宝桥南山3 天前
Visual Studio - 记录一个Visual Studio 2026(Insiders)的更新问题
windows·microsoft·微软·.net·.netcore·visual studio
Risehuxyc3 天前
HttpClient怎么传POST参数
c#