Skip to content

Commit

Permalink
Merge pull request #781 from RSuter/master
Browse files Browse the repository at this point in the history
Release v9.10.74
  • Loading branch information
RicoSuter authored Sep 21, 2018
2 parents f075f31 + 14e2190 commit 873ceae
Show file tree
Hide file tree
Showing 27 changed files with 489 additions and 236 deletions.
1 change: 1 addition & 0 deletions build/04_RunTests.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dotnet test "%~dp0/../src\NJsonSchema.Benchmark\NJsonSchema.Benchmark.csproj" -c Release || goto :error
dotnet test "%~dp0/../src\NJsonSchema.Tests\NJsonSchema.Tests.csproj" -c Release || goto :error
dotnet test "%~dp0/../src\NJsonSchema.CodeGeneration.Tests\NJsonSchema.CodeGeneration.Tests.csproj" -c Release || goto :error
dotnet test "%~dp0/../src\NJsonSchema.CodeGeneration.CSharp.Tests\NJsonSchema.CodeGeneration.CSharp.Tests.csproj" -c Release || goto :error
Expand Down
25 changes: 25 additions & 0 deletions src/NJsonSchema.Benchmark/GeneratorPerformance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using BenchmarkDotNet.Attributes;
using NJsonSchema.Tests.Generation;
using System.Threading.Tasks;
using NJsonSchema.Infrastructure;

using static NJsonSchema.Tests.Generation.XmlDocTests;

namespace NJsonSchema.Benchmark
{
public class GeneratorPerformance
{
private readonly XmlDocTests _tests;

public GeneratorPerformance()
{
_tests = new XmlDocTests();
}

[Benchmark]
public async Task XmlDocTests()
{
await typeof(ClassWithInheritdoc).GetMethod("Bar").GetXmlSummaryAsync();
}
}
}
65 changes: 65 additions & 0 deletions src/NJsonSchema.Benchmark/GeneratorPerformanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Diagnostics;
using System.Threading.Tasks;
using NBench;
using NJsonSchema.Infrastructure;
using Pro.NBench.xUnit.XunitExtensions;
using Xunit;
using Xunit.Abstractions;

namespace NJsonSchema.Benchmark
{
public class GeneratorPerformanceTests
{
private readonly GeneratorPerformance _generatorPerformance = new GeneratorPerformance();
private Counter _counter;

public GeneratorPerformanceTests(ITestOutputHelper output)
{
Trace.Listeners.Clear();
Trace.Listeners.Add(new XunitTraceListener(output));
}

[PerfSetup]
#pragma warning disable xUnit1013 // Public method should be marked as test
public void Setup(BenchmarkContext context)
#pragma warning restore xUnit1013 // Public method should be marked as test
{
_counter = context.GetCounter("Iterations");
}

/// <summary>
/// Ensure that we can serialise at least 200 times per second (5ms).
/// </summary>
[NBenchFact]
[PerfBenchmark(
Description = "Xml Docs (with cache)",
NumberOfIterations = 3,
RunTimeMilliseconds = 1000,
RunMode = RunMode.Throughput,
TestMode = TestMode.Test)]
[CounterThroughputAssertion("Iterations", MustBe.GreaterThan, 200)]
public void XmlDocTestsWithCache()
{
_generatorPerformance.XmlDocTests().GetAwaiter().GetResult();
_counter.Increment();
}

/// <summary>
/// Ensure that we can serialise at least 200 times per second (5ms).
/// </summary>
[NBenchFact]
[PerfBenchmark(
Description = "Xml Docs (without cache)",
NumberOfIterations = 3,
RunTimeMilliseconds = 1000,
RunMode = RunMode.Throughput,
TestMode = TestMode.Test)]
[CounterThroughputAssertion("Iterations", MustBe.GreaterThan, 200)]
public void XmlDocTestsWithoutCache()
{
XmlDocumentationExtensions.ClearCacheAsync().GetAwaiter().GetResult();
_generatorPerformance.XmlDocTests().GetAwaiter().GetResult();
_counter.Increment();
}
}
}
1 change: 1 addition & 0 deletions src/NJsonSchema.Benchmark/NJsonSchema.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NJsonSchema.Tests\NJsonSchema.Tests.csproj" />
<ProjectReference Include="..\NJsonSchema\NJsonSchema.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public async Task When_class_has_description_then_csharp_has_xml_comment()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<Teacher>();
schema.Description = "ClassDesc.";
schema.ActualSchema.Description = "ClassDesc.";
var generator = new CSharpGenerator(schema);

//// Act
Expand All @@ -295,7 +295,7 @@ public async Task When_property_has_description_then_csharp_has_xml_comment()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<Teacher>();
schema.Properties["Class"].Description = "PropertyDesc.";
schema.ActualProperties["Class"].Description = "PropertyDesc.";
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });

//// Act
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.10.73</Version>
<Version>9.10.74</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 @@ -19,13 +19,18 @@ public async Task When_class_is_abstract_then_is_abstract_TypeScript_keyword_is_
{
/// Arrange
var schema = await JsonSchema4.FromTypeAsync<AbstractClass>();
var json = schema.ToJson();

/// Act
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings { TypeScriptVersion = 2.0m });
var code = generator.GenerateFile("AbstractClass");

/// Assert
Assert.Contains("export abstract class AbstractClass", code);

Assert.Contains("base: string", code);
Assert.Contains("super: string", code);
Assert.Contains("foo: string", code);
}

public class ContainerClass
Expand Down Expand Up @@ -53,12 +58,12 @@ public async Task When_property_is_required_and_abstract_then_it_is_not_instanti
[JsonConverter(typeof(JsonInheritanceConverter))]
public class BaseClass
{

public string Base { get; set; }
}

public class SuperClass : AbstractClass
{

public string Super { get; set; }
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ public async Task When_property_has_description_then_csharp_has_xml_comment()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<Teacher>();
schema.Properties["Class"].Description = "PropertyDesc.";
schema.ActualProperties["Class"].Description = "PropertyDesc.";
var json = schema.ToJson();

var generator = new TypeScriptGenerator(schema);

//// Act
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.10.73</Version>
<Version>9.10.74</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 @@ -33,7 +33,7 @@ protected ClassTemplateModelBase(TypeResolverBase resolver, JsonSchema4 schema,
public abstract string ClassName { get; }

/// <summary>Gets or sets a value indicating whether the type is abstract.</summary>
public bool IsAbstract => _schema.IsAbstract;
public bool IsAbstract => _schema.ActualTypeSchema.IsAbstract;

/// <summary>Gets the property extension data.</summary>
public IDictionary<string, object> ExtensionData => _schema.ExtensionData;
Expand All @@ -49,7 +49,7 @@ public class DerivedClassModel
{
internal DerivedClassModel(string typeName, JsonSchema4 schema, OpenApiDiscriminator discriminator, TypeResolverBase resolver)
{
var mapping = discriminator.Mapping.SingleOrDefault(m => m.Value.ActualSchema == schema.ActualSchema);
var mapping = discriminator.Mapping.SingleOrDefault(m => m.Value.ActualTypeSchema == schema.ActualTypeSchema);

ClassName = resolver.GetOrGenerateTypeName(schema, typeName);
IsAbstract = schema.ActualTypeSchema.IsAbstract;
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.10.73</Version>
<Version>9.10.74</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
2 changes: 1 addition & 1 deletion src/NJsonSchema.Tests/Conversion/ArrayTypeToSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task When_converting_type_inheriting_from_dictionary_then_it_should
var data = schema.ToJson();

//// Assert
Assert.Equal(JsonObjectType.Object, schema.Type);
Assert.Equal(JsonObjectType.Object, schema.ActualTypeSchema.Type);
Assert.DoesNotContain("Foo", json);
Assert.DoesNotContain("foo", json);
}
Expand Down
20 changes: 20 additions & 0 deletions src/NJsonSchema.Tests/Generation/AttributeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,25 @@ public class AttributeTestClass
[ReadOnly(true)]
public bool ReadOnly { get; set; }
}

public class ClassWithTypedRange
{
[Range(typeof(decimal), "0", "1")]
public decimal Foo { get; set; }
}

[Fact]
public async Task When_range_has_type_and_strings_then_it_is_processed_correctly()
{
//// Arrange

//// Act
var schema = await JsonSchema4.FromTypeAsync<ClassWithTypedRange>();
var property = schema.Properties["Foo"];

//// Assert
Assert.Equal(0.0m, property.Minimum);
Assert.Equal(1.0m, property.Maximum);
}
}
}
4 changes: 2 additions & 2 deletions src/NJsonSchema.Tests/Generation/ExceptionTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public async Task When_exception_schema_is_generated_then_special_properties_are
var exceptionSchema = schema.InheritedSchema.ActualSchema;

//// Assert
Assert.True(schema.Properties.ContainsKey("foo"));
Assert.True(exceptionSchema.Properties.ContainsKey("InnerException"));
Assert.True(schema.ActualProperties.ContainsKey("foo"));
Assert.True(exceptionSchema.ActualProperties.ContainsKey("InnerException"));
}
}
}
44 changes: 40 additions & 4 deletions src/NJsonSchema.Tests/Generation/InheritanceTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
Expand Down Expand Up @@ -88,9 +89,9 @@ public async Task When_generating_type_with_inheritance_then_allOf_has_one_item(
var schema = await JsonSchema4.FromTypeAsync<Teacher>();

//// Assert
Assert.NotNull(schema.Properties["Class"]);
Assert.NotNull(schema.ActualProperties["Class"]);

Assert.Equal(1, schema.AllOf.Count);
Assert.Equal(2, schema.AllOf.Count);
Assert.Contains(schema.Definitions, d => d.Key == "Person");
Assert.NotNull(schema.AllOf.First().ActualSchema.Properties["Name"]);
}
Expand Down Expand Up @@ -298,10 +299,45 @@ public async Task Existing_non_string_property_cant_be_discriminant()
//// Arrange

//// Act
Task<JsonSchema4> getSchema() => JsonSchema4.FromTypeAsync<BaseClass_WithIntDiscriminant>();
Task<JsonSchema4> GetSchema() => JsonSchema4.FromTypeAsync<BaseClass_WithIntDiscriminant>();

//// Assert
await Assert.ThrowsAsync<InvalidOperationException>(getSchema);
await Assert.ThrowsAsync<InvalidOperationException>(GetSchema);
}

public class Foo
{
public Bar Bar { get; set; }
}

public class Bar : Dictionary<string, string>
{
public string Baz { get; set; }
}

[Fact]
public async Task When_class_inherits_from_dictionary_then_allOf_contains_base_dictionary_schema_and_actual_schema()
{
//// Arrange
var settings = new JsonSchemaGeneratorSettings
{
SchemaType = SchemaType.OpenApi3
};

//// Act
var schema = await JsonSchema4.FromTypeAsync<Foo>(settings);
var json = schema.ToJson();

//// Assert
var bar = schema.Definitions["Bar"];

Assert.Equal(2, bar.AllOf.Count);

Assert.Equal(bar.AllOf.Last(), bar.ActualTypeSchema);
Assert.Equal(bar.AllOf.First(), bar.InheritedSchema);

Assert.True(bar.AllOf.First().IsDictionary); // base class (dictionary)
Assert.True(bar.AllOf.Last().ActualProperties.Any()); // actual class
}
}
}
19 changes: 19 additions & 0 deletions src/NJsonSchema.Tests/Generation/SchemaGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ public async Task When_property_is_object_then_it_should_not_be_a_dictonary_but_
Assert.False(property.IsDictionary);
}

public class ClassWithStaticProperty
{
public static string Foo { get; set; }

public string Bar { get; set; }
}

[Fact]
public async Task When_property_is_static_then_it_is_ignored()
{
/// Act
var schema = await JsonSchema4.FromTypeAsync<ClassWithStaticProperty>();
var json = schema.ToJson();

/// Assert
Assert.Equal(1, schema.ActualProperties.Count);
Assert.True(schema.ActualProperties.ContainsKey("Bar"));
}

// Used as demo for https://github.com/swagger-api/swagger-ui/issues/1056

//public class TestClass
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.Tests/Generation/XmlDocTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public async Task When_xml_doc_is_missing_then_summary_is_missing()
//// Assert
Assert.Empty(summary);
}

public abstract class BaseBaseClass
{
/// <summary>Foo.</summary>
Expand Down
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.10.73</Version>
<Version>9.10.74</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
Loading

0 comments on commit 873ceae

Please sign in to comment.