diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs index 188c83fde..066cdc0c7 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; +using System.IO; +using System.Reflection; using System.Runtime.Serialization; using System.Threading.Tasks; using Newtonsoft.Json; @@ -132,5 +133,33 @@ public async Task When_property_references_any_schema_with_inheritance_then_prop //// Assert Assert.Contains("public Dog Dog { get; set; }", code); } + + [Fact] + public async Task When_definitions_inherit_from_root_schema() + { + //// Arrange + var path = GetTestDirectory() + "/References/Animal.json"; + + //// Act + var schema = await JsonSchema.FromFileAsync(path); + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Record }); + + //// Act + var code = generator.GenerateFile(); + + //// Assert + Assert.Contains("public abstract partial class Animal", code); + Assert.Contains("public partial class Cat : Animal", code); + Assert.Contains("public partial class PersianCat : Cat", code); + Assert.Contains("[JsonInheritanceAttribute(\"Cat\", typeof(Cat))]", code); + Assert.Contains("[JsonInheritanceAttribute(\"PersianCat\", typeof(PersianCat))]", code); + } + + private string GetTestDirectory() + { + var codeBase = Assembly.GetExecutingAssembly().CodeBase; + var uri = new UriBuilder(codeBase); + return Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path)); + } } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj index 36fd4994c..5a8583248 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NJsonSchema.CodeGeneration.CSharp.Tests.csproj @@ -7,6 +7,7 @@ + @@ -16,6 +17,9 @@ Always + + Always + Always diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json b/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json new file mode 100644 index 000000000..6e8b7fd21 --- /dev/null +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json @@ -0,0 +1,25 @@ +{ + "title": "Animal", + "type": "object", + "x-abstract": true, + "discriminator": "discriminator", + + "definitions": { + "Cat": { + "type": "object", + "allOf": [ + { + "$ref": "Animal.json" + } + ] + }, + "PersianCat": { + "type": "object", + "allOf": [ + { + "$ref": "#/definitions/Cat" + } + ] + } + } +} \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs index fb26820d8..2fb426cdd 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs @@ -78,6 +78,7 @@ schema is JsonSchemaProperty property && var markAsNullableType = Settings.GenerateNullableReferenceTypes && isNullable; if (schema.ActualTypeSchema.IsAnyType && + schema.ActualDiscriminator == null && schema.InheritedSchema == null && // not in inheritance hierarchy schema.AllOf.Count == 0 && !Types.Keys.Contains(schema) && diff --git a/src/NJsonSchema/JsonSchemaAppender.cs b/src/NJsonSchema/JsonSchemaAppender.cs index 5591a77ef..019b073e0 100644 --- a/src/NJsonSchema/JsonSchemaAppender.cs +++ b/src/NJsonSchema/JsonSchemaAppender.cs @@ -44,7 +44,7 @@ public virtual void AppendSchema(JsonSchema schema, string typeNameHint) if (schema == RootObject) { - throw new ArgumentException("The root schema cannot be appended."); + return; } if (!RootSchema.Definitions.Values.Contains(schema))