public async Task <IActionResult> UpdateDatamodel(string org, string app, string filepath) { SchemaKeywordCatalog.Add <InfoKeyword>(); using (Stream resource = Request.Body) { // Read the request body and deserialize to Json Schema using StreamReader streamReader = new StreamReader(resource); string content = await streamReader.ReadToEndAsync(); TextReader textReader = new StringReader(content); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); JsonSchema jsonSchemas = new Manatee.Json.Serialization.JsonSerializer().Deserialize <JsonSchema>(jsonValue); // Serialize and store the Json Schema var serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue toar = serializer.Serialize(jsonSchemas); byte[] byteArray = Encoding.UTF8.GetBytes(toar.ToString()); MemoryStream jsonstream = new MemoryStream(byteArray); await _repository.WriteData(org, app, $"{filepath}.schema.json", jsonstream); // Convert to XML Schema and store in repository JsonSchemaToXsd jsonSchemaToXsd = new JsonSchemaToXsd(); XmlSchema xmlschema = jsonSchemaToXsd.CreateXsd(jsonSchemas); MemoryStream xsdStream = new MemoryStream(); XmlTextWriter xwriter = new XmlTextWriter(xsdStream, new UpperCaseUtf8Encoding()); xwriter.Formatting = Formatting.Indented; xwriter.WriteStartDocument(false); xmlschema.Write(xsdStream); await _repository.WriteData(org, app, $"{filepath}.xsd", xsdStream); } return(Ok()); }
public async Task <IActionResult> UpdateDatamodel(string org, string app, string modelName) { SchemaKeywordCatalog.Add <InfoKeyword>(); try { modelName = modelName.AsFileName(); } catch { return(BadRequest("Invalid model name value.")); } string filePath = $"App/models/{modelName}"; using (Stream resource = Request.Body) { // Read the request body and deserialize to Json Schema using StreamReader streamReader = new StreamReader(resource); string content = await streamReader.ReadToEndAsync(); TextReader textReader = new StringReader(content); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); JsonSchema jsonSchemas = new Manatee.Json.Serialization.JsonSerializer().Deserialize <JsonSchema>(jsonValue); // Create the directory if it does not exist string appPath = _repository.GetAppPath(org, app); string directory = appPath + Path.GetDirectoryName(filePath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } // Serialize and store the Json Schema var serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue toar = serializer.Serialize(jsonSchemas); byte[] byteArray = Encoding.UTF8.GetBytes(toar.ToString()); MemoryStream jsonstream = new MemoryStream(byteArray); await _repository.WriteData(org, app, $"{filePath}.schema.json", jsonstream); // update meta data JsonSchemaToInstanceModelGenerator converter = new JsonSchemaToInstanceModelGenerator(org, app, jsonSchemas); ModelMetadata modelMetadata = converter.GetModelMetadata(); string root = modelMetadata.Elements != null && modelMetadata.Elements.Count > 0 ? modelMetadata.Elements.Values.First(e => e.ParentElement == null).TypeName : null; _repository.UpdateApplicationWithAppLogicModel(org, app, modelName, "Altinn.App.Models." + root); // Convert to XML Schema and store in repository JsonSchemaToXsd jsonSchemaToXsd = new JsonSchemaToXsd(); XmlSchema xmlschema = jsonSchemaToXsd.CreateXsd(jsonSchemas); MemoryStream xsdStream = new MemoryStream(); XmlTextWriter xwriter = new XmlTextWriter(xsdStream, new UpperCaseUtf8Encoding()); xwriter.Formatting = Formatting.Indented; xwriter.WriteStartDocument(false); xmlschema.Write(xsdStream); await _repository.WriteData(org, app, $"{filePath}.xsd", xsdStream); } return(Ok()); }
public async void ConvertXsdToJsonSchemaAndBackViaString_CorrectNumberOfPropertiesAndDefinitions() { // string xsdName = "Designer.Tests._TestData.Model.Xsd.schema_3451_8_forms_4106_35721.xsd"; string xsdName = "Designer.Tests._TestData.Model.Xsd.schema_4581_100_forms_5245_41111.xsd"; SchemaKeywordCatalog.Add <InfoKeyword>(); // Arrange XmlReader xsdReader = XmlReader.Create(LoadTestData(xsdName)); XsdToJsonSchema xsdToJsonSchemaConverter = new XsdToJsonSchema(xsdReader); // Act JsonSchema convertedSchema = xsdToJsonSchemaConverter.AsJsonSchema(); JsonSerializer serializer = new JsonSerializer(); JsonValue serializedConvertedSchema = serializer.Serialize(convertedSchema); byte[] byteArray = Encoding.UTF8.GetBytes(serializedConvertedSchema.ToString()); MemoryStream jsonstream = new MemoryStream(byteArray); await WriteData(xsdName + ".json", jsonstream); File.WriteAllText(xsdName + ".json", serializedConvertedSchema.ToString()); string savedJsonSchemaFileTextContent = File.ReadAllText(xsdName + ".json"); TextReader textReader = new StringReader(savedJsonSchemaFileTextContent); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); JsonSchema jsonSchemaFromFile = new Manatee.Json.Serialization.JsonSerializer().Deserialize <JsonSchema>(jsonValue); JsonSchemaToXsd jsonSchemaToXsd = new JsonSchemaToXsd(); XmlSchema xmlschema = jsonSchemaToXsd.CreateXsd(convertedSchema); MemoryStream xmlStream = new MemoryStream(); XmlTextWriter xwriter = new XmlTextWriter(xmlStream, new UpperCaseUtf8Encoding()); xwriter.Formatting = Formatting.Indented; xwriter.WriteStartDocument(false); xmlschema.Write(xmlStream); await WriteData(xsdName + ".new", xmlStream); XmlSchema xmlschemaFromFile = jsonSchemaToXsd.CreateXsd(jsonSchemaFromFile); MemoryStream xmlStreamFile = new MemoryStream(); XmlTextWriter xwriterFile = new XmlTextWriter(xmlStreamFile, new UpperCaseUtf8Encoding()); xwriterFile.Formatting = Formatting.Indented; xwriterFile.WriteStartDocument(false); xmlschemaFromFile.Write(xmlStreamFile); await WriteData(xsdName + ".newfile", xmlStreamFile); Assert.NotNull(convertedSchema); }
public void SeresOrXmlSchema_ShouldSerializeToCSharp(string xsdResource, string modelName, string expectedJsonSchemaResource, string expectedCSharpResource) { SchemaKeywordCatalog.Add <InfoKeyword>(); var org = "yabbin"; var app = "hvem-er-hvem"; Stream xsdStream = TestDataHelper.LoadDataFromEmbeddedResource(xsdResource); XmlReader xmlReader = XmlReader.Create(xsdStream, new XmlReaderSettings { IgnoreWhitespace = true }); // Compare generated JSON Schema XsdToJsonSchema xsdToJsonSchemaConverter = new XsdToJsonSchema(xmlReader); JsonSchema jsonSchema = xsdToJsonSchemaConverter.AsJsonSchema(); var expectedJsonSchema = TestDataHelper.LoadDataFromEmbeddedResourceAsJsonSchema(expectedJsonSchemaResource); jsonSchema.Should().BeEquivalentTo(expectedJsonSchema); // Compare generated C# classes ModelMetadata modelMetadata = GenerateModelMetadata(org, app, jsonSchema); string classes = GenerateCSharpClasses(modelMetadata); Assembly assembly = Compiler.CompileToAssembly(classes); Type type = assembly.GetType(modelName); var modelInstance = assembly.CreateInstance(type.FullName); string expectedClasses = TestDataHelper.LoadDataFromEmbeddedResourceAsString(expectedCSharpResource); Assembly expectedAssembly = Compiler.CompileToAssembly(expectedClasses); Type expectedType = expectedAssembly.GetType(modelName); var expectedModelInstance = expectedAssembly.CreateInstance(expectedType.FullName); expectedType.HasSameMetadataDefinitionAs(type); modelInstance.Should().BeEquivalentTo(expectedModelInstance); type.Should().BeDecoratedWith <XmlRootAttribute>(); }