Skip to content

Commit

Permalink
Issue in generation of C# classes with inheritance fixed (#1319)
Browse files Browse the repository at this point in the history
* Bug in JsonReferenceResolver fixed which prevented the referencing of definitions in other files.

* Issue in the JsonSchemaAppender for the generation of classes fixed when the definitions reference the root schema. When the passed schema is already the root schema the method returns immediately instead of throwing an exception.

Issue in the CSharpTypeResolver for the generation of base classes without properties fixed. When classes don't contain properties but contain the "descriminator" attribute they should still be generated.

Co-authored-by: Egli, Florian {DMMA~Rotkreuz-Tro} <[email protected]>
  • Loading branch information
twodogmbh and Egli, Florian {DMMA~Rotkreuz-Tro} authored Feb 25, 2021
1 parent 48946d7 commit d8f5302
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<None Remove="References\A.json" />
<None Remove="References\Animal.json" />
<None Remove="References\B.json" />
<None Remove="References\C.json" />
<None Remove="References\D.json" />
Expand All @@ -16,6 +17,9 @@
<Content Include="References\A.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\Animal.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\B.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
25 changes: 25 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Animal.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/JsonSchemaAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit d8f5302

Please sign in to comment.