Skip to content

Commit a18343b

Browse files
committed
added json converters for TimeSpan
1 parent d9dbcea commit a18343b

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,8 @@ public void addGenericHostSupportingFiles(final String clientPackageDir, final S
10951095
supportingFiles.add(new SupportingFile("ApiFactory.mustache", clientPackageDir, "ApiFactory.cs"));
10961096
supportingFiles.add(new SupportingFile("DateTimeJsonConverter.mustache", clientPackageDir, "DateTimeJsonConverter.cs"));
10971097
supportingFiles.add(new SupportingFile("DateTimeNullableJsonConverter.mustache", clientPackageDir, "DateTimeNullableJsonConverter.cs"));
1098+
supportingFiles.add(new SupportingFile("TimeSpanJsonConverter.mustache", clientPackageDir, "TimeSpanJsonConverter.cs"));
1099+
supportingFiles.add(new SupportingFile("TimeSpanNullableJsonConverter.mustache", clientPackageDir, "TimeSpanNullableJsonConverter.cs"));
10981100
if (useDateOnly()) {
10991101
supportingFiles.add(new SupportingFile("DateOnlyJsonConverter.mustache", clientPackageDir, "DateOnlyJsonConverter.cs"));
11001102
supportingFiles.add(new SupportingFile("DateOnlyNullableJsonConverter.mustache", clientPackageDir, "DateOnlyNullableJsonConverter.cs"));

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/HostConfiguration.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ namespace {{packageName}}.{{clientPackage}}
4343
_jsonOptions.Converters.Add(new DateOnlyJsonConverter());
4444
_jsonOptions.Converters.Add(new DateOnlyNullableJsonConverter());
4545
{{/supportsDateOnly}}
46+
_jsonOptions.Converters.Add(new TimeSpanJsonConverter());
47+
_jsonOptions.Converters.Add(new TimeSpanNullableJsonConverter());
4648
{{#models}}
4749
{{#model}}
4850
{{#isEnum}}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{{>partial_header}}
2+
using System;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
using System.Xml;
6+
7+
namespace {{packageName}}.{{clientPackage}}
8+
{
9+
/// <summary>
10+
/// Formatter for 'duration' openapi format as defined by ISO8601
11+
/// see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#data-types
12+
/// </summary>
13+
{{>visibility}} class TimeSpanJsonConverter : JsonConverter<TimeSpan>
14+
{
15+
/// <summary>
16+
/// Returns a TimeSpan from the Json object
17+
/// </summary>
18+
/// <param name="reader"></param>
19+
/// <param name="typeToConvert"></param>
20+
/// <param name="options"></param>
21+
/// <returns></returns>
22+
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
23+
{
24+
if (reader.TokenType == JsonTokenType.Null)
25+
throw new NotSupportedException();
26+
27+
string value = reader.GetString(){{nrt!}};
28+
return XmlConvert.ToTimeSpan(value);
29+
}
30+
31+
/// <summary>
32+
/// Writes the TimeSpan to the json writer in ISO 8601 duration format
33+
/// </summary>
34+
/// <param name="writer"></param>
35+
/// <param name="timeSpanValue"></param>
36+
/// <param name="options"></param>
37+
public override void Write(Utf8JsonWriter writer, TimeSpan timeSpanValue, JsonSerializerOptions options) => writer.WriteStringValue(XmlConvert.ToString(timeSpanValue));
38+
}
39+
}
40+
41+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{{>partial_header}}
2+
using System;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
using System.Xml;
6+
7+
namespace {{packageName}}.{{clientPackage}}
8+
{
9+
/// <summary>
10+
/// Formatter for 'duration' openapi format as defined by ISO8601
11+
/// see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#data-types
12+
/// </summary>
13+
{{>visibility}} class TimeSpanNullableJsonConverter : JsonConverter<TimeSpan?>
14+
{
15+
/// <summary>
16+
/// Returns a TimeSpan from the Json object
17+
/// </summary>
18+
/// <param name="reader"></param>
19+
/// <param name="typeToConvert"></param>
20+
/// <param name="options"></param>
21+
/// <returns></returns>
22+
public override TimeSpan? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
23+
{
24+
if (reader.TokenType == JsonTokenType.Null)
25+
return null;
26+
27+
string value = reader.GetString(){{nrt!}};
28+
return XmlConvert.ToTimeSpan(value);
29+
}
30+
31+
/// <summary>
32+
/// Writes the TimeSpan to the json writer in ISO 8601 duration format
33+
/// </summary>
34+
/// <param name="writer"></param>
35+
/// <param name="timeSpanValue"></param>
36+
/// <param name="options"></param>
37+
public override void Write(Utf8JsonWriter writer, TimeSpan? timeSpanValue, JsonSerializerOptions options)
38+
{
39+
if (timeSpanValue == null)
40+
writer.WriteNullValue();
41+
else
42+
writer.WriteStringValue(XmlConvert.ToString(timeSpanValue.Value));
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)