Skip to content

Commit bff4ece

Browse files
authored
Merge pull request #267 from bingenito/jsonschema
2 parents e1f2ef8 + 877f3b7 commit bff4ece

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

.cve/allow-list.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,4 @@
3030
]]></notes>
3131
<cpe>cpe:/a:morgan_project:morgan</cpe>
3232
</suppress>
33-
<suppress>
34-
<notes><![CDATA[
35-
file name: Newtonsoft.Json.Schema.dll
36-
]]></notes>
37-
<packageUrl regex="true">^pkg:generic/Newtonsoft\.Json\.Schema@.*$</packageUrl>
38-
<cve>CVE-2024-21907</cve>
39-
</suppress>
4033
</suppressions>

src/Tests/Finos.Fdc3.NewtonsoftJson.Tests/Context/ContextSchemaTest.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
using Finos.Fdc3.Context;
77
using Finos.Fdc3.NewtonsoftJson.Serialization;
8+
using Json.Schema;
89
using Newtonsoft.Json;
9-
using Newtonsoft.Json.Linq;
10-
using Newtonsoft.Json.Schema;
1110
using System.Reflection;
11+
using System.Text.Json.Nodes;
1212

1313
namespace Finos.Fdc3.NewtonsoftJson.Tests.Context;
1414

1515
public abstract class ContextSchemaTest
1616
{
17-
protected JSchema? Schema { get; private set; }
17+
protected JsonSchema? Schema { get; private set; }
1818
protected string SchemaUrl { get; private set; }
1919
protected JsonSerializerSettings SerializerSettings { get; }
2020

@@ -26,13 +26,31 @@ public ContextSchemaTest(string schemaUrl)
2626

2727
protected async Task<string> ValidateSchema(IContext context)
2828
{
29-
this.Schema = JSchema.Parse(await (await new HttpClient().GetAsync(this.SchemaUrl)).Content.ReadAsStringAsync(), new JSchemaUrlResolver());
29+
SchemaRegistry.Global.Fetch = uri =>
30+
{
31+
using var client = new HttpClient();
32+
var text = client.GetStringAsync(uri).GetAwaiter().GetResult();
33+
return JsonSchema.FromText(text);
34+
};
35+
36+
string schemaText = await (await new HttpClient().GetAsync(this.SchemaUrl)).Content.ReadAsStringAsync();
37+
this.Schema = JsonSchema.FromText(schemaText);
3038

3139
string serializedContext = JsonConvert.SerializeObject(context, this.SerializerSettings);
32-
JToken json = JToken.Parse(serializedContext);
33-
IList<string> errorMessages;
34-
bool isValid = json.IsValid(this.Schema, out errorMessages);
35-
Assert.True(isValid, String.Join(",", errorMessages.ToArray<string>()));
40+
var instanceJson = JsonNode.Parse(serializedContext);
41+
42+
var options = new EvaluationOptions { OutputFormat = OutputFormat.List };
43+
var results = this.Schema.Evaluate(instanceJson, options);
44+
45+
if (!results.IsValid)
46+
{
47+
var errorMessages = results.Details?
48+
.Where(d => d.Errors != null)
49+
.SelectMany(d => d.Errors!.Values)
50+
.ToArray() ?? Array.Empty<string>();
51+
Assert.True(results.IsValid, String.Join(",", errorMessages));
52+
}
53+
3654
return serializedContext;
3755
}
3856

src/Tests/Finos.Fdc3.NewtonsoftJson.Tests/Finos.Fdc3.NewtonsoftJson.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<ItemGroup>
2222
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
2323
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
24-
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" />
24+
<PackageReference Include="JsonSchema.Net" Version="7.4.0" />
2525
<PackageReference Include="xunit" Version="2.9.3" />
2626
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
2727
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)