public async Task InlineSchema_ShouldSerializeToCSharp() { var org = "yabbin"; var app = "datamodelling"; var jsonSchemaString = @"{""properties"":{""melding"":{""properties"":{""test"":{""type"":""object"",""properties"":{""navn"":{""type"":""string""}}}},""type"":""object""}},""definitions"":{}, ""required"": [""melding""]}"; JsonSchema jsonSchema = await ParseJsonSchema(jsonSchemaString); ModelMetadata modelMetadata = GenerateModelMetadata(org, app, jsonSchema); string classes = GenerateCSharpClasses(modelMetadata); Assembly assembly = Compiler.CompileToAssembly(classes); Type type = assembly.GetType("Altinn.App.Models.melding"); // Make sure the JSON can be serialized into the generated C# class // var json = @"{""melding"":{""test"":{""navn"":""Ronny""}}}"; var json = @"{""test"":{""navn"":""Ronny""}}"; object jsonObj = JsonSerializer.Deserialize(json, type); // Make sure the serialized JSON equals what we expect var jsonSerialized = new Manatee.Json.Serialization.JsonSerializer().Serialize(jsonObj); Assert.Equal(json, jsonSerialized.ToString()); // Make sure the serialized JSON validates var jsonValidationResult = jsonSchema.Validate(new JsonValue(jsonSerialized.ToString())); Assert.True(jsonValidationResult.IsValid); // Validate JSON against JSON Schema (Manatee seems to think this is fine, but it's not ref. https://www.jsonschemavalidator.net/). jsonValidationResult = jsonSchema.Validate(new JsonValue(json), new JsonSchemaOptions() { }); Assert.True(jsonValidationResult.IsValid); // Make sure the xml can be deserialized var xml = "<melding><test><navn>Ronny</navn></test></melding>"; object xmlObj = SerializationHelper.Deserialize(xml, type); // Validate XML against generated XSD // OBS! On inline schemas the generated XSD only adds the root node, and does not traverse the properties. // This should be handled in the new XSD generator. JsonSchemaToXsd jsonSchemaToXsd = new JsonSchemaToXsd(); XmlSchema xmlSchema = jsonSchemaToXsd.CreateXsd(jsonSchema); var xmlSchemaValidator = new XmlSchemaValidator(xmlSchema); Assert.True(xmlSchemaValidator.Validate(xml)); // Do a deep compare, property by property, value by value jsonObj.Should().BeEquivalentTo(xmlObj); }
public void SeresSchema_ShouldSerializeToCSharp(string resourceName, string modelName, string json, string xml) { var org = "yabbin"; var app = "hvem-er-hvem"; JsonSchema jsonSchema = TestDataHelper.LoadDataFromEmbeddedResourceAsJsonSchema(resourceName); ModelMetadata modelMetadata = GenerateModelMetadata(org, app, jsonSchema); string classes = GenerateCSharpClasses(modelMetadata); Assembly assembly = Compiler.CompileToAssembly(classes); Type type = assembly.GetType(modelName); // Make sure the JSON can be serialized into the generated C# class object jsonObj = JsonSerializer.Deserialize(json, type); // Make sure the serialized JSON equals what we expect var jsonSerialized = new Manatee.Json.Serialization.JsonSerializer().Serialize(jsonObj); Assert.Equal(json, jsonSerialized.ToString()); // Make sure the serialized JSON validates // Manatee fails on this, but not https://www.jsonschemavalidator.net/ // var jsonValidationResult = jsonSchema.Validate(new JsonValue(jsonSerialized.ToString())); // Assert.True(jsonValidationResult.IsValid); // Validate JSON against JSON Schema (Manatee seems to think this is fine, but it's not ref. https://www.jsonschemavalidator.net/). // jsonValidationResult = jsonSchema.Validate(new JsonValue(json), new JsonSchemaOptions() { }); // Assert.True(jsonValidationResult.IsValid); // Make sure the xml can be deserialized object xmlObj = SerializationHelper.Deserialize(xml, type); // Validate XML against generated XSD // OBS! On inline schemas the generated XSD only adds the root node, and does not traverse the properties. // This should be handled in the new XSD generator. JsonSchemaToXsd jsonSchemaToXsd = new JsonSchemaToXsd(); XmlSchema xmlSchema = jsonSchemaToXsd.CreateXsd(jsonSchema); var xmlSchemaValidator = new XmlSchemaValidator(xmlSchema); Assert.True(xmlSchemaValidator.Validate(xml)); // Do a deep compare, property by property, value by value jsonObj.Should().BeEquivalentTo(xmlObj); }