From 30c328ea92bcfaa21b8d62ffd15bc4621828f631 Mon Sep 17 00:00:00 2001 From: Rico Suter Date: Tue, 13 Nov 2018 15:44:54 +0100 Subject: [PATCH 1/2] Add support for JsonInheritanceAttribute in schema generator (#813) --- .../Generation/JsonSchemaGenerator.cs | 53 ++++++++++++------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs index 1447b11c0..adf84f9bb 100644 --- a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs +++ b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs @@ -350,8 +350,7 @@ protected virtual async Task GenerateObjectAsync(Type type, GenerateInheritanceDiscriminator(type, rootSchema); - if (Settings.GenerateKnownTypes) - await GenerateKnownTypesAsync(type, schemaResolver).ConfigureAwait(false); + await GenerateKnownTypesAsync(type, schemaResolver).ConfigureAwait(false); if (Settings.GenerateXmlObjects) schema.GenerateXmlObjectForType(type); @@ -644,30 +643,48 @@ protected virtual string[] GetTypeProperties(Type type) private async Task GenerateKnownTypesAsync(Type type, JsonSchemaResolver schemaResolver) { - var knownTypeAttributes = type.GetTypeInfo() - .GetCustomAttributes(Settings.GetActualFlattenInheritanceHierarchy(type)) // Known types of inherited classes will be generated later (in GenerateInheritanceAsync) - .Where(a => a.GetType().Name == "KnownTypeAttribute") - .OfType(); + var attributes = type.GetTypeInfo() + .GetCustomAttributes(Settings.GetActualFlattenInheritanceHierarchy(type)); - foreach (dynamic attribute in knownTypeAttributes) + if (Settings.GenerateKnownTypes) { - if (attribute.Type != null) - await AddKnownTypeAsync(attribute.Type, schemaResolver).ConfigureAwait(false); - else if (attribute.MethodName != null) + var knownTypeAttributes = attributes + // Known types of inherited classes will be generated later (in GenerateInheritanceAsync) + .Where(a => a.GetType().IsAssignableTo("KnownTypeAttribute", TypeNameStyle.Name)) + .OfType(); + + foreach (dynamic attribute in knownTypeAttributes) { - var methodInfo = type.GetRuntimeMethod((string)attribute.MethodName, new Type[0]); - if (methodInfo != null) + if (attribute.Type != null) + await AddKnownTypeAsync(attribute.Type, schemaResolver).ConfigureAwait(false); + else if (attribute.MethodName != null) { - var knownTypes = methodInfo.Invoke(null, null) as IEnumerable; - if (knownTypes != null) + var methodInfo = type.GetRuntimeMethod((string)attribute.MethodName, new Type[0]); + if (methodInfo != null) { - foreach (var knownType in knownTypes) - await AddKnownTypeAsync(knownType, schemaResolver).ConfigureAwait(false); + var knownTypes = methodInfo.Invoke(null, null) as IEnumerable; + if (knownTypes != null) + { + foreach (var knownType in knownTypes) + await AddKnownTypeAsync(knownType, schemaResolver).ConfigureAwait(false); + } } } + else + throw new ArgumentException($"A KnownType attribute on {type.FullName} does not specify a type or a method name.", nameof(type)); + } + } + + foreach (var jsonConverterAttribute in attributes + .Where(a => a.GetType().IsAssignableTo("JsonInheritanceAttribute", TypeNameStyle.Name))) + { + var knownType = ReflectionExtensions.TryGetPropertyValue( + jsonConverterAttribute, "Type", null); + + if (knownType != null) + { + await AddKnownTypeAsync(knownType, schemaResolver).ConfigureAwait(false); } - else - throw new ArgumentException($"A KnownType attribute on {type.FullName} does not specify a type or a method name.", nameof(type)); } } From a0d077a24cba0fb960360551c09fc0bf955ccc0a Mon Sep 17 00:00:00 2001 From: Rico Suter Date: Tue, 13 Nov 2018 15:47:25 +0100 Subject: [PATCH 2/2] v9.12.2 --- .../NJsonSchema.CodeGeneration.CSharp.csproj | 2 +- .../NJsonSchema.CodeGeneration.TypeScript.csproj | 2 +- .../NJsonSchema.CodeGeneration.csproj | 2 +- src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj | 2 +- src/NJsonSchema/NJsonSchema.csproj | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj index 21544e6b0..766e7d174 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj +++ b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj @@ -2,7 +2,7 @@ netstandard1.3;net451 JSON Schema reader, generator and validator for .NET - 9.12.1 + 9.12.2 json schema validation generator .net Copyright © Rico Suter, 2017 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj index 7439a0614..b68116630 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj +++ b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj @@ -2,7 +2,7 @@ netstandard1.3;net451 JSON Schema reader, generator and validator for .NET - 9.12.1 + 9.12.2 json schema validation generator .net Copyright © Rico Suter, 2017 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj index 1684657b1..fc3466ce0 100644 --- a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj +++ b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj @@ -2,7 +2,7 @@ netstandard1.3;net451 JSON Schema reader, generator and validator for .NET - 9.12.1 + 9.12.2 json schema validation generator .net Copyright © Rico Suter, 2017 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj index 1c715645f..532a422e8 100644 --- a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj +++ b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj @@ -2,7 +2,7 @@ netstandard1.3;net45 JSON Schema reader, generator and validator for .NET - 9.12.1 + 9.12.2 json schema validation generator .net Copyright © Rico Suter, 2017 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema/NJsonSchema.csproj b/src/NJsonSchema/NJsonSchema.csproj index 3ec7ffdc2..f0b6a52d2 100644 --- a/src/NJsonSchema/NJsonSchema.csproj +++ b/src/NJsonSchema/NJsonSchema.csproj @@ -2,7 +2,7 @@ netstandard1.0;net40;net45 JSON Schema reader, generator and validator for .NET - 9.12.1 + 9.12.2 json schema validation generator .net Copyright © Rico Suter, 2017 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md