public void WhenNamespaceSpecifiedForNestedContainer_SchemaReadser_NamespaceNotBackedUp() { var nodes = JsonSchema.FromJson( new JsonSchemaBuilder() .WithContainer( new JsonSchemaBuilder.ContainerBuilder("HelloWorld") .WithNamespace("Unity.Properties.Samples.Schema") .WithNestedContainer( new JsonSchemaBuilder.ContainerBuilder("NestedHelloWorld") .WithNamespace("Foo") ) .WithNestedContainer( new JsonSchemaBuilder.ContainerBuilder("PlainNestedHelloWorld") ) ) .ToJson()); Assert.AreEqual(1, nodes.PropertyTypeNodes.Count); Assert.AreEqual(nodes.PropertyTypeNodes[0].NestedContainers.Count, 2); Assert.AreEqual("NestedHelloWorld", nodes.PropertyTypeNodes[0].NestedContainers[0].TypeName); Assert.AreEqual("Unity.Properties.Samples.Schema.HelloWorld/NestedHelloWorld", nodes.PropertyTypeNodes[0].NestedContainers[0].FullTypeName ); Assert.AreEqual("PlainNestedHelloWorld", nodes.PropertyTypeNodes[0].NestedContainers[1].TypeName); Assert.AreEqual( "Unity.Properties.Samples.Schema.HelloWorld/PlainNestedHelloWorld", nodes.PropertyTypeNodes[0].NestedContainers[1].FullTypeName ); }
public void Online(JsonSchema schema) { try { var localSchemaJson = schema.ToJson(_serializer); var onlineSchemaText = JsonSchemaOptions.Download(schema.Id); var onlineSchemaJson = JsonValue.Parse(onlineSchemaText); var onlineSchema = new JsonSchema(); onlineSchema.FromJson(onlineSchemaJson, _serializer); var localValidation = schema.Validate(onlineSchemaJson); var onlineValidation = onlineSchema.Validate(localSchemaJson); Assert.AreEqual(onlineSchema, schema); onlineValidation.AssertValid(); localValidation.AssertValid(); Assert.AreEqual(onlineSchemaJson, localSchemaJson); } catch (WebException) { Assert.Inconclusive(); } catch (AggregateException e) { if (e.InnerExceptions.OfType <WebException>().Any()) { Assert.Inconclusive(); } throw; } }
public static int ManateeValidation() { var schemaContent = File.ReadAllText(@"D:\code\test\JsonSchemaTest\JsonSchemaTest\schema.json"); var schemaValue = JsonValue.Parse(schemaContent); var schema = new JsonSchema(); schema.FromJson(schemaValue, new JsonSerializer()); var total = 0; var validCount = 0; using (var rd = new StreamReader(@"D:\code\test\JsonSchemaTest\JsonSchemaTest\oneksrcdata.tsv")) { string line; while ((line = rd.ReadLine()) != null) { var strs = line.Split('\t'); if (strs.Length >= 2) { total++; var json = JsonValue.Parse(strs[1]); var validateResult = schema.Validate(json); if (validateResult.IsValid) { validCount++; } } } } return(validCount); }
public void LoadJsonSchemaTest() { JsonSchema jsonSchema; using (StreamReader streamReader = new StreamReader("Common/example.schema.json", Encoding.UTF8)) { jsonSchema = new JsonSchema(); jsonSchema.FromJson(JsonValue.Parse(streamReader.ReadToEnd()), new JsonSerializer()); } JsonValue jsonInstance; using (StreamReader streamReader = new StreamReader("Common/example.json", Encoding.UTF8)) { jsonInstance = JsonValue.Parse(streamReader.ReadToEnd()); } // Test schema var schemaValidationResults = jsonSchema.ValidateSchema(); Assert.True(schemaValidationResults.IsValid); Assert.Empty(schemaValidationResults.OtherErrors); // Test instance var validationResults = jsonSchema.Validate(jsonInstance); Assert.True(validationResults.IsValid); }
public object Deserialize(SerializationContext context) { var schema = new JsonSchema(); schema.FromJson(context.LocalValue, context.RootSerializer); return(schema); }
public void WhenAssemblyPropertyContainer_ReflectionJsonSchemaGenerator_ReturnsAValidJson() { string assemblyFilePath = string.Empty; string errors = string.Empty; string code = @" using System.Collections.Generic; using Unity.Properties; namespace Unity.Properties.TestCases { public partial class HelloWorld : IPropertyContainer { public static IPropertyBag bag { get; } = new PropertyBag(new List<IProperty> {}.ToArray()); public IVersionStorage VersionStorage { get; } public IPropertyBag PropertyBag => bag; private MyContainer m_MyContainer; private static readonly ContainerProperty<HelloWorld, MyContainer> s_MyContainer = new ContainerProperty<HelloWorld, MyContainer>( ""MyContainer"", c => c.m_MyContainer, (c, v) => c.m_MyContainer = v); public class MyContainer : IPropertyContainer { public static IPropertyBag bag { get; } = new PropertyBag(new List<IProperty> {}.ToArray()); public IVersionStorage VersionStorage { get; } public IPropertyBag PropertyBag => bag; } }; } "; Assert.IsTrue(CompileTestUtils.TryCompileToFile(code, out assemblyFilePath, out errors), errors); using (new FileDisposer(assemblyFilePath)) { var result = JsonSchema.FromJson( JsonSchema.ToJson( new JsonSchema() { PropertyTypeNodes = ReflectionPropertyTree.Read(assemblyFilePath) } ) ); var containers = new List <PropertyTypeNode>(); VisitContainer(result.PropertyTypeNodes, c => { containers.Add(c); }); Assert.True(containers.Count == 2); Assert.True(containers[0].TypeName == "HelloWorld"); Assert.True(containers[1].TypeName == "MyContainer"); } }
public void WhenMultipleContainers_SchemaReadser_CanResolveContainerTypes() { var nodes = JsonSchema.FromJson( new JsonSchemaBuilder() .WithContainer( new JsonSchemaBuilder.ContainerBuilder("HelloWorld") .WithNamespace("Unity.Properties.Samples.Schema") .WithNestedContainer( new JsonSchemaBuilder.ContainerBuilder("NestedHelloWorld") .WithNestedContainer( new JsonSchemaBuilder.ContainerBuilder("NestedHelloWorld") .WithProperty("MyFooBar", "FooBar") ) ) .WithNestedContainer( new JsonSchemaBuilder.ContainerBuilder("NestedHelloWorld") .WithNamespace("Unity.Properties.Samples.Schema") .WithNestedContainer( new JsonSchemaBuilder.ContainerBuilder("NestedHelloWorld") .WithProperty("MyFooBar", "FooBar") ) ) ) .WithContainer(new JsonSchemaBuilder.ContainerBuilder("Foo", true) .WithNamespace("Unity.Properties.Samples.Schema") .WithProperty("MyBar", "Bar") ) .WithContainer(new JsonSchemaBuilder.ContainerBuilder("Bar") .WithNamespace("Unity.Properties.Foo.Schema") .WithProperty("MyFooBar", "FooBar") ) .WithContainer(new JsonSchemaBuilder.ContainerBuilder("FooBar", true) .WithProperty("MyNested", "HelloWorld.NestedHelloWorld", "5") ) .ToJson()); Assert.AreEqual(4, nodes.PropertyTypeNodes.Count); Assert.AreEqual(nodes.PropertyTypeNodes[0].NestedContainers.Count, 2); Assert.AreEqual(nodes.PropertyTypeNodes[0].NestedContainers[0].TypeName, "NestedHelloWorld"); Assert.AreEqual(new List <string> { "HelloWorld", "Foo", "Bar", "FooBar" }, nodes.PropertyTypeNodes.Select(c => c.TypeName) ); Assert.AreEqual(new List <PropertyTypeNode.TypeTag> { PropertyTypeNode.TypeTag.Class, PropertyTypeNode.TypeTag.Struct, PropertyTypeNode.TypeTag.Class, PropertyTypeNode.TypeTag.Struct, }, nodes.PropertyTypeNodes.Select(c => c.Tag) ); }
public void Online(string testName, JsonSchema schema) { try { var localSchemaJson = schema.ToJson(_serializer); var onlineSchemaText = JsonSchemaOptions.Download(schema.Id); var onlineSchemaJson = JsonValue.Parse(onlineSchemaText); var onlineSchema = new JsonSchema(); onlineSchema.FromJson(onlineSchemaJson, _serializer); var localValidation = schema.Validate(onlineSchemaJson); var onlineValidation = onlineSchema.Validate(localSchemaJson); try { Console.WriteLine("Asserting schema equality"); Assert.AreEqual(onlineSchema, schema); Console.WriteLine("Validating local against online"); onlineValidation.AssertValid(); Console.WriteLine("Validating online against local"); localValidation.AssertValid(); Console.WriteLine("Asserting json equality"); Assert.AreEqual(onlineSchemaJson, localSchemaJson); } catch (Exception) { Console.WriteLine("Online {0}", onlineSchemaJson); Console.WriteLine("Local {0}", localSchemaJson); throw; } } catch (WebException) { Assert.Inconclusive(); } catch (HttpRequestException) { Assert.Inconclusive(); } catch (SocketException) { Assert.Inconclusive(); } catch (AggregateException e) { if (e.InnerExceptions.OfType <WebException>().Any() || e.InnerExceptions.OfType <HttpRequestException>().Any()) { Assert.Inconclusive(); } throw; } }
public void WhenNoTypesInSchema_SchemaReader_ReturnsAnEmptyContainerList() { var result = JsonSchema.FromJson( new JsonSchemaBuilder() .WithNamespace("Unity.Properties.Samples.Schema") .WithEmptyContainerList() .ToJson() ); Assert.Zero(result.PropertyTypeNodes.Count); }
public void WhenEmptyUsings_SchemaReader_DoesNotGenerateDefaults() { var result = JsonSchema.FromJson( new JsonSchemaBuilder() .WithNamespace("Unity.Properties.Samples.Schema") .WithUsing(new List <string> { }) .WithEmptyContainerList() .ToJson() ); Assert.Zero(result.UsingAssemblies.Count); }
public void Issue15_DeclaredTypeWithDeclaredEnum() { var text = "{\"type\":\"string\",\"enum\":[\"FeatureCollection\"]}"; var json = JsonValue.Parse(text); var expected = new JsonSchema() .Type(JsonSchemaType.String) .Enum("FeatureCollection"); var actual = new JsonSchema(); actual.FromJson(json, _serializer); Assert.AreEqual(expected, actual); }
public void WhenUsingsSpecified_SchemaReader_DoesOverrideDefaults() { var result = JsonSchema.FromJson( new JsonSchemaBuilder() .WithNamespace("Unity.Properties.Samples.Schema") .WithUsing(new List <string> { "Unity.Properties.Editor" }) .WithEmptyContainerList() .ToJson() ); Assert.IsTrue(result.UsingAssemblies.Count == 1); }
public void WhenAssemblyContainsPropertyContainer_ReflectionJsonSchemaGenerator_ReturnsAValidJson() { string assemblyFilePath = string.Empty; string errors = string.Empty; string code = @" using System.Collections.Generic; using Unity.Properties; namespace Unity.Properties.TestCases { public partial class HelloWorld : IPropertyContainer { public static IPropertyBag bag { get; } = new PropertyBag(new List<IProperty> {}.ToArray()); public IVersionStorage VersionStorage { get; } public IPropertyBag PropertyBag => bag; public class Foo { public int Data; public List<float> Floats = new List<float>(); } public Foo foo { get; } = new Foo(); }; } "; Assert.IsTrue(CompileTestUtils.TryCompileToFile(code, out assemblyFilePath, out errors), errors); using (new FileDisposer(assemblyFilePath)) { var result = JsonSchema.FromJson( JsonSchema.ToJson( new JsonSchema() { PropertyTypeNodes = ReflectionPropertyTree.Read(assemblyFilePath) } ) ); var containers = new List <PropertyTypeNode>(); VisitContainer(result.PropertyTypeNodes, c => { containers.Add(c); }); Assert.True(containers.Count == 1); Assert.True(containers[0].TypeName == "HelloWorld"); } }
public void WhenClassWithNamespace_SchemaReader_ReturnsProperNamespaceForClass() { var result = JsonSchema.FromJson( new JsonSchemaBuilder() .WithNamespace("Unity.Properties.Samples.Schema") .WithContainer( new JsonSchemaBuilder.ContainerBuilder("HelloWorld") .WithEmptyPropertiesList() ) .ToJson() ); Assert.AreEqual(1, result.PropertyTypeNodes.Count); Assert.AreEqual("Unity.Properties.Samples.Schema", result.PropertyTypeNodes[0].TypePath.Namespace); }
public void DeserializeSchema_TypePropertyIsArray_Issue14() { var text = "{\"type\":\"object\",\"properties\":{\"home\":{\"type\":[\"object\",\"null\"],\"properties\":{\"street\":{\"type\":\"string\"}}}}}"; var json = JsonValue.Parse(text); var expected = new JsonSchema() .Type(JsonSchemaType.Object) .Property("home", new JsonSchema() .Type(JsonSchemaType.Object | JsonSchemaType.Null) .Property("street", new JsonSchema().Type(JsonSchemaType.String))); var actual = new JsonSchema(); actual.FromJson(json, new JsonSerializer()); Assert.AreEqual(expected, actual); }
public PropertyTypeNode.TypeTag WhenClassType_SchemaReader_ReturnsTypeTagAsClass(string containerKind) { var result = JsonSchema.FromJson( new JsonSchemaBuilder() .WithNamespace("Unity.Properties.Samples.Schema") .WithContainer( new JsonSchemaBuilder.ContainerBuilder("HelloWorld", containerKind == "struct") .WithEmptyPropertiesList() ) .ToJson() ); Assert.AreEqual(1, result.PropertyTypeNodes.Count); Assert.AreEqual("HelloWorld", result.PropertyTypeNodes[0].TypeName); return(result.PropertyTypeNodes[0].Tag); }
public void WhenContainersetAsNoDefaultImplementation_SchemaReader_ReturnsNoDefaultImplementationFlagSet() { var result = JsonSchema.FromJson( new JsonSchemaBuilder() .WithNamespace("Unity.Properties.Samples.Schema") .WithContainer( new JsonSchemaBuilder.ContainerBuilder("HelloWorld") .WithNoDefaultImplementation() .WithEmptyPropertiesList() ) .ToJson() ); Assert.AreEqual(1, result.PropertyTypeNodes.Count); Assert.IsTrue(result.PropertyTypeNodes[0].NoDefaultImplementation); }
public void WhenPropertyIsCustom_SchemaReader_ReturnsCustomProperty() { var result = JsonSchema.FromJson( new JsonSchemaBuilder() .WithNamespace("Unity.Properties.Samples.Schema") .WithContainer( new JsonSchemaBuilder.ContainerBuilder("HelloWorld") .WithProperty("Data", "int", "5", "", "", false, false, true) .WithProperty("Int", "int") ) .ToJson() ); Assert.AreEqual(1, result.PropertyTypeNodes.Count); Assert.IsTrue(result.PropertyTypeNodes[0].Properties[0].IsCustomProperty); Assert.IsFalse(result.PropertyTypeNodes[0].Properties[1].IsCustomProperty); }
public void WhenBasicTypesInSchema_SchemaReadser_ReturnsAValidContainerList() { var injectedBuiltinTypes = new Dictionary <string, PropertyTypeNode.TypeTag> { { "MyStruct", PropertyTypeNode.TypeTag.Struct } }; var result = JsonSchema.FromJson( new JsonSchemaBuilder() .WithNamespace("Unity.Properties.Samples.Schema") .WithContainer( new JsonSchemaBuilder.ContainerBuilder("HelloWorld", true) .WithProperty("Data", "int", "5") .WithProperty("Floats", "list", "", "float") .WithProperty("MyStruct", "SomeData") ) .ToJson(), injectedBuiltinTypes ); Assert.AreEqual(1, result.PropertyTypeNodes.Count); Assert.AreEqual("HelloWorld", result.PropertyTypeNodes[0].TypeName); Assert.IsTrue(result.PropertyTypeNodes[0].Tag == PropertyTypeNode.TypeTag.Struct); Assert.AreEqual(3, result.PropertyTypeNodes[0].Properties.Count); Assert.AreEqual(new System.Collections.Generic.List <string> { "Data", "Floats", "MyStruct" }, result.PropertyTypeNodes[0].Properties.Select(c => c.PropertyName) ); Assert.AreEqual(new System.Collections.Generic.List <string> { "int", "List", "SomeData" }, result.PropertyTypeNodes[0].Properties.Select(c => c.TypeName) ); Assert.AreEqual(new System.Collections.Generic.List <string> { "5", "", "" }, result.PropertyTypeNodes[0].Properties.Select(c => c.DefaultValue) ); Assert.AreEqual(new System.Collections.Generic.List <string> { "", "float", "" }, result.PropertyTypeNodes[0].Properties.Select(c => (c.Of != null ? c.Of.TypeName : string.Empty)) ); }
private static void TestFullCircle(string json, string rootPropertyContainerTypeName) { // json -> csharp var g = new CSharpGenerationBackend(); g.Generate(JsonSchema.FromJson(json).PropertyTypeNodes); // csharp -> assembly var assemblyFilePath = string.Empty; var errors = string.Empty; Assert.IsTrue( CompileTestUtils.TryCompileToFile( g.Code.ToString(), out assemblyFilePath, out errors)); // assembly -> property visitor var assembly = Assembly.LoadFile(assemblyFilePath); Assert.NotNull(assembly); var type = assembly.GetType(rootPropertyContainerTypeName); var container = (IPropertyContainer)Activator.CreateInstance(type); var visitor = new PropertyLeafVisitor(); container.Visit(visitor); Assert.NotZero(visitor.Properties.Count); // -> json var generatedJson = JsonSchema.ToJson( new JsonSchema() { PropertyTypeNodes = ReflectionPropertyTree.Read(assemblyFilePath) } ); Assert.NotNull(generatedJson.Length); }
public string TestContainsPropertyContainerNestedInsidePlainStructOrClass(string parentType) { string assemblyFilePath = string.Empty; string errors = string.Empty; string code = $@" using System.Collections.Generic; using Unity.Properties; public {parentType} HelloWorld {{ public class Foo : IPropertyContainer {{ public static IPropertyBag bag {{ get; }} = new PropertyBag(new List<IProperty> {{}}.ToArray()); public IVersionStorage VersionStorage {{ get; }} public IPropertyBag PropertyBag => bag; }} public Foo foo {{ get; }} }}; "; Assert.IsTrue(CompileTestUtils.TryCompileToFile(code, out assemblyFilePath, out errors), errors); using (new FileDisposer(assemblyFilePath)) { var result = JsonSchema.FromJson( JsonSchema.ToJson( new JsonSchema() { PropertyTypeNodes = ReflectionPropertyTree.Read(assemblyFilePath) } ) ); var containers = new List <PropertyTypeNode>(); VisitContainer(result.PropertyTypeNodes, c => { containers.Add(c); }); Assert.True(containers.Count == 1); return(containers[0].TypeName); } }
static void Main(string[] args) { var json = new JsonObject { ["test1"] = 1, ["test2"] = "hello" }; var serializer = new JsonSerializer(); var obj = serializer.Deserialize <ITest>(json); Console.WriteLine(obj.Test1); Console.WriteLine(obj.Test2); var schema04json = MetaSchemas.Draft04.ToJson(serializer); var schema04 = new JsonSchema(); schema04.FromJson(schema04json, serializer); Console.ReadLine(); }
public void WhenNestedPropertyContainers_SchemaReadser_GeneratesNestedTypeTree() { var result = JsonSchema.FromJson($@" [ {{ ""Version"": ""{JsonSchema.CurrentVersion}"", ""Namespace"": ""Unity.Properties.Samples.Schema"", ""Types"": [ { GetJsonContainerDefinitionWithName( "Root", new List<string>() { GetJsonContainerDefinitionWithName("Root>Foo", new List<string>()), GetJsonContainerDefinitionWithName("Root>Bar", new List<string>()), GetJsonContainerDefinitionWithName("Root>FooBar", new List<string>() { GetJsonContainerDefinitionWithName("Root>FooBar>BarFoo", new List<string>()), }), }) } ] }} ] "); Assert.AreEqual(1, result.PropertyTypeNodes.Count); var classNames = new List <string>(); VisitContainer(result.PropertyTypeNodes, (PropertyTypeNode n) => { classNames.Add(n.TypeName); }); Assert.AreEqual( new List <string> { "Root", "Root>Foo", "Root>Bar", "Root>FooBar", "Root>FooBar>BarFoo" }, classNames ); }
public void WhenAssemblyDoesNotContainPropertyContainers_ReflectionJsonSchemaGenerator_ReturnsAnEmptyJson() { string assemblyFilePath = string.Empty; string errors = string.Empty; string code = @" using System.Collections.Generic; public partial class HelloWorld { public class Foo { public int Data; public List<float> Floats = new List<float>(); } public Foo foo { get; } = new Foo(); }; "; Assert.IsTrue(CompileTestUtils.TryCompileToFile(code, out assemblyFilePath, out errors), errors); using (new FileDisposer(assemblyFilePath)) { var result = JsonSchema.FromJson( JsonSchema.ToJson( new JsonSchema() { PropertyTypeNodes = ReflectionPropertyTree.Read(assemblyFilePath) } ) ); var containers = new List <PropertyTypeNode>(); VisitContainer(result.PropertyTypeNodes, c => { containers.Add(c); }); Assert.Zero(containers.Count); } }
public void WhenPropertyIsNotPublic_SchemaReadser_Json() { // Do a roundtrip var result = JsonSchema.FromJson( JsonSchema.ToJson( JsonSchema.FromJson( new JsonSchemaBuilder() .WithContainer( new JsonSchemaBuilder.ContainerBuilder("HelloWorld", true) .WithProperty("Data", "int", "5", "", "", false, true) .WithProperty("Floats", "list", "", "float") ) .ToJson() ) ) ); Assert.AreEqual(new List <bool> { true, false, }, result.PropertyTypeNodes[0].Properties.Select(c => c.IsPublicProperty) ); }
public void DontInitializeBackingField_PropertyFlag_Is_ProperlyRead() { // Do a roundtrip var result = JsonSchema.FromJson( JsonSchema.ToJson( JsonSchema.FromJson( new JsonSchemaBuilder() .WithContainer( new JsonSchemaBuilder.ContainerBuilder("HelloWorld", true) .WithProperty("Data", "int", "", "", "", false, false, false, true) .WithProperty("Data1", "int", "", "", "", false, false, false, false) ) .ToJson() ) ) ); Assert.AreEqual(new List <bool> { true, false }, result.PropertyTypeNodes[0].Properties.Select(c => c.DontInitializeBackingField) ); }
public void WhenAssemblyContainsNestedPropertyContainers_ReflectionJsonSchemaGenerator_ReturnsAValidJson() { string assemblyFilePath = string.Empty; string errors = string.Empty; string code = @" using System.Collections.Generic; using Unity.Properties; namespace Unity.Properties.TestCases { public partial class HelloWorld : IPropertyContainer { public static IPropertyBag bag { get; } = new PropertyBag(new List<IProperty> {}.ToArray()); public IVersionStorage VersionStorage { get; } public IPropertyBag PropertyBag => bag; public class Foo : IPropertyContainer { public static IPropertyBag bag { get; } = new PropertyBag(new List<IProperty> {}.ToArray()); public IVersionStorage VersionStorage { get; } public IPropertyBag PropertyBag => bag; public class Bar : IPropertyContainer { public static IPropertyBag bag { get; } = new PropertyBag(new List<IProperty> {}.ToArray()); public IVersionStorage VersionStorage { get; } public IPropertyBag PropertyBag => bag; } } }; } "; Assert.IsTrue(CompileTestUtils.TryCompileToFile(code, out assemblyFilePath, out errors), errors); using (new FileDisposer(assemblyFilePath)) { var result = JsonSchema.FromJson( JsonSchema.ToJson( new JsonSchema() { PropertyTypeNodes = ReflectionPropertyTree.Read(assemblyFilePath) } ) ); var containers = new List <PropertyTypeNode>(); VisitContainer(result.PropertyTypeNodes, c => { containers.Add(c); }); var containerNames = containers.Select(c => c.TypeName).ToList(); Assert.AreEqual( new System.Collections.Generic.List <string> { "HelloWorld", "Foo", "Bar" }, containerNames ); } }
public void WhenEmptyStringForSchema_SchemaReader_ReturnsAnEmptyContainerList() { Assert.Throws <Exception>(() => JsonSchema.FromJson(string.Empty)); }