Skip to content

Commit

Permalink
Fix writing dependencies, add JSchema.ToString settings overload (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Dec 23, 2019
1 parent a848c36 commit 000a31d
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
$treatWarningsAsErrors = $false
$workingName = if ($workingName) {$workingName} else {"Working"}
$assemblyVersion = if ($assemblyVersion) {$assemblyVersion} else {$majorVersion + '.0.0'}
$netCliChannel = "2.0"
$netCliVersion = "3.0.100-preview9-013927"
$netCliChannel = "Current"
$netCliVersion = "3.1.100"
$nugetUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"

$baseDir = resolve-path ..
Expand All @@ -33,7 +33,7 @@

$builds = @(
@{Framework = "netstandard1.3"; TestsFunction = "NetCliTests"; TestFramework = "netcoreapp2.1"; Enabled=$true}
@{Framework = "netstandard2.0"; TestsFunction = "NetCliTests"; TestFramework = "netcoreapp3.0"; Enabled=$true}
@{Framework = "netstandard2.0"; TestsFunction = "NetCliTests"; TestFramework = "netcoreapp3.1"; Enabled=$true}
@{Framework = "net45"; TestsFunction = "NUnitTests"; TestFramework = "net46"; NUnitFramework="net-4.0"; Enabled=$true},
@{Framework = "net40"; TestsFunction = "NUnitTests"; NUnitFramework="net-4.0"; Enabled=$true},
@{Framework = "net35"; TestsFunction = "NUnitTests"; NUnitFramework="net-2.0"; Enabled=$true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Reflection;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema.Infrastructure;
using System.Text;

namespace Newtonsoft.Json.Schema.Tests.Infrastructure
{
Expand Down Expand Up @@ -346,6 +347,17 @@ public void BadRegexPattern()
[Test]
public void ReadAllResourceSchemas()
{
JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
resolver.Add(
new Uri("http://json-schema.org/draft-04/schema"),
Encoding.UTF8.GetBytes(TestHelpers.OpenFileText("Resources/Schemas/schema-draft-v4.json")));
resolver.Add(
new Uri("http://json.schemastore.org/grunt-task"),
Encoding.UTF8.GetBytes(TestHelpers.OpenFileText("Resources/Schemas/grunt-task.json")));
resolver.Add(
new Uri("http://json.schemastore.org/jshintrc"),
Encoding.UTF8.GetBytes(TestHelpers.OpenFileText("Resources/Schemas/jshintrc.json")));

string schemaDir = TestHelpers.ResolveFilePath(@"resources\schemas");

string[] schemaFilePaths = Directory.GetFiles(schemaDir, "*.json");
Expand All @@ -357,7 +369,7 @@ public void ReadAllResourceSchemas()
using (StreamReader sr = File.OpenText(schemaFilePath))
using (JsonTextReader reader = new JsonTextReader(sr))
{
JSchema schema = JSchema.Load(reader, new JSchemaUrlResolver());
JSchema schema = JSchema.Load(reader, resolver);
}
}
catch (Exception ex)
Expand Down
5 changes: 4 additions & 1 deletion Src/Newtonsoft.Json.Schema.Tests/Issues/Issue0032Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public void Test()
{
string swaggerJson = TestHelpers.OpenFileText("Resources/Schemas/swagger-2.0.json");

JSchemaUrlResolver resolver = new JSchemaUrlResolver();
JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
resolver.Add(
new Uri("http://json-schema.org/draft-04/schema"),
Encoding.UTF8.GetBytes(TestHelpers.OpenFileText("Resources/Schemas/schema-draft-v4.json")));

JSchema swagger = JSchema.Parse(swaggerJson, resolver);

Expand Down
102 changes: 102 additions & 0 deletions Src/Newtonsoft.Json.Schema.Tests/Issues/Issue0200Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#region License
// Copyright (c) Newtonsoft. All Rights Reserved.
// License: https://raw.github.com/JamesNK/Newtonsoft.Json.Schema/master/LICENSE.md
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Schema.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif

namespace Newtonsoft.Json.Schema.Tests.Issues
{
[TestFixture]
public class Issue0200Tests : TestFixtureBase
{
[Test]
public void Test()
{
// Arrange
string schemaJson = @"{
""type"": ""object"",
""definitions"": {
""foo"": {
""type"": ""object"",
""properties"": {
""bar"": {
""$ref"": ""#/definitions/bar""
},
""baz"": {
""$ref"": ""#/definitions/bar""
}
}
},
""bar"": {
""type"": ""string""
}
},
""dependencies"": {
""foo"": {
""oneOf"": [
{
""properties"": {
""foo"": {
""$ref"": ""#/definitions/foo""
}
}
}
]
}
}
}";

JSchema schema = JSchema.Parse(schemaJson);

// Act
string serialized = schema.ToString();

// Assert
StringAssert.AreEqual(@"{
""definitions"": {
""foo"": {
""type"": ""object"",
""properties"": {
""bar"": {
""$ref"": ""#/definitions/bar""
},
""baz"": {
""$ref"": ""#/definitions/bar""
}
}
},
""bar"": {
""type"": ""string""
}
},
""type"": ""object"",
""dependencies"": {
""foo"": {
""oneOf"": [
{
""properties"": {
""foo"": {
""$ref"": ""#/definitions/foo""
}
}
}
]
}
}
}", serialized);
}
}
}
22 changes: 22 additions & 0 deletions Src/Newtonsoft.Json.Schema.Tests/JSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,27 @@ public void PatternProperties_Remove_NullKey()
Parameter name: key",
"Value cannot be null. (Parameter 'key')");
}

[Test]
public void ToString_JsonWriterSettings()
{
JSchema propertySchema = new JSchema();

JSchema s = new JSchema();
s.Properties["one"] = propertySchema;
s.Properties["two"] = propertySchema;

string json = s.ToString(new JSchemaWriterSettings
{
ReferenceHandling = JSchemaWriterReferenceHandling.Never
});

StringAssert.AreEqual(@"{
""properties"": {
""one"": {},
""two"": {}
}
}", json);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks Condition="'$(TestFrameworks)'==''">net46;net40;net35;netcoreapp2.1;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks Condition="'$(TestFrameworks)'==''">net46;net40;net35;netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks Condition="'$(TestFrameworks)'!=''">$(TestFrameworks)</TargetFrameworks>
<VersionPrefix>1.0</VersionPrefix>
<Authors>Newtonsoft</Authors>
Expand All @@ -12,7 +12,7 @@
<AssemblyName>Newtonsoft.Json.Schema.Tests</AssemblyName>
<IsPackable>false</IsPackable>
<!-- Workaround for https://github.com/nunit/nunit3-vs-adapter/issues/296 -->
<DebugType Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'netcoreapp2.1' AND '$(TargetFramework)' != 'netcoreapp3.0'">Full</DebugType>
<DebugType Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'netcoreapp2.1' AND '$(TargetFramework)' != 'netcoreapp3.1'">Full</DebugType>
<NoWarn>1685;1701;1702;1705</NoWarn>
<!-- Disabled because SourceLink isn't referenced to calculate paths -->
<DeterministicSourcePaths>false</DeterministicSourcePaths>
Expand Down Expand Up @@ -74,7 +74,7 @@
<DefineConstants>NETSTANDARD1_3;DNXCORE50;PORTABLE;HAVE_BENCHMARKS;$(AdditionalConstants)</DefineConstants>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.0'">
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
<PackageReference Include="BenchmarkDotNet" Version="0.10.9" />
<PackageReference Include="FSharp.Core" Version="4.2.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.3.0" />
Expand All @@ -83,7 +83,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='netcoreapp3.0'">
<PropertyGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
<AssemblyTitle>Json.NET Schema Tests .NET Standard 2.0</AssemblyTitle>
<ReferringTargetFrameworkForProjectReferences>.NETStandard,Version=v2.0</ReferringTargetFrameworkForProjectReferences>
<DefineConstants>NETSTANDARD1_3;DNXCORE50;PORTABLE;HAVE_BENCHMARKS;$(AdditionalConstants)</DefineConstants>
Expand Down
4 changes: 2 additions & 2 deletions Src/Newtonsoft.Json.Schema/Infrastructure/JSchemaWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,13 @@ private void WriteSchemaObjectInternal(JSchema schema)
_writer.WriteStartObject();
foreach (KeyValuePair<string, object> dependency in schema._dependencies)
{
_writer.WritePropertyName(dependency.Key);
if (dependency.Value is JSchema s)
{
s.WriteTo(_writer);
ReferenceOrWriteSchema(schema, s, dependency.Key);
}
else if (dependency.Value is IList<string> a)
{
_writer.WritePropertyName(dependency.Key);
_writer.WriteStartArray();
for (int i = 0; i < a.Count; i++)
{
Expand Down
12 changes: 12 additions & 0 deletions Src/Newtonsoft.Json.Schema/JSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,18 @@ public string ToString(SchemaVersion version)
return ToStringInternal(settings);
}

/// <summary>
/// Returns the JSON for this schema using the specified <see cref="JSchemaWriterSettings"/>.
/// </summary>
/// <param name="settings">The settings used to write the schema.</param>
/// <returns>The JSON for this schema.</returns>
public string ToString(JSchemaWriterSettings settings)
{
ValidationUtils.ArgumentNotNull(settings, nameof(settings));

return ToStringInternal(settings);
}

private string ToStringInternal(JSchemaWriterSettings settings)
{
StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
Expand Down
2 changes: 1 addition & 1 deletion Src/global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.0.100-preview9-013927"
"version": "3.1.100"
}
}

0 comments on commit 000a31d

Please sign in to comment.