-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Generate JSON schema from type and merge in extension schema * Rename Extension class
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Xunit; | ||
|
||
namespace V.Udodov.Json.Tests | ||
{ | ||
public class JsonSchemaTests | ||
{ | ||
private class TheMock | ||
{ | ||
public string MockProp { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void WhenGeneratingJsonSchemaFromTypeAndStringItShouldGenerate() | ||
{ | ||
const string schema = @"{ | ||
'type': 'object', | ||
'properties': { | ||
'shoe_size': { 'type': 'number', 'minimum': 5, 'maximum': 12, 'multipleOf': 1.0 } | ||
} | ||
}"; | ||
|
||
var result = typeof(TheMock).Merge(schema); | ||
|
||
result.JsonEquals(@"{ | ||
""$id"": ""V.Udodov.Json.Tests.JsonSchemaTests+TheMock"", | ||
""type"": ""object"", | ||
""properties"": { | ||
""mockProp"": { | ||
""type"": [ | ||
""string"", | ||
""null"" | ||
] | ||
}, | ||
""shoe_size"": { | ||
""type"": ""number"", | ||
""minimum"": 5.0, | ||
""maximum"": 12.0, | ||
""multipleOf"": 1.0 | ||
} | ||
}, | ||
""required"": [ | ||
""mockProp"" | ||
] | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public void WhenGeneratingJsonSchemaFromTypeItShouldGenerate() | ||
{ | ||
const string schema = null; | ||
|
||
var result = typeof(TheMock).Merge(schema); | ||
|
||
result.JsonEquals(@"{ | ||
""$id"": ""V.Udodov.Json.Tests.JsonSchemaTests+TheMock"", | ||
""type"": ""object"", | ||
""properties"": { | ||
""mockProp"": { | ||
""type"": [ | ||
""string"", | ||
""null"" | ||
] | ||
} | ||
}, | ||
""required"": [ | ||
""mockProp"" | ||
] | ||
}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Linq; | ||
using Newtonsoft.Json.Schema; | ||
using Newtonsoft.Json.Schema.Generation; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace V.Udodov.Json | ||
{ | ||
public static class TypeExtensions | ||
{ | ||
/// <summary> | ||
/// 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) | ||
{ | ||
var schema = new JSchemaGenerator | ||
{ | ||
SchemaIdGenerationHandling = | ||
id == null ? SchemaIdGenerationHandling.FullTypeName : SchemaIdGenerationHandling.None, | ||
ContractResolver = new CamelCasePropertyNamesContractResolver() | ||
}.Generate(mergeIn); | ||
|
||
if(!string.IsNullOrWhiteSpace(jsonSchema)) | ||
JSchema.Parse(jsonSchema).Properties.ToList().ForEach(schema.Properties.Add); | ||
|
||
return schema.ToString(); | ||
} | ||
} | ||
} |