You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public abstract class BaseClass
{
public Guid Id { get; set; }
}
public sealed class ChildA
{
public string Name { get; set; }
}
public sealed class ChildB
{
public int Age { get; set; }
}
public sealed class ClassContainingBase
{
public BaseClass BaseClass { get; set; }
}
If I configure the ClassContainingBase with fluent validator like this: RuleFor(c => c.BaseClass).NotEmpty();
Add the appropriate x-abstract extensions and child schema into the swagger document, it will generate the following:
And this will generate the following c# client (regardless of the generate default values setting, Nswag v13.17.0.0, NJsonSchema v10.8.0):
[Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), "$type")]
[JsonInheritanceAttribute("ChildA", typeof(ChildA))]
[JsonInheritanceAttribute("ChildB", typeof(ChildB))]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.17.0.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
public abstract partial class BaseClass
{
[Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Guid Id { get; set; }
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.17.0.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ChildA : BaseClass
{
[Newtonsoft.Json.JsonProperty("name", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Name { get; set; }
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.17.0.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ChildB : BaseClass
{
[Newtonsoft.Json.JsonProperty("age", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public int Age { get; set; }
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.17.0.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class ClassContainingBase
{
[Newtonsoft.Json.JsonProperty("baseClass", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
public BaseClass BaseClass { get; set; } = new BaseClass();
}
As you can see in the generated ClassContainingBase it tries to instantiate BaseClass, which is clearly not valid.
So I'd expect that the generated client never tries to instantiate an abstract class, regardless of its required setting.
I took a look at the source code of NJsonSchema, and I think the problem is around NJsonSchema.CodeGeneration.CSharp.CSharpValueGenerator's GetDefaultValue method since if the schema.Type.IsObject is true, then it should also check if the property.IsAbstract is true, and if yes then it should return null
Take this object structure:
If I configure the ClassContainingBase with fluent validator like this:
RuleFor(c => c.BaseClass).NotEmpty();
Add the appropriate x-abstract extensions and child schema into the swagger document, it will generate the following:
And this will generate the following c# client (regardless of the generate default values setting, Nswag v13.17.0.0, NJsonSchema v10.8.0):
As you can see in the generated ClassContainingBase it tries to instantiate BaseClass, which is clearly not valid.
So I'd expect that the generated client never tries to instantiate an abstract class, regardless of its required setting.
I took a look at the source code of NJsonSchema, and I think the problem is around NJsonSchema.CodeGeneration.CSharp.CSharpValueGenerator's GetDefaultValue method since if the schema.Type.IsObject is true, then it should also check if the property.IsAbstract is true, and if yes then it should return null
I also created a pull request into NJsonSchema: RicoSuter/NJsonSchema#1570
The text was updated successfully, but these errors were encountered: