Skip to content

Commit

Permalink
Merge pull request #950 from RicoSuter/master
Browse files Browse the repository at this point in the history
Release v9.13.31
  • Loading branch information
RicoSuter authored Apr 18, 2019
2 parents 3095a52 + ffdcaf9 commit b03ce1a
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 10 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.30</Version>
<Version>9.13.31</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.30</Version>
<Version>9.13.31</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.30</Version>
<Version>9.13.31</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
19 changes: 18 additions & 1 deletion src/NJsonSchema.Tests/Generation/DictionaryTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using NJsonSchema.Generation;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -50,5 +51,21 @@ public async Task When_value_type_is_nullable_then_json_schema_is_nullable()
Assert.True(schema.Properties["Mapping3"].IsDictionary);
Assert.True(schema.Properties["Mapping3"].AdditionalPropertiesSchema.IsNullable(SchemaType.JsonSchema));
}

[Fact]
public async Task When_value_type_is_nullable_then_json_schema_is_nullable_Swagger2()
{
//// Act
var schema = await JsonSchema4.FromTypeAsync<EnumKeyDictionaryTest>(new JsonSchemaGeneratorSettings
{
SchemaType = SchemaType.Swagger2,
GenerateCustomNullableProperties = true
});
var data = schema.ToJson();

//// Assert
Assert.True(schema.Properties["Mapping3"].IsDictionary);
Assert.True(schema.Properties["Mapping3"].AdditionalPropertiesSchema.IsNullable(SchemaType.Swagger2));
}
}
}
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.30</Version>
<Version>9.13.31</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
10 changes: 8 additions & 2 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,11 @@ public virtual async Task<TSchemaType> GenerateWithReferenceAndNullabilityAsync<
schema.OneOf.Add(new JsonSchema4 { Type = JsonObjectType.Null });
}
else
{
schema.Type = schema.Type | JsonObjectType.Null;
}
}
else if (Settings.SchemaType == SchemaType.OpenApi3)
else if (Settings.SchemaType == SchemaType.OpenApi3 || Settings.GenerateCustomNullableProperties)
{
schema.IsNullableRaw = isNullable;
}
Expand All @@ -265,9 +267,13 @@ public virtual async Task<TSchemaType> GenerateWithReferenceAndNullabilityAsync<
if (isNullable)
{
if (Settings.SchemaType == SchemaType.JsonSchema)
{
referencingSchema.OneOf.Add(new JsonSchema4 { Type = JsonObjectType.Null });
else if (Settings.SchemaType == SchemaType.OpenApi3)
}
else if (Settings.SchemaType == SchemaType.OpenApi3 || Settings.GenerateCustomNullableProperties)
{
referencingSchema.IsNullableRaw = true;
}
}

// See https://github.com/RSuter/NJsonSchema/issues/531
Expand Down
3 changes: 3 additions & 0 deletions src/NJsonSchema/Generation/JsonSchemaGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ public JsonSchemaGeneratorSettings()
[JsonIgnore]
public ICollection<ISchemaProcessor> SchemaProcessors { get; } = new Collection<ISchemaProcessor>();

/// <summary>Gets or sets a value indicating whether to generate x-nullable properties (Swagger 2 only).</summary>
public bool GenerateCustomNullableProperties { get; set; }

/// <summary>Gets or sets the contract resolver.</summary>
/// <remarks><see cref="DefaultPropertyNameHandling"/> will be ignored.</remarks>
[JsonIgnore]
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema/JsonSchema4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public IDictionary<string, JsonProperty> ActualProperties
[JsonProperty("x-abstract", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public bool IsAbstract { get; set; }

/// <summary>Gets or sets a value indicating whether the schema is nullable (Open API only).</summary>
/// <summary>Gets or sets a value indicating whether the schema is nullable (native in Open API 'nullable', custom in Swagger 'x-nullable').</summary>
[JsonProperty("x-nullable", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public bool? IsNullableRaw { get; set; }

Expand Down Expand Up @@ -729,7 +729,7 @@ public JsonSchema4 AdditionalPropertiesSchema
/// <returns>true if the type can be null.</returns>
public virtual bool IsNullable(SchemaType schemaType)
{
if (schemaType == SchemaType.OpenApi3 && IsNullableRaw == true)
if (IsNullableRaw == true)
return true;

if (IsEnumeration && Enumeration.Contains(null))
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.30</Version>
<Version>9.13.31</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

0 comments on commit b03ce1a

Please sign in to comment.