diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs index 0d2e717e7..c32c46b9e 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs @@ -1,7 +1,11 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Threading.Tasks; +using Newtonsoft.Json; using NJsonSchema.CodeGeneration.CSharp; +using NJsonSchema.Converters; using Xunit; namespace NJsonSchema.CodeGeneration.Tests.CSharp @@ -38,5 +42,39 @@ public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritan Assert.Contains("public EmptyClassInheritingDictionary CustomDictionary", code); Assert.Contains("public partial class EmptyClassInheritingDictionary : System.Collections.Generic.Dictionary", code); } + + [KnownType(typeof(MyException))] + [JsonConverter(typeof(JsonInheritanceConverter), "kind")] + public class ExceptionBase : Exception + { + public string Foo { get; set; } + } + + public class MyException : ExceptionBase + { + public string Bar { get; set; } + } + + public class ExceptionContainer + { + public ExceptionBase Exception { get; set; } + } + + [Fact] + public async Task When_class_with_discriminator_has_base_class_then_csharp_is_generated_correctly() + { + //// Arrange + var schema = await JsonSchema4.FromTypeAsync(); + var data = schema.ToJson(); + + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco }); + + //// Act + var code = generator.GenerateFile(); + + //// Assert + Assert.Contains("class ExceptionBase : Exception", code); + Assert.Contains("class MyException : ExceptionBase", code); + } } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs index efd9ca53c..f9ee1c67a 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs @@ -89,10 +89,10 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings, public bool GenerateJsonMethods => _settings.GenerateJsonMethods; /// Gets a value indicating whether the class has discriminator property. - public bool HasDiscriminator => !string.IsNullOrEmpty(_schema.Discriminator); + public bool HasDiscriminator => !string.IsNullOrEmpty(_schema.ActualDiscriminator); /// Gets the discriminator property name. - public string Discriminator => _schema.Discriminator; + public string Discriminator => _schema.ActualDiscriminator; /// Gets a value indicating whether the class has a parent class. public bool HasInheritance => _schema.InheritedTypeSchema != null; diff --git a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj index 1eb31633d..617154b15 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj +++ b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj @@ -2,9 +2,9 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.2 + 9.13.3 json schema validation generator .net - Copyright © Rico Suter, 2017 + Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md http://NJsonSchema.org True diff --git a/src/NJsonSchema.CodeGeneration.Tests/InheritanceSerializationTests.cs b/src/NJsonSchema.CodeGeneration.Tests/InheritanceSerializationTests.cs index ca8931522..048488431 100644 --- a/src/NJsonSchema.CodeGeneration.Tests/InheritanceSerializationTests.cs +++ b/src/NJsonSchema.CodeGeneration.Tests/InheritanceSerializationTests.cs @@ -208,7 +208,7 @@ public async Task When_JsonInheritanceConverter_is_set_then_discriminator_field_ //// Act var baseSchema = schema.Properties["Animal"].ActualTypeSchema.ActualSchema; - var discriminator = baseSchema.Discriminator; + var discriminator = baseSchema.ActualDiscriminator; var property = baseSchema.Properties["discriminator"]; var json = schema.ToJson(); @@ -230,10 +230,10 @@ public async Task When_JsonInheritanceConverter_is_set_then_discriminator_mappin var baseSchema = schema.Definitions["SubClass"].ActualSchema; //// Assert - Assert.Equal(3, baseSchema.DiscriminatorObject.Mapping.Count); - Assert.True(baseSchema.DiscriminatorObject.Mapping.ContainsKey("SubClass1")); - Assert.True(baseSchema.DiscriminatorObject.Mapping.ContainsKey("SubClass2")); - Assert.True(baseSchema.DiscriminatorObject.Mapping.ContainsKey("SubClass3")); + Assert.Equal(3, baseSchema.ActualDiscriminatorObject.Mapping.Count); + Assert.True(baseSchema.ActualDiscriminatorObject.Mapping.ContainsKey("SubClass1")); + Assert.True(baseSchema.ActualDiscriminatorObject.Mapping.ContainsKey("SubClass2")); + Assert.True(baseSchema.ActualDiscriminatorObject.Mapping.ContainsKey("SubClass3")); } [Fact] diff --git a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/InheritanceTests.cs b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/InheritanceTests.cs index af1c5585f..8be0fd6fc 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript.Tests/InheritanceTests.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript.Tests/InheritanceTests.cs @@ -1,4 +1,8 @@ -using System.Collections.Generic; +using Newtonsoft.Json; +using NJsonSchema.Converters; +using System; +using System.Collections.Generic; +using System.Runtime.Serialization; using System.Threading.Tasks; using Xunit; @@ -35,5 +39,39 @@ public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritan Assert.DoesNotContain("EmptyClassInheritingDictionary", code); Assert.Contains("customDictionary: { [key: string] : any; } | undefined;", code); } + + [KnownType(typeof(MyException))] + [JsonConverter(typeof(JsonInheritanceConverter), "kind")] + public class ExceptionBase : Exception + { + public string Foo { get; set; } + } + + public class MyException : ExceptionBase + { + public string Bar { get; set; } + } + + public class ExceptionContainer + { + public ExceptionBase Exception { get; set; } + } + + [Fact] + public async Task When_class_with_discriminator_has_base_class_then_csharp_is_generated_correctly() + { + //// Arrange + var schema = await JsonSchema4.FromTypeAsync(); + var data = schema.ToJson(); + + var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 2.0m }); + + //// Act + var code = generator.GenerateFile(); + + //// Assert + Assert.Contains("class ExceptionBase extends Exception", code); + Assert.Contains("class MyException extends ExceptionBase", code); + } } } diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/Models/ClassTemplateModel.cs b/src/NJsonSchema.CodeGeneration.TypeScript/Models/ClassTemplateModel.cs index 6394d01a4..d001ec536 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/Models/ClassTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript/Models/ClassTemplateModel.cs @@ -46,13 +46,13 @@ public ClassTemplateModel(string typeName, string discriminatorName, public string DiscriminatorName { get; } /// Gets a value indicating whether the class has a discriminator property. - public bool HasDiscriminator => !string.IsNullOrEmpty(_schema.Discriminator); + public bool HasDiscriminator => !string.IsNullOrEmpty(_schema.ActualDiscriminator); /// Gets a value indicating whether the class or an inherited class has a discriminator property. - public bool HasBaseDiscriminator => _schema.BaseDiscriminator != null; + public bool HasBaseDiscriminator => _schema.ResponsibleDiscriminatorObject != null; /// Gets the class discriminator property name (may be defined in a inherited class). - public string BaseDiscriminator => _schema.BaseDiscriminator?.PropertyName; + public string BaseDiscriminator => _schema.ResponsibleDiscriminatorObject?.PropertyName; /// Gets a value indicating whether the class has description. public bool HasDescription => !(_schema is JsonProperty) && !string.IsNullOrEmpty(_schema.Description); diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj index d35ca89c8..36377f460 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj +++ b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj @@ -2,9 +2,9 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.2 + 9.13.3 json schema validation generator .net - Copyright © Rico Suter, 2017 + Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md http://NJsonSchema.org True diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs index 149df471b..17ca1e165 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs +++ b/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptTypeResolver.cs @@ -55,7 +55,7 @@ public override string Resolve(JsonSchema4 schema, bool isNullable, string typeN /// The result. public bool SupportsConstructorConversion(JsonSchema4 schema) { - return schema?.ActualSchema.BaseDiscriminator == null; + return schema?.ActualSchema.ResponsibleDiscriminatorObject == null; } private string Resolve(JsonSchema4 schema, string typeNameHint, bool addInterfacePrefix) diff --git a/src/NJsonSchema.CodeGeneration/Models/ClassTemplateModelBase.cs b/src/NJsonSchema.CodeGeneration/Models/ClassTemplateModelBase.cs index 416153c96..c4e385b65 100644 --- a/src/NJsonSchema.CodeGeneration/Models/ClassTemplateModelBase.cs +++ b/src/NJsonSchema.CodeGeneration/Models/ClassTemplateModelBase.cs @@ -41,7 +41,7 @@ protected ClassTemplateModelBase(TypeResolverBase resolver, JsonSchema4 schema, /// Gets the derived class names (discriminator key/type name). public ICollection DerivedClasses => _schema .GetDerivedSchemas(_rootObject) - .Select(p => new DerivedClassModel(p.Value, p.Key, _schema.ActualSchema.BaseDiscriminator, _resolver)) + .Select(p => new DerivedClassModel(p.Value, p.Key, _schema.ActualSchema.ResponsibleDiscriminatorObject, _resolver)) .ToList(); /// The model of a derived class. diff --git a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj index ea541238a..51c26fe10 100644 --- a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj +++ b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj @@ -2,9 +2,9 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.2 + 9.13.3 json schema validation generator .net - Copyright © Rico Suter, 2017 + Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md http://NJsonSchema.org True diff --git a/src/NJsonSchema.Tests/Generation/InheritanceTests.cs b/src/NJsonSchema.Tests/Generation/InheritanceTests.cs index 9b1157e12..7e43defa8 100644 --- a/src/NJsonSchema.Tests/Generation/InheritanceTests.cs +++ b/src/NJsonSchema.Tests/Generation/InheritanceTests.cs @@ -211,7 +211,7 @@ public async Task When_discriminator_is_externally_defined_then_it_is_generated( Assert.True(schema.Definitions.ContainsKey(nameof(BCommonThing))); var baseSchema = schema.Definitions[nameof(CommonThingBase)]; - Assert.Equal("discriminator", baseSchema.Discriminator); + Assert.Equal("discriminator", baseSchema.ActualDiscriminator); } [Fact] @@ -339,5 +339,43 @@ public async Task When_class_inherits_from_dictionary_then_allOf_contains_base_d Assert.True(bar.AllOf.First().IsDictionary); // base class (dictionary) Assert.True(bar.AllOf.Last().ActualProperties.Any()); // actual class } + + [KnownType(typeof(MyException))] + [JsonConverter(typeof(JsonInheritanceConverter), "kind")] + public class ExceptionBase : Exception + { + public string Foo { get; set; } + } + + public class MyException : ExceptionBase + { + public string Bar { get; set; } + } + + public class ExceptionContainer + { + public ExceptionBase Exception { get; set; } + } + + [Fact] + public async Task When_class_with_discriminator_has_base_class_then_mapping_is_placed_in_type_schema_and_not_root() + { + //// Arrange + var settings = new JsonSchemaGeneratorSettings + { + SchemaType = SchemaType.OpenApi3 + }; + + //// Act + var schema = await JsonSchema4.FromTypeAsync(settings); + var json = schema.ToJson(); + + //// Assert + var exceptionBase = schema.Definitions["ExceptionBase"]; + + Assert.Null(exceptionBase.DiscriminatorObject); + Assert.NotNull(exceptionBase.ActualTypeSchema.DiscriminatorObject); + Assert.True(exceptionBase.ActualTypeSchema.DiscriminatorObject.Mapping.ContainsKey("MyException")); + } } } diff --git a/src/NJsonSchema.Tests/Infrastructure/ReflectionExtensionsTests.cs b/src/NJsonSchema.Tests/Infrastructure/ReflectionExtensionsTests.cs new file mode 100644 index 000000000..275cfc143 --- /dev/null +++ b/src/NJsonSchema.Tests/Infrastructure/ReflectionExtensionsTests.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using NJsonSchema.Infrastructure; +using Xunit; + +namespace NJsonSchema.Tests.Infrastructure +{ + public class ReflectionExtensionsTests + { + public class MyType : IEquatable, IEnumerable + { + public bool Equals(MyType other) + { + throw new NotImplementedException(); + } + + public IEnumerator GetEnumerator() + { + throw new NotImplementedException(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + throw new NotImplementedException(); + } + } + + [Fact] + public void When_type_implements_equatable_then_item_type_can_be_retrieved() + { + //// Act + var itemType = typeof(MyType).GetEnumerableItemType(); + + //// Assert + Assert.Equal(typeof(int), itemType); + } + } +} \ No newline at end of file diff --git a/src/NJsonSchema.Tests/Serialization/DiscriminatorSerializationTests.cs b/src/NJsonSchema.Tests/Serialization/DiscriminatorSerializationTests.cs index 246a0f676..9c04619a2 100644 --- a/src/NJsonSchema.Tests/Serialization/DiscriminatorSerializationTests.cs +++ b/src/NJsonSchema.Tests/Serialization/DiscriminatorSerializationTests.cs @@ -45,7 +45,7 @@ public async Task When_discriminator_object_is_set_then_schema_is_correctly_seri Assert.Equal(json, json2); - Assert.Equal(schema2.Definitions["Foo"], schema2.DiscriminatorObject.Mapping["Bar"].ActualSchema); + Assert.Equal(schema2.Definitions["Foo"], schema2.ActualDiscriminatorObject.Mapping["Bar"].ActualSchema); } [Fact] diff --git a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj index cf844abe7..d96c56886 100644 --- a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj +++ b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj @@ -2,9 +2,9 @@ netstandard1.3;netstandard2.0;net45 JSON Schema reader, generator and validator for .NET - 9.13.2 + 9.13.3 json schema validation generator .net - Copyright © Rico Suter, 2017 + Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md http://NJsonSchema.org True diff --git a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs index fbc8b3483..92e228e1d 100644 --- a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs +++ b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs @@ -330,8 +330,8 @@ protected virtual async Task GenerateObjectAsync(Type type, JsonTypeDescription typeDescription, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { schemaResolver.AddSchema(type, false, schema); - var rootSchema = schema; + var rootSchema = schema; var actualSchema = await GenerateInheritanceAsync(type, schema, schemaResolver).ConfigureAwait(false); if (actualSchema != null) { @@ -348,7 +348,7 @@ protected virtual async Task GenerateObjectAsync(Type type, schema.Description = await type.GetTypeInfo().GetDescriptionAsync(type.GetTypeInfo().GetCustomAttributes()).ConfigureAwait(false); schema.IsAbstract = type.GetTypeInfo().IsAbstract; - GenerateInheritanceDiscriminator(type, rootSchema); + GenerateInheritanceDiscriminator(type, rootSchema, schema); await GenerateKnownTypesAsync(type, schemaResolver).ConfigureAwait(false); @@ -716,9 +716,9 @@ private async Task GenerateInheritanceAsync(Type type, JsonSchema4 if (!typeDescription.IsDictionary && !type.IsArray) { await GeneratePropertiesAsync(baseType, schema, schemaResolver).ConfigureAwait(false); - await GenerateInheritanceAsync(baseType, schema, schemaResolver).ConfigureAwait(false); + var actualSchema = await GenerateInheritanceAsync(baseType, schema, schemaResolver).ConfigureAwait(false); - GenerateInheritanceDiscriminator(baseType, schema); + GenerateInheritanceDiscriminator(baseType, schema, actualSchema ?? schema); } } else @@ -781,9 +781,9 @@ private async Task GenerateInheritanceAsync(Type type, JsonSchema4 !typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(i.GetTypeInfo())) { await GeneratePropertiesAsync(i, schema, schemaResolver).ConfigureAwait(false); - await GenerateInheritanceAsync(i, schema, schemaResolver).ConfigureAwait(false); + var actualSchema = await GenerateInheritanceAsync(i, schema, schemaResolver).ConfigureAwait(false); - GenerateInheritanceDiscriminator(i, schema); + GenerateInheritanceDiscriminator(i, schema, actualSchema ?? schema); } } } @@ -791,7 +791,7 @@ private async Task GenerateInheritanceAsync(Type type, JsonSchema4 return null; } - private void GenerateInheritanceDiscriminator(Type type, JsonSchema4 schema) + private void GenerateInheritanceDiscriminator(Type type, JsonSchema4 schema, JsonSchema4 typeSchema) { if (!Settings.GetActualFlattenInheritanceHierarchy(type)) { @@ -801,7 +801,7 @@ private void GenerateInheritanceDiscriminator(Type type, JsonSchema4 schema) var discriminatorName = TryGetInheritanceDiscriminatorName(discriminatorConverter); // Existing property can be discriminator only if it has String type - if (schema.Properties.TryGetValue(discriminatorName, out JsonProperty existingProperty) && + if (typeSchema.Properties.TryGetValue(discriminatorName, out JsonProperty existingProperty) && (existingProperty.Type & JsonObjectType.String) == 0) { throw new InvalidOperationException("The JSON discriminator property '" + discriminatorName + "' must be a string property on type '" + type.FullName + "' (it is recommended to not implement the discriminator property at all)."); @@ -813,8 +813,8 @@ private void GenerateInheritanceDiscriminator(Type type, JsonSchema4 schema) PropertyName = discriminatorName }; - schema.DiscriminatorObject = discriminator; - schema.Properties[discriminatorName] = new JsonProperty + typeSchema.DiscriminatorObject = discriminator; + typeSchema.Properties[discriminatorName] = new JsonProperty { Type = JsonObjectType.String, IsRequired = true @@ -822,7 +822,7 @@ private void GenerateInheritanceDiscriminator(Type type, JsonSchema4 schema) } else { - var baseDiscriminator = schema.BaseDiscriminator; + var baseDiscriminator = schema.ResponsibleDiscriminatorObject ?? schema.ActualTypeSchema.ResponsibleDiscriminatorObject; baseDiscriminator?.AddMapping(type, schema); } } diff --git a/src/NJsonSchema/Infrastructure/ReflectionExtensions.cs b/src/NJsonSchema/Infrastructure/ReflectionExtensions.cs index 286b6cea9..ac1f1a1c2 100644 --- a/src/NJsonSchema/Infrastructure/ReflectionExtensions.cs +++ b/src/NJsonSchema/Infrastructure/ReflectionExtensions.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -128,9 +129,13 @@ public static Type GetEnumerableItemType(this Type type) foreach (var iface in type.GetTypeInfo().GetInterfaces()) #endif { - itemType = GetEnumerableItemType(iface); - if (itemType != null) - return itemType; + if (typeof(IEnumerable).GetTypeInfo() + .IsAssignableFrom(iface.GetTypeInfo())) + { + itemType = GetEnumerableItemType(iface); + if (itemType != null) + return itemType; + } } } return itemType; diff --git a/src/NJsonSchema/JsonProperty.cs b/src/NJsonSchema/JsonProperty.cs index 90ee25385..aae6711f5 100644 --- a/src/NJsonSchema/JsonProperty.cs +++ b/src/NJsonSchema/JsonProperty.cs @@ -84,7 +84,7 @@ public bool IsRequired /// Gets a value indicating whether the property is an inheritance discriminator. [JsonIgnore] - public bool IsInheritanceDiscriminator => ParentSchema.Discriminator == Name; + public bool IsInheritanceDiscriminator => ParentSchema.ActualDiscriminator == Name; /// Determines whether the specified property null handling is nullable. /// The schema type. diff --git a/src/NJsonSchema/JsonSchema4.Serialization.cs b/src/NJsonSchema/JsonSchema4.Serialization.cs index 470df59b1..40c39d1bf 100644 --- a/src/NJsonSchema/JsonSchema4.Serialization.cs +++ b/src/NJsonSchema/JsonSchema4.Serialization.cs @@ -60,7 +60,11 @@ internal void OnDeserialized(StreamingContext ctx) Initialize(); } - /// Gets or sets the discriminator property (Swagger only). + /// Gets the discriminator property (Swagger only). + [JsonIgnore] + public string ActualDiscriminator => ActualTypeSchema.Discriminator; + + /// Gets or sets the discriminator property (Swagger only, should not be used in internal tooling). [JsonIgnore] public string Discriminator { @@ -75,11 +79,17 @@ public string Discriminator }; } else + { DiscriminatorObject = null; + } } } - /// Gets or sets the discriminator (OpenApi only). + /// Gets the actual resolved discriminator of this schema (no inheritance, OpenApi only). + [JsonIgnore] + public OpenApiDiscriminator ActualDiscriminatorObject => DiscriminatorObject ?? ActualTypeSchema.DiscriminatorObject; + + /// Gets or sets the discriminator of this schema (OpenApi only). [JsonIgnore] public OpenApiDiscriminator DiscriminatorObject { get; set; } @@ -96,7 +106,7 @@ internal object DiscriminatorRaw } set { - if (value is String) + if (value is string) Discriminator = (string)value; else if (value != null) DiscriminatorObject = ((JObject)value).ToObject(); diff --git a/src/NJsonSchema/JsonSchema4.cs b/src/NJsonSchema/JsonSchema4.cs index 36fe466a6..40e9e57f3 100644 --- a/src/NJsonSchema/JsonSchema4.cs +++ b/src/NJsonSchema/JsonSchema4.cs @@ -270,7 +270,8 @@ public bool Inherits(JsonSchema4 schema) /// Gets the discriminator or discriminator of an inherited schema (or null). [JsonIgnore] - public OpenApiDiscriminator BaseDiscriminator => DiscriminatorObject ?? InheritedSchema?.ActualSchema.BaseDiscriminator; + public OpenApiDiscriminator ResponsibleDiscriminatorObject => + ActualDiscriminatorObject ?? InheritedSchema?.ActualSchema.ResponsibleDiscriminatorObject; /// Gets all properties of this schema (i.e. all direct properties and properties from the schemas in allOf which do not have a type). /// Used for code generation. diff --git a/src/NJsonSchema/NJsonSchema.csproj b/src/NJsonSchema/NJsonSchema.csproj index 49543463d..592bb25ba 100644 --- a/src/NJsonSchema/NJsonSchema.csproj +++ b/src/NJsonSchema/NJsonSchema.csproj @@ -2,9 +2,9 @@ netstandard1.0;netstandard2.0;net40;net45 JSON Schema reader, generator and validator for .NET - 9.13.2 + 9.13.3 json schema validation generator .net - Copyright © Rico Suter, 2017 + Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md http://NJsonSchema.org True