/// <summary> /// Parses the XSD, generates Json Schema and generates the meta model using /// the new classes. /// </summary> private static ModelMetadata CreateMetamodelNewWay(string xsdResource, string org, string app) { Stream xsdStream = TestDataHelper.LoadDataFromEmbeddedResource(xsdResource); XmlReader xmlReader = XmlReader.Create(xsdStream, new XmlReaderSettings { IgnoreWhitespace = true }); var xmlSchema = XmlSchema.Read(xmlReader, (_, _) => { }); var schemaSet = new XmlSchemaSet(); schemaSet.Add(xmlSchema); schemaSet.Compile(); var xsdToJsonConverter = new XmlSchemaToJsonSchemaConverter(); Json.Schema.JsonSchema convertedJsonSchema = xsdToJsonConverter.Convert(xmlSchema); var convertedJsonSchemaString = JsonSerializer.Serialize(convertedJsonSchema, new JsonSerializerOptions() { Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement), WriteIndented = true }); var metamodelConverter = new JsonSchemaToMetamodelConverter(new SeresJsonSchemaAnalyzer()); ModelMetadata actualMetamodel = metamodelConverter.Convert("melding", convertedJsonSchemaString); return(actualMetamodel); }
public void Convert_FromSeresSchema_ShouldConvert(string xsdSchemaPath, string expectedMetamodelPath) { // Arrange // Convert the Seres XSD to JSON Schema XmlSchema originalXsd = TestDataHelper.LoadXmlSchemaTestData(xsdSchemaPath); var xsdToJsonConverter = new XmlSchemaToJsonSchemaConverter(); JsonSchema convertedJsonSchema = xsdToJsonConverter.Convert(originalXsd); var convertedJsonSchemaString = JsonSerializer.Serialize(convertedJsonSchema, new JsonSerializerOptions() { Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement), WriteIndented = true }); // Convert to Metadata model var metamodelConverter = new JsonSchemaToMetamodelConverter(new SeresJsonSchemaAnalyzer()); metamodelConverter.KeywordProcessed += KeywordProcessedHandler; metamodelConverter.SubSchemaProcessed += SubSchemaProcessedHandler; // Act var actualMetamodel = metamodelConverter.Convert("melding", convertedJsonSchemaString); // Leaving this for easy access/manual verification var actualMetamodelJson = JsonSerializer.Serialize(actualMetamodel, new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true, Converters = { new JsonStringEnumConverter() } }); // Assert var expectedMetamodelJson = TestDataHelper.LoadTestDataFromFileAsString(expectedMetamodelPath); var expectedMetamodel = JsonSerializer.Deserialize <ModelMetadata>(expectedMetamodelJson, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true, Converters = { new JsonStringEnumConverter() } }); MetadataAssertions.IsEquivalentTo(expectedMetamodel, actualMetamodel); actualMetamodel.Elements.Values.Where(e => e.ParentElement == null).ToList().Count.Should().Be(1); // Compile the generated class to verify it compiles without errors var classes = new JsonMetadataParser().CreateModelFromMetadata(actualMetamodel); var compiledAssembly = Compiler.CompileToAssembly(classes); compiledAssembly.Should().NotBeNull(); }
private async Task <string> ProcessNewXsd( AltinnAppGitRepository altinnAppGitRepository, MemoryStream xsdMemoryStream, string filePath) { Json.Schema.JsonSchema jsonSchema = GenerateJsonSchemaFromXsd(xsdMemoryStream); var jsonContent = SerializeJson(jsonSchema); await altinnAppGitRepository.WriteTextByRelativePathAsync( Path.ChangeExtension(filePath, "schema.json"), jsonContent, true); var jsonSchemaConverterStrategy = JsonSchemaConverterStrategyFactory.SelectStrategy(jsonSchema); var metamodelConverter = new JsonSchemaToMetamodelConverter(jsonSchemaConverterStrategy.GetAnalyzer()); var schemaName = GetSchemaName(filePath); var modelMetadata = metamodelConverter.Convert(schemaName, jsonContent); await altinnAppGitRepository.UpdateModelMetadata(modelMetadata, schemaName); await UpdateCSharpClasses(altinnAppGitRepository, modelMetadata, schemaName); await UpdateApplicationMetadata(altinnAppGitRepository, schemaName, schemaName); return(jsonContent); }