Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to NJsonSchema lib #415

Merged
merged 5 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Microsoft.Health.Fhir.Liquid.Converter.Models;
using Microsoft.Health.Fhir.Liquid.Converter.Models.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using NJsonSchema;
using Xunit;

namespace Microsoft.Health.Fhir.Liquid.Converter.UnitTests.DotLiquids
Expand All @@ -23,7 +23,6 @@ public class TemplateLocalFileSystemTests
public void GivenAValidTemplateDirectory_WhenGetTemplate_CorrectResultsShouldBeReturned()
{
var templateLocalFileSystem = new TemplateLocalFileSystem(TestConstants.Hl7v2TemplateDirectory, DataType.Hl7v2);
var context = new Context(CultureInfo.InvariantCulture);

// Template exists
Assert.NotNull(templateLocalFileSystem.GetTemplate("ADT_A01"));
Expand All @@ -49,11 +48,10 @@ public void GivenAValidTemplateDirectory_WhenGetTemplateWithContext_CorrectResul
}

[Fact]
public void GivenAValidTemplateDirectory_WhenGetJsonSchemaTemplate_CorrectResultsShouldBeReturned()
public async void GivenAValidTemplateDirectory_WhenGetJsonSchemaTemplate_CorrectResultsShouldBeReturned()
{
var templateLocalFileSystem = new TemplateLocalFileSystem(Path.Join(TestConstants.TestTemplateDirectory, @"ValidValidateTemplates"), DataType.Hl7v2);
var testSchemaPath = "Schemas/TestSchema.schema.json";
var context = new Context(CultureInfo.InvariantCulture);

var schemaTemplate = templateLocalFileSystem.GetTemplate(testSchemaPath);

Expand All @@ -64,8 +62,8 @@ public void GivenAValidTemplateDirectory_WhenGetJsonSchemaTemplate_CorrectResult
Assert.NotNull(jSchemaDocument);
Assert.NotNull(jSchemaDocument.Schema);

JSchema expectedJSchema = JSchema.Parse(File.ReadAllText(Path.Join(TestConstants.TestTemplateDirectory, @"ValidValidateTemplates", testSchemaPath)));
Assert.True(JToken.DeepEquals(JToken.Parse(jSchemaDocument.Schema.ToString()), JToken.Parse(expectedJSchema.ToString())));
JsonSchema expectedJSchema = await JsonSchema.FromJsonAsync(File.ReadAllText(Path.Join(TestConstants.TestTemplateDirectory, @"ValidValidateTemplates", testSchemaPath)));
Assert.True(JToken.DeepEquals(JToken.Parse(jSchemaDocument.Schema.ToJson()), JToken.Parse(expectedJSchema.ToJson())));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void GivenValidValidateMatchedTemplateContent_WithJsonContext_WhenParseAn
for (int i = 0; i < expectSchemaFiles.Count; i++)
{
var expectedSchemaObject = JObject.Parse(File.ReadAllText(Path.Join(TestConstants.TestTemplateDirectory, expectSchemaFiles[i])));
var actualSchemaObject = JObject.Parse(context.ValidateSchemas[i].ToString());
var actualSchemaObject = JObject.Parse(context.ValidateSchemas[i].ToJson());
Assert.True(JToken.DeepEquals(expectedSchemaObject, actualSchemaObject));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"type": "invalidType"
- //Invalid schema content
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Patient customized schema",
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Patient customized schema",
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
QuanWanxx marked this conversation as resolved.
Show resolved Hide resolved
"title": "Patient customized schema",
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using DotLiquid;
using DotLiquid.Exceptions;
Expand All @@ -13,7 +14,7 @@
using Microsoft.Health.Fhir.Liquid.Converter.Models.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using NJsonSchema;
using RenderException = Microsoft.Health.Fhir.Liquid.Converter.Exceptions.RenderException;

namespace Microsoft.Health.Fhir.Liquid.Converter.DotLiquids
Expand Down Expand Up @@ -50,7 +51,7 @@ public override void Initialize(string tagName, string markup, List<string> toke

public override void Render(Context context, TextWriter result)
{
JSchema validateSchema = LoadValidateSchema(context);
JsonSchema validateSchema = LoadValidateSchema(context);

if (context is JSchemaContext jSchemaContext)
{
Expand All @@ -76,15 +77,17 @@ public override void Render(Context context, TextWriter result)
throw new RenderException(FhirConverterErrorCode.InvalidValidateBlockContent, string.Format(Resources.InvalidValidateContentBlock, ex.Message));
}

if (!validateObject.IsValid(validateSchema, out IList<string> messages))
var errors = validateSchema.Validate(validateObject);

if (errors.Any())
{
throw new RenderException(FhirConverterErrorCode.UnmatchedValidateBlockContent, string.Format(Resources.UnMatchedValidateBlockContent, _schemaFileName, string.Join(";", messages)));
throw new RenderException(FhirConverterErrorCode.UnmatchedValidateBlockContent, string.Format(Resources.UnMatchedValidateBlockContent, _schemaFileName, string.Join(";", errors)));
}

result.Write(validateContent);
}

private JSchema LoadValidateSchema(Context context)
private JsonSchema LoadValidateSchema(Context context)
{
if (!(context.Registers["file_system"] is IFhirConverterTemplateFileSystem fileSystem))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.9" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.14" />
<PackageReference Include="NJsonSchema" Version="10.7.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System;
using System.Collections.Generic;
using DotLiquid;
using Newtonsoft.Json.Schema;
using NJsonSchema;

namespace Microsoft.Health.Fhir.Liquid.Converter.Models.Json
{
Expand All @@ -15,9 +15,9 @@ public class JSchemaContext : Context
public JSchemaContext(List<Hash> environments, Hash outerScope, Hash registers, ErrorsOutputMode errorsOutputMode, int maxIterations, int timeout, IFormatProvider formatProvider)
: base(environments, outerScope, registers, errorsOutputMode, maxIterations, timeout, formatProvider)
{
ValidateSchemas = new List<JSchema>();
ValidateSchemas = new List<JsonSchema>();
}

public List<JSchema> ValidateSchemas { get; set; }
public List<JsonSchema> ValidateSchemas { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

using System;
using DotLiquid;
using Newtonsoft.Json.Schema;
using NJsonSchema;

namespace Microsoft.Health.Fhir.Liquid.Converter.Models.Json
{
public class JSchemaDocument : Document
{
public JSchemaDocument(JSchema schema)
public JSchemaDocument(JsonSchema schema)
{
if (schema == null)
{
Expand All @@ -21,6 +21,6 @@ public JSchemaDocument(JSchema schema)
Schema = schema;
}

public JSchema Schema { get; set; }
public JsonSchema Schema { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// -------------------------------------------------------------------------------------------------

using System.Collections.Generic;
using Newtonsoft.Json.Schema;
using NJsonSchema;

namespace Microsoft.Health.Fhir.Liquid.Converter.Models.Json
{
Expand All @@ -14,11 +14,11 @@ public JSchemaTraceInfo()
{
}

public JSchemaTraceInfo(List<JSchema> validateSchemas)
public JSchemaTraceInfo(List<JsonSchema> validateSchemas)
{
ValidateSchemas = validateSchemas;
}

public List<JSchema> ValidateSchemas { get; set; }
public List<JsonSchema> ValidateSchemas { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Microsoft.Health.Fhir.Liquid.Converter.Models;
using Microsoft.Health.Fhir.Liquid.Converter.Models.Json;
using Microsoft.Health.Fhir.Liquid.Converter.Parsers;
using Newtonsoft.Json.Schema;
using NJsonSchema;

namespace Microsoft.Health.Fhir.Liquid.Converter.Processors
{
Expand Down Expand Up @@ -41,7 +41,7 @@ protected override Context CreateContext(ITemplateProvider templateProvider, IDi
timeout: timeout,
formatProvider: CultureInfo.InvariantCulture)
{
ValidateSchemas = new List<JSchema>(),
ValidateSchemas = new List<JsonSchema>(),
};

// Load filters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using Microsoft.Health.Fhir.Liquid.Converter.Models;
using Microsoft.Health.Fhir.Liquid.Converter.Models.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Schema;
using NJsonSchema;

namespace Microsoft.Health.Fhir.Liquid.Converter.Utilities
{
Expand Down Expand Up @@ -51,6 +51,7 @@ public static Dictionary<string, Template> ParseTemplates(Dictionary<string, str
parsedTemplates[templateKey] = ParseTemplate(templateKey, entry.Value);
}
}

return parsedTemplates;
}

Expand Down Expand Up @@ -145,12 +146,12 @@ public static Template ParseJsonSchemaTemplate(string content)
return null;
}

JSchema schema;
JsonSchema schema;
try
{
schema = JSchema.Parse(content);
schema = JsonSchema.FromJsonAsync(content).GetAwaiter().GetResult();
}
catch (JSchemaReaderException ex)
catch (JsonReaderException ex)
QuanWanxx marked this conversation as resolved.
Show resolved Hide resolved
{
throw new TemplateLoadException(FhirConverterErrorCode.InvalidJsonSchema, string.Format(Resources.InvalidJsonSchemaContent, ex.Message), ex);
}
Expand Down