Skip to content

Commit

Permalink
Fix STJ inheritance, closes RicoSuter/NSwag#3871
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Feb 10, 2022
1 parent 932884e commit e2a60bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyContainer>();
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();
Expand All @@ -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<MyContainer>();
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<TBase> : System.Text.Json.Serialization.JsonConverter<TBase>", code);
}
}
}
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public override IEnumerable<CodeArtifact> 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));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//-----------------------------------------------------------------------
// <copyright file="JsonInheritanceConverterTemplateModel.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

namespace NJsonSchema.CodeGeneration.CSharp.Models
{
/// <summary>The JsonInheritanceConverterTemplateModel.</summary>
public class JsonInheritanceConverterTemplateModel
{
private readonly CSharpGeneratorSettings _settings;

/// <summary>The DateFormatConverterTemplateModel.</summary>
public JsonInheritanceConverterTemplateModel(CSharpGeneratorSettings settings)
{
_settings = settings;
}

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

0 comments on commit e2a60bc

Please sign in to comment.