NSwag:您如何在C#-> Swagger-> C#客户端中使用自定义值对象类型?

问题描述 投票:3回答:1

我有一个在输入和输出中都使用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")相同的格式,但是我不能为了上帝的爱而想出如何制作[C0 ]使用这些格式将其正确转换回相应的Noda时间类型。我通常会丢失某些东西吗?还是目前无法做到?

c# swagger swagger-codegen nodatime nswag
1个回答
0
投票

我没有Swagger json问题的解决方案,但是我可以为C#客户端生成部分提供帮助。

而不是从NSwag json生成客户端,我们要做的是让swagger2csclient使用反射生成客户端。我正在使用“运行时将Web API通过反射”设置为“默认”:

NSwagStudio

此生成器“使用.NET反射来分析ASP.NET Web API或ASP.NET Core控制器”。当然,您的里程可能会有所不同-还有一个“ .NET程序集”选项和/或您可能需要显式设置运行时。

在右侧窗格上,单击“ CSharp Client”,然后切换到“ CSharp Client”选项卡:

enter image description here

在上面的屏幕快照中可以看到第一份秘密调味料:我们将enter image description here添加为其他名称空间。

此外,我们需要让NSwagStudio生成DTO类,这是非常重要的事情-not通过将它们添加到“排除的类型名称”列表中来生成任何NodaTime类型:

NodaTime

我使用的类型排除字符串是:enter image description here

您还需要查看许多其他选项。完成此操作后,按DateInterval,DateTimeZone,Duration,Instant,Interval,IsoDayOfWeek,LocalDate,LocalDateTime,LocalTime,Offset,OffsetDate,OffsetDateTime,OffsetTime,Period,ZonedDateTime,CalendarSystem,Era,将生成您的C#客户端。


将客户端粘贴到引用NodaTime的项目中,我们可以看到使用了NodaTime类型:

Generate Outputs

我的测试使用了您的班级enter image description here和此控制器:

NodaTimeDataStructure

出于本测试/演示的目的,我将其内置到针对.NET 4.8并使用ASP.NET Core 2.2的库中。

© www.soinside.com 2019 - 2024. All rights reserved.