Skip to content

Commit

Permalink
Merge pull request #960 from RicoSuter/master
Browse files Browse the repository at this point in the history
Release v9.13.37
  • Loading branch information
RicoSuter committed May 2, 2019
2 parents da99fed + c10d51d commit e442b6e
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.36</Version>
<Version>9.13.37</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.36</Version>
<Version>9.13.37</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.36</Version>
<Version>9.13.37</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
14 changes: 2 additions & 12 deletions src/NJsonSchema.Tests/Generation/DefaultReflectionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,12 @@ public void When_ReferenceTypeNullHandling_is_Null_then_nullability_is_correct()
{ typeof(Dictionary<string, string>), 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]
Expand Down Expand Up @@ -74,17 +69,12 @@ public void When_ReferenceTypeNullHandling_is_NotNull_then_nullability_is_correc
{ typeof(Dictionary<string, string>), 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));
}
}
}
2 changes: 1 addition & 1 deletion src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.36</Version>
<Version>9.13.37</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
26 changes: 19 additions & 7 deletions src/NJsonSchema/Generation/DefaultReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@ public class DefaultReflectionService : IReflectionService
/// <param name="parentAttributes">The parent's attributes (i.e. parameter or property attributes).</param>
/// <param name="settings">The settings.</param>
/// <returns>The <see cref="JsonTypeDescription"/>. </returns>
public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaGeneratorSettings settings)
public JsonTypeDescription GetDescription(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaGeneratorSettings settings)
{
var isNullable = IsNullable(type, parentAttributes, settings);
return GetDescription(type, parentAttributes, settings.DefaultReferenceTypeNullHandling, settings);
}

/// <summary>Creates a <see cref="JsonTypeDescription"/> from a <see cref="Type"/>. </summary>
/// <param name="type">The type. </param>
/// <param name="parentAttributes">The parent's attributes (i.e. parameter or property attributes).</param>
/// <param name="defaultReferenceTypeNullHandling">The default reference type null handling used when no nullability information is available.</param>
/// <param name="settings">The settings.</param>
/// <returns>The <see cref="JsonTypeDescription"/>. </returns>
public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribute> parentAttributes,
ReferenceTypeNullHandling defaultReferenceTypeNullHandling, JsonSchemaGeneratorSettings settings)
{
var isNullable = IsNullable(type, parentAttributes, defaultReferenceTypeNullHandling);

var jsonSchemaTypeAttribute = type.GetTypeInfo().GetCustomAttribute<JsonSchemaTypeAttribute>() ??
parentAttributes?.OfType<JsonSchemaTypeAttribute>().SingleOrDefault();
Expand Down Expand Up @@ -151,9 +163,9 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribu
{
#if !LEGACY
// Remove JsonSchemaTypeAttributes to avoid stack overflows
var typeDescription = GetDescription(type.GenericTypeArguments[0], parentAttributes?.Where(a => !(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;
Expand All @@ -168,9 +180,9 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribu
/// <summary>Checks whether a type is nullable.</summary>
/// <param name="type">The type.</param>
/// <param name="parentAttributes">The parent attributes (e.g. property or parameter attributes).</param>
/// <param name="settings">The settings</param>
/// <param name="defaultReferenceTypeNullHandling">The default reference type null handling used when no nullability information is available.</param>
/// <returns>true if the type can be null.</returns>
public virtual bool IsNullable(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaGeneratorSettings settings)
public virtual bool IsNullable(Type type, IEnumerable<Attribute> parentAttributes, ReferenceTypeNullHandling defaultReferenceTypeNullHandling)
{
var jsonPropertyAttribute = parentAttributes?.OfType<JsonPropertyAttribute>().SingleOrDefault();
if (jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.DisallowNull)
Expand All @@ -186,7 +198,7 @@ public virtual bool IsNullable(Type type, IEnumerable<Attribute> parentAttribute
return true;

var isValueType = type != typeof(string) && type.GetTypeInfo().IsValueType;
return isValueType == false && settings.DefaultReferenceTypeNullHandling == ReferenceTypeNullHandling.Null;
return isValueType == false && defaultReferenceTypeNullHandling == ReferenceTypeNullHandling.Null;
}

/// <summary>Checks whether the given type is a file/binary type.</summary>
Expand Down
13 changes: 11 additions & 2 deletions src/NJsonSchema/Generation/IReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ namespace NJsonSchema.Generation
/// <summary>Provides methods to reflect on types.</summary>
public interface IReflectionService
{
/// <summary>Creates a <see cref="JsonTypeDescription"/> from a <see cref="Type"/>. </summary>
/// <param name="type">The type. </param>
/// <param name="parentAttributes">The parent's attributes (i.e. parameter or property attributes).</param>
/// <param name="defaultReferenceTypeNullHandling">The default reference type null handling.</param>
/// <param name="settings">The settings.</param>
/// <returns>The <see cref="JsonTypeDescription"/>. </returns>
JsonTypeDescription GetDescription(Type type, IEnumerable<Attribute> parentAttributes,
ReferenceTypeNullHandling defaultReferenceTypeNullHandling, JsonSchemaGeneratorSettings settings);

/// <summary>Creates a <see cref="JsonTypeDescription"/> from a <see cref="Type"/>. </summary>
/// <param name="type">The type. </param>
/// <param name="parentAttributes">The parent's attributes (i.e. parameter or property attributes).</param>
Expand All @@ -24,8 +33,8 @@ public interface IReflectionService
/// <summary>Checks whether a type is nullable.</summary>
/// <param name="type">The type.</param>
/// <param name="parentAttributes">The parent attributes (e.g. property or parameter attributes).</param>
/// <param name="settings">The settings</param>
/// <param name="defaultReferenceTypeNullHandling">The default reference type null handling used when no nullability information is available.</param>
/// <returns>true if the type can be null.</returns>
bool IsNullable(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaGeneratorSettings settings);
bool IsNullable(Type type, IEnumerable<Attribute> parentAttributes, ReferenceTypeNullHandling defaultReferenceTypeNullHandling);
}
}
2 changes: 1 addition & 1 deletion src/NJsonSchema/Generation/JsonSchemaGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public JsonSchemaGeneratorSettings()
ExcludedTypeNames = new string[0];
}

/// <summary>Gets or sets the default null handling (if NotNullAttribute and CanBeNullAttribute are missing, default: Null).</summary>
/// <summary>Gets or sets the default reference type null handling when no nullability information is available (if NotNullAttribute and CanBeNullAttribute are missing, default: Null).</summary>
public ReferenceTypeNullHandling DefaultReferenceTypeNullHandling { get; set; }

/// <summary>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).</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/NJsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.0;netstandard2.0;net40;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.36</Version>
<Version>9.13.37</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema/ReferenceTypeNullHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

namespace NJsonSchema
{
/// <summary>Specifies the default null handling for reference types.</summary>
/// <summary>Specifies the default null handling for reference types when no nullability information is available.</summary>
public enum ReferenceTypeNullHandling
{
/// <summary>Reference types can be null by default (C# default).</summary>
/// <summary>Reference types are nullable by default (C# default).</summary>
Null,

/// <summary>Reference types cannot be null by default.</summary>
Expand Down

0 comments on commit e442b6e

Please sign in to comment.