From 219ebf9ad4a6940e6e3d07a4f81325de72cf749a Mon Sep 17 00:00:00 2001 From: Twan Jacobs Date: Thu, 9 Jun 2022 09:58:50 +0200 Subject: [PATCH] Fix DateFormatConverter System.Text.Json to respect DateType setting (#1527) --- .../GeneralGeneratorTests.cs | 4 ++-- .../Models/DateFormatConverterTemplateModel.cs | 3 +++ .../Templates/DateFormatConverter.liquid | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs index 306dd2ed5..bc0b442ee 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs @@ -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", code); Assert.Contains(@"[System.Text.Json.Serialization.JsonConverter(typeof(DateFormatConverter))]", code); AssertCompile(code); @@ -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", code); Assert.Contains(@"[System.Text.Json.Serialization.JsonConverter(typeof(DateFormatConverter))]", code); AssertCompile(code); diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/DateFormatConverterTemplateModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/DateFormatConverterTemplateModel.cs index 967a7016d..112761406 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/DateFormatConverterTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/DateFormatConverterTemplateModel.cs @@ -26,5 +26,8 @@ public DateFormatConverterTemplateModel(CSharpGeneratorSettings settings) /// Gets a value indicating whether to use System.Text.Json public bool UseSystemTextJson => _settings.JsonLibrary == CSharpJsonLibrary.SystemTextJson; + + /// Gets the date .NET type. + public string DateType => _settings.DateType; } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/DateFormatConverter.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/DateFormatConverter.liquid index dc87d9341..a4d55752b 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/DateFormatConverter.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/DateFormatConverter.liquid @@ -1,8 +1,8 @@ [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "{{ ToolchainVersion }}")] {%- if UseSystemTextJson -%} -internal class DateFormatConverter : System.Text.Json.Serialization.JsonConverter +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) @@ -10,10 +10,10 @@ internal class DateFormatConverter : System.Text.Json.Serialization.JsonConverte 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")); }