- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 Noda Time 的 API输入和输出类型。这些类型使用默认的 Noda Time 序列化格式(基本上是 ISO-8601 格式)序列化为 JSON 中的字符串。
我有一个看起来像这样的对象:
public class NodaTimeDataStructure
{
public System.DateTime DateTime { get; set; }
public DateInterval DateInterval { get; set; }
public DateTimeZone DateTimeZone { get; set; }
public Duration Duration { get; set; }
public Instant Instant { get; set; }
public Interval Interval { get; set; }
public IsoDayOfWeek IsoDayOfWeek { get; set; }
public LocalDate LocalDate { get; set; }
public LocalDateTime LocalDateTime { get; set; }
public LocalTime LocalTime { get; set; }
public Offset Offset { get; set; }
public OffsetDate OffsetDate { get; set; }
public OffsetDateTime OffsetDateTime { get; set; }
public OffsetTime OffsetTime { get; set; }
public Period Period { get; set; }
public ZonedDateTime ZonedDateTime { get; set; }
}
这通常会产生以下 Swagger JSON:
"NodaTimeDataStructure": {
"type": "object",
"additionalProperties": false,
"required": [
"dateTime", "duration", "instant", "interval", "isoDayOfWeek", "localDate", "localDateTime",
"localTime", "offset", "offsetDate", "offsetDateTime", "offsetTime", "zonedDateTime"
],
"properties": {
"dateTime": { "type": "string", "format": "date-time" },
"instant": { "type": "string", "format": "date-time" },
"zonedDateTime": { "type": "string", "format": "date-time" },
"offsetDateTime": { "type": "string", "format": "date-time" },
"localDateTime": { "type": "string", "format": "date-time" },
"localDate": { "type": "string", "format": "date" },
"localTime": { "type": "string", "format": "time" },
"duration": { "type": "string", "format": "time-span" },
"dateInterval": { "type": "array", "items": { "type": "string", "format": "date" } },
"dateTimeZone": { "$ref": "#/definitions/DateTimeZone" },
"interval": { "$ref": "#/definitions/Interval" },
"isoDayOfWeek": { "$ref": "#/definitions/IsoDayOfWeek" },
"offset": { "$ref": "#/definitions/Offset" },
"offsetDate": { "$ref": "#/definitions/OffsetDate" },
"offsetTime": { "$ref": "#/definitions/OffsetTime" },
"period": { "$ref": "#/definitions/Period" }
}
}
这使得无法在 C# 客户端中转换回正确的 Noda Time 类型。除了许多不同的类型具有完全相同的格式 ("date-time"
) 使得无法进行映射之外,某些类型具有不幸的定义。 DateInterval
生成一个 “date”
数组,因为它是 LocalDate
的可枚举,但简单的开始/结束日期格式会很有效更好的。其他方法是使用 $ref
创建的,用于包含完全不感兴趣的字段的非常复杂的对象。请注意,所有这些都应序列化为简单字符串(可以说不是间隔)。
我可以创建自己的类型映射器并将它们添加到 AspNetCoreToSwaggerGeneratorSettings
中,如下所示:
var nodaTimeTypeMappers = new[]
{
CreateTypeMapper(typeof(DateInterval), "date-interval"),
CreateTypeMapper(typeof(DateTimeZone), "date-time-zone"),
CreateTypeMapper(typeof(Duration), "duration"),
CreateTypeMapper(typeof(Instant), "instant"),
CreateTypeMapper(typeof(Interval), "interval"),
CreateTypeMapper(typeof(IsoDayOfWeek), "iso-day-of-week"),
CreateTypeMapper(typeof(LocalDate), "local-date"),
CreateTypeMapper(typeof(LocalDateTime), "local-date-time"),
CreateTypeMapper(typeof(LocalTime), "local-time"),
CreateTypeMapper(typeof(Offset), "offset"),
CreateTypeMapper(typeof(OffsetDate), "offset-date"),
CreateTypeMapper(typeof(OffsetDateTime), "offset-date-time"),
CreateTypeMapper(typeof(OffsetTime), "offset-time"),
CreateTypeMapper(typeof(Period), "period"),
CreateTypeMapper(typeof(ZonedDateTime), "zoned-date-time"),
};
foreach (var typeMapper in nodaTimeTypeMappers)
{
settings.TypeMappers.Add(typeMapper);
}
PrimitiveTypeMapper CreateTypeMapper(Type type, string name)
{
return new PrimitiveTypeMapper(type, s =>
{
s.Type = JsonObjectType.String;
s.Format = "noda-time-" + name;
});
}
得到这样的东西:
"NodaTimeRequest": {
"type": "object",
"additionalProperties": false,
"required": [
"dateTime", "duration", "instant", "interval", "isoDayOfWeek", "localDate", "localDateTime",
"localTime", "offset", "offsetDate", "offsetDateTime", "offsetTime", "zonedDateTime"
],
"properties": {
"dateTime": { "type": "string", "format": "date-time" },
"dateInterval": { "type": "string", "format": "noda-time-date-interval" },
"dateTimeZone": { "type": "string", "format": "noda-time-date-time-zone" },
"duration": { "type": "string", "format": "noda-time-duration" },
"instant": { "type": "string", "format": "noda-time-instant" },
"interval": { "type": "string", "format": "noda-time-interval" },
"isoDayOfWeek": { "type": "string", "format": "noda-time-iso-day-of-week" },
"localDate": { "type": "string", "format": "noda-time-local-date" },
"localDateTime": { "type": "string", "format": "noda-time-local-date-time" },
"localTime": { "type": "string", "format": "noda-time-local-time" },
"offset": { "type": "string", "format": "noda-time-offset" },
"offsetDate": { "type": "string", "format": "noda-time-offset-date" },
"offsetDateTime": { "type": "string", "format": "noda-time-offset-date-time" },
"offsetTime": { "type": "string", "format": "noda-time-offset-time" },
"period": { "type": "string", "format": "noda-time-period" },
"zonedDateTime": { "type": "string", "format": "noda-time-zoned-date-time" }
}
}
这允许像现有格式一样使用格式(“date-time”
、“date”
、“time”
, "time-span"
), 但我实在想不出如何让 swagger2csclient
使用这些格式正确地转换回相应的 Noda时间类型。我通常会遗漏什么吗?或者这目前不可能吗?
最佳答案
我没有 Swagger json 问题的解决方案,但我可以帮助解决 C# 客户端生成部分。
不是从 NSwag json 生成客户端,我们要做的是 NSwagStudio使用反射生成客户端。我正在使用“通过反射的 Web API”,并将运行时设置为“默认”:
此生成器“使用 .NET 反射来分析 ASP.NET Web API 或 ASP.NET Core Controller ”。您的里程当然可能会有所不同 - 还有一个“.NET 程序集”选项和/或您可能需要明确设置运行时。
在右侧 Pane 中,单击“CSharp 客户端”并切换到“CSharp 客户端”选项卡:
在上面的屏幕截图中可以看到第一道秘方:我们将 NodaTime
添加为附加命名空间。
更进一步,我们需要让 NSwagStudio 生成 DTO 类,并且 - 这是真正重要的事情 - 不通过将它们添加到“排除的类型名称”列表来生成任何 NodaTime 类型:
我使用的类型排除字符串是:DateInterval,DateTimeZone,Duration,Instant,Interval,IsoDayOfWeek,LocalDate,LocalDateTime,LocalTime,Offset,OffsetDate,OffsetDateTime,OffsetTime,Period,ZonedDateTime,CalendarSystem,Era
.
您还需要查看许多其他选项。完成后,按 Generate Outputs
即可生成 C# 客户端。
将客户端粘贴到引用 NodaTime 的项目中,我们可以看到使用了 NodaTime 类型:
我的测试使用了您的类 NodaTimeDataStructure
和这个 Controller :
[Route("api/[controller]")]
[ApiController]
public class NodaTimeController
{
[HttpGet]
public NodaTimeDataStructure Get() => new NodaTimeDataStructure();
}
出于此测试/演示的目的,我将其构建到一个以 .NET 4.8 为目标并使用 ASP.NET Core 2.2 的库中。
关于c# - NSwag:如何在 C# -> Swagger -> C# 客户端中使用自定义值对象类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58006479/
我试图在我的微服务项目中生成一个单一的 swagger,在 Api 网关中将所有服务 swagger 聚合成一个单一的服务。为了实现这一点,我正在学习下一个教程 https://objectpartn
我的任务是将 Swagger 安装到 Web API 项目中。 已安装:来自 nuget 的最新版本的 Swashbuckle。 (Swashbuckle.Core.Net45 和 Swashbuck
我正在编写一个 swagger 规范,并且我有三个独立的端点。我如何在文档中将它们分开?我想明确区分示例:用户、帖子和其他。所以每个人都会有一个 CRUD 描述并显示在 swagger UI 中,它看
我试图在 Swagger 中定义一个查询参数,其中包含来自预定义项目集的逗号分隔字符串,例如 ?fruits=Apples,Oranges,Bananas但我从 swagger 编辑器收到以下错误 s
我正在使用 go-swagger 来生成 API 服务器。我注意到从 swagger.yml 生成的 json 被保存在 restapi/embedded_spec.go 中. 公开该 JSON 规范
我使用的是 springfox 版本 2.9.2 和 swagger 注释 1.5.x。 ApiModel 注释支持鉴别器、子类型和父属性,这些是使多态性工作所需的,但我没有看到生成的正确的 apid
我正在尝试使用本地 swagger.json 文件在 swagger 文档中显示。 我的 swagger.json 文件位于/home/user1/swagger-ui/dist/swagger.js
我们有一些数字字段,由于遗留原因,它们具有隐式长度限制。 给定一个长度限制为 5 的数字字段,显而易见的方法是将最大值设置为 99999,但是有没有办法在 swagger 规范中指定 1.111 可以
我们的项目为单个 API 使用多个 swagger 文件,但看起来 swagger-codegen 只接受一个。在这种情况下,我们如何使用 swagger-codegen 生成代码? 最佳答案 您可以
我正在尝试使用 https://github.com/swagger-api/swagger-codegen 生成 nodejs 客户端 sdk这是我使用的命令 swagger-codegen gen
我定义了一个以 MyObject 作为参数的路径。 MyObject 具有猫和狗的属性。这些有默认值。 在 swagger-editor 中,该示例不显示默认值,但试用确实创建了一个具有正确默认值的
我最近从 Swashbuckle 过渡到 Swagger-Net .进行更改后我遇到的一个问题是,现在我无法调用需要在 Authorization header 中发送 token 的 API。下面是
正在使用 AspNetCore 为使用 IIS 托管的 Web 应用程序设置 swagger。 .json 页面加载并且似乎可以很好地接触所有 API,但是当导航到 {localhost}/swagg
我想将任何复杂的 swagger-API-document(swagger.json) 解析为 Java 对象。 可能是列表> 有哪些可用选项? 我正在尝试使用 io.swagger.parser.S
我要将我的 API 服务器集成到 Google Cloud Endpoints。 到目前为止,Google Cloud Endpoints 支持 swagger 2.0。 但是我的依赖项/库现在是 u
我是 swagger 的新手,发现有两个用于 swagger 注释的包:io.swagger.annotations 和 com.wordnik.swagger.annotations。我想知道它们之
好的,我有许多 io.swagger.models.Swagger 对象,我已将它们合并到一个新的 super Swagger 中。现在我想要 super html。我怎样才能得到这个?请注意,为了获
我们当前的部署模式要求我手动编写 swagger json 输出,该输出将由我公司使用的基于 Swagger 的 UI 使用。我希望我正在编写的 json 能够提供“默认”值来填充所有输入字段(包括
我有以下 HTTP 触发的 Azure 函数。我已经使用此链接为我的端点设置了 Swagger here .以下 API 需要一组查询字符串参数,即 "name"、"email"、"phone",因此
我正在努力让 Swagger 正确呈现我的 ServiceStack 服务。 我希望看到一个 UserId 字符串作为表单参数,一个 PrivateCustomer 对象作为主体参数,但是尽管 Use
我是一名优秀的程序员,十分优秀!