Skip to content

Commit

Permalink
merge extension (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
vudodov committed Jul 18, 2019
1 parent 56ecf8e commit 2b1e3d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 4 additions & 2 deletions V.Udodov.Json.Tests/JsonSchemaTests.cs
Expand Up @@ -13,16 +13,18 @@ private class TheMock
public void WhenGeneratingJsonSchemaFromTypeAndStringItShouldGenerate()
{
const string schema = @"{
'version': 12,
'type': 'object',
'properties': {
'shoe_size': { 'type': 'number', 'minimum': 5, 'maximum': 12, 'multipleOf': 1.0 }
}
}";

var result = typeof(TheMock).Merge(schema);
var result = typeof(TheMock).MergeLeft(schema);

result.JsonEquals(@"{
""$id"": ""V.Udodov.Json.Tests.JsonSchemaTests+TheMock"",
""version"": 12,
""type"": ""object"",
""properties"": {
""mockProp"": {
Expand All @@ -49,7 +51,7 @@ public void WhenGeneratingJsonSchemaFromTypeItShouldGenerate()
{
const string schema = null;

var result = typeof(TheMock).Merge(schema);
var result = typeof(TheMock).MergeLeft(schema);

result.JsonEquals(@"{
""$id"": ""V.Udodov.Json.Tests.JsonSchemaTests+TheMock"",
Expand Down
28 changes: 26 additions & 2 deletions V.Udodov.Json/TypeExtensions.cs
Expand Up @@ -12,7 +12,17 @@ public static class TypeExtensions
/// Generates JSON Schema from the type and merges it with JSON Schema provided in <paramref name="jsonSchema"/>.
/// Optionally resulting JSON Schema Id might be defined. If not Id will be generated based from the <paramref name="mergeIn"/> full name.
/// </summary>
public static string Merge(this Type mergeIn, string jsonSchema, Uri id = null)
public static string MergeLeft(this Type mergeIn, string jsonSchema, Uri id = null)
=> Merge(mergeIn, jsonSchema, id, true);

/// <summary>
/// Generates JSON Schema from the type and merges it into JSON Schema provided in <paramref name="jsonSchema"/>.
/// Optionally resulting JSON Schema Id might be defined. If not Id will be generated based from the <paramref name="mergeIn"/> full name.
/// </summary>
public static string MergeRight(this Type mergeIn, string jsonSchema, Uri id = null)
=> Merge(mergeIn, jsonSchema, id, false);

private static string Merge(Type mergeIn, string jsonSchema, Uri id = null, bool left = true)
{
var schema = new JSchemaGenerator
{
Expand All @@ -22,9 +32,23 @@ public static string Merge(this Type mergeIn, string jsonSchema, Uri id = null)
}.Generate(mergeIn);

if(!string.IsNullOrWhiteSpace(jsonSchema))
JSchema.Parse(jsonSchema).Properties.ToList().ForEach(schema.Properties.Add);
return left
? Merge(schema, JSchema.Parse(jsonSchema)).ToString()
: Merge(JSchema.Parse(jsonSchema), schema).ToString();

return schema.ToString();
}


private static JSchema Merge(JSchema left, JSchema right)
{
right.Properties.ToList().ForEach(left.Properties.Add);
right.ExtensionData.ToList().ForEach(left.ExtensionData.Add);
right.Required.ToList().ForEach(left.Required.Add);
left.Id = left.Id ?? right.Id;
left.SchemaVersion = left.SchemaVersion ?? right.SchemaVersion;

return left;
}
}
}

0 comments on commit 2b1e3d4

Please sign in to comment.