From e2a60bcd593479e3f8a7b64e87b02c9b4b7bb5d6 Mon Sep 17 00:00:00 2001 From: Rico Suter Date: Thu, 10 Feb 2022 14:20:32 +0100 Subject: [PATCH] Fix STJ inheritance, closes https://github.com/RicoSuter/NSwag/issues/3871 --- .../InheritanceInterfaceTests.cs | 24 ++++++++++++++++-- .../CSharpGenerator.cs | 4 +-- .../JsonInheritanceConverterTemplateModel.cs | 25 +++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/NJsonSchema.CodeGeneration.CSharp/Models/JsonInheritanceConverterTemplateModel.cs diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceInterfaceTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceInterfaceTests.cs index f166d4b71..3114beea0 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceInterfaceTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceInterfaceTests.cs @@ -42,13 +42,13 @@ public class Banana : Fruit, IBanana } [Fact] - public async Task When_schema_has_base_schema_then_it_is_referenced() + public async Task When_schema_has_base_schema_then_it_is_referenced_with_Newtonsoft() { //// Arrange var json = JsonSchema.FromType(); var data = json.ToJson(); - var generator = new CSharpGenerator(json, new CSharpGeneratorSettings()); + var generator = new CSharpGenerator(json, new CSharpGeneratorSettings { JsonLibrary = CSharpJsonLibrary.NewtonsoftJson }); //// Act var code = generator.GenerateFile(); @@ -57,6 +57,26 @@ public async Task When_schema_has_base_schema_then_it_is_referenced() Assert.True(json.Properties["Item"].ActualTypeSchema.AllOf.First().HasReference); Assert.Contains("[Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), \"discriminator\")]", code); Assert.Contains("[JsonInheritanceAttribute(\"Banana\", typeof(Banana))]", code); + Assert.Contains("internal class JsonInheritanceConverter : Newtonsoft.Json.JsonConverter", code); + } + + [Fact] + public async Task When_schema_has_base_schema_then_it_is_referenced_with_STJ() + { + //// Arrange + var json = JsonSchema.FromType(); + var data = json.ToJson(); + + var generator = new CSharpGenerator(json, new CSharpGeneratorSettings { JsonLibrary = CSharpJsonLibrary.SystemTextJson }); + + //// Act + var code = generator.GenerateFile(); + + //// Assert + Assert.True(json.Properties["Item"].ActualTypeSchema.AllOf.First().HasReference); + Assert.Contains("[JsonInheritanceConverter(typeof(Fruit), \"discriminator\")]", code); + Assert.Contains("[JsonInheritanceAttribute(\"Banana\", typeof(Banana))]", code); + Assert.Contains("internal class JsonInheritanceConverter : System.Text.Json.Serialization.JsonConverter", code); } } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs index 8692322c6..7ccbfdaeb 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs @@ -57,13 +57,13 @@ public override IEnumerable GenerateTypes() { if (Settings.ExcludedTypeNames?.Contains("JsonInheritanceAttribute") != true) { - var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceAttribute", new object()); + var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceAttribute", new JsonInheritanceConverterTemplateModel(Settings)); artifacts.Add(new CodeArtifact("JsonInheritanceAttribute", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template)); } if (Settings.ExcludedTypeNames?.Contains("JsonInheritanceConverter") != true) { - var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceConverter", new object()); + var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceConverter", new JsonInheritanceConverterTemplateModel(Settings)); artifacts.Add(new CodeArtifact("JsonInheritanceConverter", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template)); } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/JsonInheritanceConverterTemplateModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/JsonInheritanceConverterTemplateModel.cs new file mode 100644 index 000000000..ef18c733f --- /dev/null +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/JsonInheritanceConverterTemplateModel.cs @@ -0,0 +1,25 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +namespace NJsonSchema.CodeGeneration.CSharp.Models +{ + /// The JsonInheritanceConverterTemplateModel. + public class JsonInheritanceConverterTemplateModel + { + private readonly CSharpGeneratorSettings _settings; + + /// The DateFormatConverterTemplateModel. + public JsonInheritanceConverterTemplateModel(CSharpGeneratorSettings settings) + { + _settings = settings; + } + + /// Gets a value indicating whether to use System.Text.Json + public bool UseSystemTextJson => _settings.JsonLibrary == CSharpJsonLibrary.SystemTextJson; + } +}