Skip to content

Commit

Permalink
Fix DateFormatConverter System.Text.Json to respect DateType setting (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AroglDarthu authored Jun 9, 2022
1 parent d8df4f3 commit 219ebf9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ public async Task When_definition_contains_date_and_use_system_text_json_then_co
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains(@"class DateFormatConverter", code);
Assert.Contains(@"class DateFormatConverter : System.Text.Json.Serialization.JsonConverter<System.DateTime>", code);
Assert.Contains(@"[System.Text.Json.Serialization.JsonConverter(typeof(DateFormatConverter))]", code);

AssertCompile(code);
Expand Down Expand Up @@ -1791,7 +1791,7 @@ public async Task When_definition_contains_date_and_use_system_text_json_then_co
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains(@"class DateFormatConverter", code);
Assert.Contains(@"class DateFormatConverter : System.Text.Json.Serialization.JsonConverter<System.DateTimeOffset>", code);
Assert.Contains(@"[System.Text.Json.Serialization.JsonConverter(typeof(DateFormatConverter))]", code);

AssertCompile(code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ public DateFormatConverterTemplateModel(CSharpGeneratorSettings settings)

/// <summary>Gets a value indicating whether to use System.Text.Json</summary>
public bool UseSystemTextJson => _settings.JsonLibrary == CSharpJsonLibrary.SystemTextJson;

/// <summary>Gets the date .NET type.</summary>
public string DateType => _settings.DateType;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "{{ ToolchainVersion }}")]
{%- if UseSystemTextJson -%}
internal class DateFormatConverter : System.Text.Json.Serialization.JsonConverter<System.DateTime>
internal class DateFormatConverter : System.Text.Json.Serialization.JsonConverter<{{ DateType }}>
{
public override System.DateTime Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
public override {{ DateType }} Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
var dateTime = reader.GetString();
if (dateTime == null)
{
throw new System.Text.Json.JsonException("Unexpected JsonTokenType.Null");
}

return System.DateTime.Parse(dateTime);
return {{ DateType }}.Parse(dateTime);
}

public override void Write(System.Text.Json.Utf8JsonWriter writer, System.DateTime value, System.Text.Json.JsonSerializerOptions options)
public override void Write(System.Text.Json.Utf8JsonWriter writer, {{ DateType }} value, System.Text.Json.JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd"));
}
Expand Down

0 comments on commit 219ebf9

Please sign in to comment.