diff --git a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj index a38d01ca2..e3b9ce4c5 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;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.36 + 9.13.37 json schema validation generator .net Copyright © Rico Suter, 2018 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 3c923469d..b03e78b22 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;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.36 + 9.13.37 json schema validation generator .net Copyright © Rico Suter, 2018 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 2e0a2f2c9..97e9acf8d 100644 --- a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj +++ b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.36 + 9.13.37 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.Tests/Generation/DefaultReflectionServiceTests.cs b/src/NJsonSchema.Tests/Generation/DefaultReflectionServiceTests.cs index 2c5b9db13..4d0c6c3d4 100644 --- a/src/NJsonSchema.Tests/Generation/DefaultReflectionServiceTests.cs +++ b/src/NJsonSchema.Tests/Generation/DefaultReflectionServiceTests.cs @@ -34,17 +34,12 @@ public void When_ReferenceTypeNullHandling_is_Null_then_nullability_is_correct() { typeof(Dictionary), true }, }; - var settings = new JsonSchemaGeneratorSettings - { - DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.Null - }; - //// Act var svc = new DefaultReflectionService(); //// Assert foreach (var check in checks) - Assert.Equal(check.Value, svc.IsNullable(check.Key, null, settings)); + Assert.Equal(check.Value, svc.IsNullable(check.Key, null, ReferenceTypeNullHandling.Null)); } [Fact] @@ -74,17 +69,12 @@ public void When_ReferenceTypeNullHandling_is_NotNull_then_nullability_is_correc { typeof(Dictionary), false }, }; - var settings = new JsonSchemaGeneratorSettings - { - DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull - }; - //// Act var svc = new DefaultReflectionService(); //// Assert foreach (var check in checks) - Assert.Equal(check.Value, svc.IsNullable(check.Key, null, settings)); + Assert.Equal(check.Value, svc.IsNullable(check.Key, null, ReferenceTypeNullHandling.NotNull)); } } } diff --git a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj index 6173577d0..0142ee72c 100644 --- a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj +++ b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net45 JSON Schema reader, generator and validator for .NET - 9.13.36 + 9.13.37 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema/Generation/DefaultReflectionService.cs b/src/NJsonSchema/Generation/DefaultReflectionService.cs index c16496f76..32cfb11fe 100644 --- a/src/NJsonSchema/Generation/DefaultReflectionService.cs +++ b/src/NJsonSchema/Generation/DefaultReflectionService.cs @@ -28,9 +28,21 @@ public class DefaultReflectionService : IReflectionService /// The parent's attributes (i.e. parameter or property attributes). /// The settings. /// The . - public virtual JsonTypeDescription GetDescription(Type type, IEnumerable parentAttributes, JsonSchemaGeneratorSettings settings) + public JsonTypeDescription GetDescription(Type type, IEnumerable parentAttributes, JsonSchemaGeneratorSettings settings) { - var isNullable = IsNullable(type, parentAttributes, settings); + return GetDescription(type, parentAttributes, settings.DefaultReferenceTypeNullHandling, settings); + } + + /// Creates a from a . + /// The type. + /// The parent's attributes (i.e. parameter or property attributes). + /// The default reference type null handling used when no nullability information is available. + /// The settings. + /// The . + public virtual JsonTypeDescription GetDescription(Type type, IEnumerable parentAttributes, + ReferenceTypeNullHandling defaultReferenceTypeNullHandling, JsonSchemaGeneratorSettings settings) + { + var isNullable = IsNullable(type, parentAttributes, defaultReferenceTypeNullHandling); var jsonSchemaTypeAttribute = type.GetTypeInfo().GetCustomAttribute() ?? parentAttributes?.OfType().SingleOrDefault(); @@ -151,9 +163,9 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable !(a is JsonSchemaTypeAttribute)), settings); + var typeDescription = GetDescription(type.GenericTypeArguments[0], parentAttributes?.Where(a => !(a is JsonSchemaTypeAttribute)), defaultReferenceTypeNullHandling, settings); #else - var typeDescription = GetDescription(type.GetGenericArguments()[0], parentAttributes?.Where(a => !(a is JsonSchemaTypeAttribute)), settings); + var typeDescription = GetDescription(type.GetGenericArguments()[0], parentAttributes?.Where(a => !(a is JsonSchemaTypeAttribute)), defaultReferenceTypeNullHandling, settings); #endif typeDescription.IsNullable = true; return typeDescription; @@ -168,9 +180,9 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerableChecks whether a type is nullable. /// The type. /// The parent attributes (e.g. property or parameter attributes). - /// The settings + /// The default reference type null handling used when no nullability information is available. /// true if the type can be null. - public virtual bool IsNullable(Type type, IEnumerable parentAttributes, JsonSchemaGeneratorSettings settings) + public virtual bool IsNullable(Type type, IEnumerable parentAttributes, ReferenceTypeNullHandling defaultReferenceTypeNullHandling) { var jsonPropertyAttribute = parentAttributes?.OfType().SingleOrDefault(); if (jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.DisallowNull) @@ -186,7 +198,7 @@ public virtual bool IsNullable(Type type, IEnumerable parentAttribute return true; var isValueType = type != typeof(string) && type.GetTypeInfo().IsValueType; - return isValueType == false && settings.DefaultReferenceTypeNullHandling == ReferenceTypeNullHandling.Null; + return isValueType == false && defaultReferenceTypeNullHandling == ReferenceTypeNullHandling.Null; } /// Checks whether the given type is a file/binary type. diff --git a/src/NJsonSchema/Generation/IReflectionService.cs b/src/NJsonSchema/Generation/IReflectionService.cs index 96569493b..383e89682 100644 --- a/src/NJsonSchema/Generation/IReflectionService.cs +++ b/src/NJsonSchema/Generation/IReflectionService.cs @@ -14,6 +14,15 @@ namespace NJsonSchema.Generation /// Provides methods to reflect on types. public interface IReflectionService { + /// Creates a from a . + /// The type. + /// The parent's attributes (i.e. parameter or property attributes). + /// The default reference type null handling. + /// The settings. + /// The . + JsonTypeDescription GetDescription(Type type, IEnumerable parentAttributes, + ReferenceTypeNullHandling defaultReferenceTypeNullHandling, JsonSchemaGeneratorSettings settings); + /// Creates a from a . /// The type. /// The parent's attributes (i.e. parameter or property attributes). @@ -24,8 +33,8 @@ public interface IReflectionService /// Checks whether a type is nullable. /// The type. /// The parent attributes (e.g. property or parameter attributes). - /// The settings + /// The default reference type null handling used when no nullability information is available. /// true if the type can be null. - bool IsNullable(Type type, IEnumerable parentAttributes, JsonSchemaGeneratorSettings settings); + bool IsNullable(Type type, IEnumerable parentAttributes, ReferenceTypeNullHandling defaultReferenceTypeNullHandling); } } \ No newline at end of file diff --git a/src/NJsonSchema/Generation/JsonSchemaGeneratorSettings.cs b/src/NJsonSchema/Generation/JsonSchemaGeneratorSettings.cs index 9f3b37c4c..0d244b8a3 100644 --- a/src/NJsonSchema/Generation/JsonSchemaGeneratorSettings.cs +++ b/src/NJsonSchema/Generation/JsonSchemaGeneratorSettings.cs @@ -44,7 +44,7 @@ public JsonSchemaGeneratorSettings() ExcludedTypeNames = new string[0]; } - /// Gets or sets the default null handling (if NotNullAttribute and CanBeNullAttribute are missing, default: Null). + /// Gets or sets the default reference type null handling when no nullability information is available (if NotNullAttribute and CanBeNullAttribute are missing, default: Null). public ReferenceTypeNullHandling DefaultReferenceTypeNullHandling { get; set; } /// Gets or sets a value indicating whether to generate abstract properties (i.e. interface and abstract properties. Properties may defined multiple times in a inheritance hierarchy, default: false). diff --git a/src/NJsonSchema/NJsonSchema.csproj b/src/NJsonSchema/NJsonSchema.csproj index f22be4403..f2363aaca 100644 --- a/src/NJsonSchema/NJsonSchema.csproj +++ b/src/NJsonSchema/NJsonSchema.csproj @@ -2,7 +2,7 @@ netstandard1.0;netstandard2.0;net40;net45 JSON Schema reader, generator and validator for .NET - 9.13.36 + 9.13.37 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema/ReferenceTypeNullHandling.cs b/src/NJsonSchema/ReferenceTypeNullHandling.cs index ac3cb9740..75ef1a31b 100644 --- a/src/NJsonSchema/ReferenceTypeNullHandling.cs +++ b/src/NJsonSchema/ReferenceTypeNullHandling.cs @@ -8,10 +8,10 @@ namespace NJsonSchema { - /// Specifies the default null handling for reference types. + /// Specifies the default null handling for reference types when no nullability information is available. public enum ReferenceTypeNullHandling { - /// Reference types can be null by default (C# default). + /// Reference types are nullable by default (C# default). Null, /// Reference types cannot be null by default.