Skip to content

Commit

Permalink
Merge pull request #793 from RSuter/master
Browse files Browse the repository at this point in the history
Release v9.11.1
  • Loading branch information
RicoSuter authored Oct 10, 2018
2 parents 0ac88d5 + fdfa714 commit a12a86f
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.11.0</Version>
<Version>9.11.1</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</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;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.11.0</Version>
<Version>9.11.1</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</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;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.11.0</Version>
<Version>9.11.1</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
419 changes: 299 additions & 120 deletions src/NJsonSchema.Tests/Generation/XmlObjectTests.cs

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions src/NJsonSchema.Tests/Validation/OneOfValidationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Threading.Tasks;
using Xunit;

namespace NJsonSchema.Tests.Validation
{
public class OneOfValidationTests
{
private string example_schema = @"{
""type"": ""object"",
""properties"": {
""A"": { ""type"": ""string"" },
""B"": { ""type"": ""integer"" },
""C"": { ""type"": ""number"" }
},
""required"": [""A""],
""additionalProperties"": false,
""oneOf"": [
{
""required"": [""B""]
},
{
""required"": [""C""]
}
]
}";

[Fact]
public async Task When_has_one_of_then_it_is_validated_correctly()
{
var schema = await JsonSchema4.FromJsonAsync(example_schema);
var matches = new string[] { @"{ ""A"": ""string"", ""B"": 3 }",
@"{ ""A"": ""string"", ""C"": 2 }" };

foreach (var match in matches)
{
var errors = schema.Validate(match);

Assert.Equal(0, errors.Count);
}
}

[Fact]
public async Task When_does_not_have_one_of_then_it_is_invalid()
{
var schema = await JsonSchema4.FromJsonAsync(example_schema);

var errors = schema.Validate(@"{ ""A"": ""string"" }");

Assert.Equal(1, errors.Count);
}

[Fact]
public async Task When_has_more_than_one_of_then_it_is_invalid()
{
var schema = await JsonSchema4.FromJsonAsync(example_schema);

var errors = schema.Validate(@"{ ""A"": ""string"", ""B"": 1, ""C"": 2 }");

Assert.Equal(1, errors.Count);
}
}
}
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;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.11.0</Version>
<Version>9.11.1</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
15 changes: 9 additions & 6 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
Expand All @@ -20,6 +14,12 @@
using NJsonSchema.Converters;
using NJsonSchema.Generation.TypeMappers;
using NJsonSchema.Infrastructure;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace NJsonSchema.Generation
{
Expand Down Expand Up @@ -450,6 +450,9 @@ private async Task GenerateArray<TSchemaType>(
if (Settings.GenerateXmlObjects)
s.GenerateXmlObjectForItemType(itemType);
}).ConfigureAwait(false);

if (Settings.GenerateXmlObjects)
schema.GenerateXmlObjectForArrayType(type);
}
else
schema.Item = JsonSchema4.CreateAnySchema();
Expand Down
44 changes: 39 additions & 5 deletions src/NJsonSchema/Infrastructure/XmlObjectExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,32 @@ public static void GenerateXmlObjectForType(this JsonSchema4 schema, Type type)
GenerateXmlObject(xmlTypeAttribute.TypeName, xmlTypeAttribute.Namespace, false, false, schema);
}
}

/// <summary>Generates an XML object for a JSON Schema definition.</summary>
/// <param name="schema">The JSON Schema</param>
/// <param name="type">The array type</param>
public static void GenerateXmlObjectForArrayType(this JsonSchema4 schema, Type type)
{
if (schema.IsArray && schema.ParentSchema == null)
{
GenerateXmlObject($@"ArrayOf{schema.Item.Xml.Name}", null, true, false, schema);
}
}

/// <summary>Generates XMLObject structure for an array with primitive types</summary>
/// <param name="schema">The JSON Schema of the item.</param>
/// <param name="type">The item type.</param>
public static void GenerateXmlObjectForItemType(this JsonSchema4 schema, Type type)
{
//Is done all the time for XML to be able to get type name as the element name if not there was an attribute defined since earlier
if (schema.Xml == null)
GenerateXmlObject(type.Name, null, false, false, schema);
// Is done all the time for XML to be able to get type name as the element name if not there was an attribute defined since earlier
var attributes = type.GetTypeInfo().GetCustomAttributes().ToList();
dynamic xmlTypeAttribute = attributes.TryGetIfAssignableTo("System.Xml.Serialization.XmlTypeAttribute");

var itemName = GetXmlItemName(type);
if (xmlTypeAttribute != null)
itemName = xmlTypeAttribute.TypeName;

GenerateXmlObject(itemName, null, false, false, schema);
}

/// <summary>Generates XMLObject structure for a property.</summary>
Expand Down Expand Up @@ -87,8 +105,8 @@ public static void GenerateXmlObjectForProperty(this JsonProperty propertySchema
xmlNamespace = xmlAttribute.Namespace;
}

//Due to that the JSON Reference is used, the xml name from the referenced type will be copied to the property.
//We need to ensure that the property name is preserved
// Due to that the JSON Reference is used, the xml name from the referenced type will be copied to the property.
// We need to ensure that the property name is preserved
if (string.IsNullOrEmpty(xmlName) && propertySchema.Type == JsonObjectType.None)
{
var referencedTypeAttributes = type.GetTypeInfo().GetCustomAttributes();
Expand All @@ -114,5 +132,21 @@ private static void GenerateXmlObject(string name, string @namespace, bool wrapp
Attribute = isAttribute
};
}

/// <summary>type.Name is used int will return Int32, string will return String etc.
/// These are not valid with how the XMLSerializer performs.</summary>
private static string GetXmlItemName(Type type)
{
if (type == typeof(int))
return "int";
else if (type == typeof(string))
return "string";
else if (type == typeof(double))
return "double";
else if (type == typeof(decimal))
return "decimal";
else
return type.Name;
}
}
}
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;net40;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.11.0</Version>
<Version>9.11.1</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
21 changes: 20 additions & 1 deletion src/NJsonSchema/Validation/JsonSchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private void ValidateProperties(JToken token, JsonSchema4 schema, string propert

foreach (var propertyInfo in schema.Properties)
{
var newPropertyPath = !string.IsNullOrEmpty(propertyPath) ? propertyPath + "." + propertyInfo.Key : propertyInfo.Key;
var newPropertyPath = GetPropertyPath(propertyPath, propertyInfo.Key);

var property = obj?.Property(propertyInfo.Key);
if (property != null)
Expand All @@ -398,6 +398,20 @@ private void ValidateProperties(JToken token, JsonSchema4 schema, string propert
errors.Add(new ValidationError(ValidationErrorKind.PropertyRequired, propertyInfo.Key, newPropertyPath, token, schema));
}

// Properties may be required in a schema without being specified as a property.
foreach (var requiredProperty in schema.RequiredProperties)
{
if (schema.Properties.ContainsKey(requiredProperty))
{
// The property has already been checked.
continue;
}

var newPropertyPath = GetPropertyPath(propertyPath, requiredProperty);
if (obj?.Property(requiredProperty) == null)
errors.Add(new ValidationError(ValidationErrorKind.PropertyRequired, requiredProperty, newPropertyPath, token, schema));
}

if (obj != null)
{
var properties = obj.Properties().ToList();
Expand All @@ -412,6 +426,11 @@ private void ValidateProperties(JToken token, JsonSchema4 schema, string propert
}
}

private string GetPropertyPath(string propertyPath, string propertyName)
{
return !string.IsNullOrEmpty(propertyPath) ? propertyPath + "." + propertyName : propertyName;
}

private void ValidateMaxProperties(JToken token, IList<JProperty> properties, JsonSchema4 schema, string propertyName, string propertyPath, List<ValidationError> errors)
{
if (schema.MaxProperties > 0 && properties.Count() > schema.MaxProperties)
Expand Down

0 comments on commit a12a86f

Please sign in to comment.