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> GetDatamodel(string org, string repository, string filepath) { try { Stream dataStream = await _repository.ReadData(org, repository, $"{filepath}.schema.json"); TextReader textReader = new StreamReader(dataStream); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); return(Ok(jsonValue.ToString())); } catch { // Will fallback to checking for XSD. See below } try { Stream dataStream = await _repository.ReadData(org, repository, $"{filepath}.xsd"); XmlReader xsdReader = XmlReader.Create(dataStream); XsdToJsonSchema xsdToJsonSchemaConverter = new XsdToJsonSchema(xsdReader); // Act JsonSchema convertedSchema = xsdToJsonSchemaConverter.AsJsonSchema(); Manatee.Json.Serialization.JsonSerializer serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue serializedConvertedSchema = serializer.Serialize(convertedSchema); return(Ok(serializedConvertedSchema.ToString())); } catch { return(NotFound()); } }
private void ValidateJsonManatee() { try { txtError.Text = ""; var serializer = new Manatee.Json.Serialization.JsonSerializer(); var json = Manatee.Json.JsonValue.Parse(txtJson.Text); var schemaJson = Manatee.Json.JsonValue.Parse(txtSchema.Text); var schema = new Manatee.Json.Schema.JsonSchema(); schema.FromJson(schemaJson, serializer); var options = new Manatee.Json.Schema.JsonSchemaOptions(); options.OutputFormat = Manatee.Json.Schema.SchemaValidationOutputFormat.Basic; var validationResults = schema.Validate(json, options); if (!validationResults.IsValid) { json = serializer.Serialize(validationResults); txtError.Text = JsonHelper.FormatJson(json.ToString()); } else { txtError.Text = "Valid"; } } catch (Exception ex) { txtError.Text = "Parse error: " + ex.Message; } }
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 Root Manatee_Deserialize() { var jsonSerializer = new Manatee.Json.Serialization.JsonSerializer(); var jsonValue = JsonValue.Parse(FirstJsonSampleString); return(jsonSerializer.Deserialize <Root>(jsonValue)); }
private static async Task <JsonSchema> DeserializeJson(string content) { TextReader textReader = new StringReader(content); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); JsonSchema jsonSchema = new Manatee.Json.Serialization.JsonSerializer().Deserialize <JsonSchema>(jsonValue); return(jsonSchema); }
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 async Task <IActionResult> GetDatamodel(string org, string repository, string modelName) { try { modelName = modelName.AsFileName(); } catch { return(BadRequest("Invalid model name value.")); } string filePath = $"App/models/{modelName}"; try { using (Stream dataStream = await _repository.ReadData(org, repository, $"{filePath}.schema.json")) { TextReader textReader = new StreamReader(dataStream); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); return(Ok(jsonValue.ToString())); } } catch { // Will fallback to checking for XSD. See below } try { using (Stream dataStream = await _repository.ReadData(org, repository, $"{filePath}.xsd")) { XmlReader xsdReader = XmlReader.Create(dataStream); XsdToJsonSchema xsdToJsonSchemaConverter = new XsdToJsonSchema(xsdReader); JsonSchema convertedSchema = xsdToJsonSchemaConverter.AsJsonSchema(); Manatee.Json.Serialization.JsonSerializer serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue serializedConvertedSchema = serializer.Serialize(convertedSchema); return(Ok(serializedConvertedSchema.ToString())); } } catch { return(NotFound()); } }
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); }
public async Task <IActionResult> Schemas() { AltinnServiceRepository repositoryClient = new AltinnServiceRepository(); Task <List <AltinnResource> > serviceRequestTask = AltinnServiceRepository.ReadAllSchemas(); await Task.WhenAll(serviceRequestTask); if (serviceRequestTask.Result != null) { Manatee.Json.Serialization.JsonSerializer serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue json = serializer.Serialize(serviceRequestTask.Result); return(Ok(json.GetIndentedString())); } return(NoContent()); }
public IJsonSchema GetSchema(string fileName) { return(_store.GetOrAdd(fileName, _ => { var assembly = Assembly.GetAssembly(typeof(iSHARE.IdentityServer .Configuration)); using (var stream = assembly.GetManifestResourceStream( $"{typeof(iSHARE.IdentityServer.Configuration).Namespace}.Schemas.{fileName}")) { using (var reader = new StreamReader(stream)) { var content = reader.ReadToEnd(); var schema = new Manatee.Json.Serialization.JsonSerializer() .Deserialize <IJsonSchema>(JsonValue.Parse(content)); return schema; } } })); }
public async Task <IActionResult> Convert() { XmlReaderSettings settings = new XmlReaderSettings { IgnoreWhitespace = true, }; if (Request.ContentType.Contains("text/xml")) { XmlReader doc = XmlReader.Create(Request.Body, settings); // XSD to Json Schema XsdToJsonSchema xsdToJsonSchemaConverter = new XsdToJsonSchema(doc, null); Manatee.Json.Serialization.JsonSerializer serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue json = serializer.Serialize(xsdToJsonSchemaConverter.AsJsonSchema()); return(Ok(json.GetIndentedString())); } return(NotFound("Cannot read body. Needs to be XSD.")); }
public static async Task <Configuration> LoadConfigurationAsync(string configurationFilenameOrUrl) { JObject localconfiguration = null; if (!string.IsNullOrWhiteSpace(configurationFilenameOrUrl)) { string configurationContent; // Load the job definition from a url or locally try { if (configurationFilenameOrUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase)) { configurationContent = await _httpClient.GetStringAsync(configurationFilenameOrUrl); } else { configurationContent = File.ReadAllText(configurationFilenameOrUrl); } } catch { throw new RegressionBotException($"Configuration '{configurationFilenameOrUrl}' could not be loaded."); } // Detect file extension string configurationExtension = null; if (configurationFilenameOrUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase)) { // Remove any query string to detect the correct extension var questionMarkIndex = configurationFilenameOrUrl.IndexOf("?"); if (questionMarkIndex != -1) { var filename = configurationFilenameOrUrl.Substring(0, questionMarkIndex); configurationExtension = Path.GetExtension(filename); } else { configurationExtension = Path.GetExtension(configurationFilenameOrUrl); } } else { configurationExtension = Path.GetExtension(configurationFilenameOrUrl); } switch (configurationExtension) { case ".json": localconfiguration = JObject.Parse(configurationContent); break; case ".yml": case ".yaml": var deserializer = new DeserializerBuilder() .WithNodeTypeResolver(new JsonTypeResolver()) .Build(); object yamlObject; try { yamlObject = deserializer.Deserialize(new StringReader(configurationContent)); } catch (YamlDotNet.Core.SyntaxErrorException e) { throw new RegressionBotException($"Error while parsing '{configurationFilenameOrUrl}'\n{e.Message}"); } var serializer = new SerializerBuilder() .JsonCompatible() .Build(); var json = serializer.Serialize(yamlObject); // Format json in case the schema validation fails and we need to render error line numbers localconfiguration = JObject.Parse(json); var schemaJson = File.ReadAllText(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "regressionbot.schema.json")); var schema = new Manatee.Json.Serialization.JsonSerializer().Deserialize <JsonSchema>(JsonValue.Parse(schemaJson)); var jsonToValidate = JsonValue.Parse(json); var validationResults = schema.Validate(jsonToValidate, new JsonSchemaOptions { OutputFormat = SchemaValidationOutputFormat.Detailed }); if (!validationResults.IsValid) { // Create a json debug file with the schema localconfiguration.AddFirst(new JProperty("$schema", "https://raw.githubusercontent.com/dotnet/crank/master/src/Microsoft.Crank.RegressionBot/regressionbot.schema.json")); var debugFilename = Path.Combine(Path.GetTempPath(), "configuration.debug.json"); File.WriteAllText(debugFilename, localconfiguration.ToString(Formatting.Indented)); var errorBuilder = new StringBuilder(); errorBuilder.AppendLine($"Invalid configuration file '{configurationFilenameOrUrl}' at '{validationResults.InstanceLocation}'"); errorBuilder.AppendLine($"{validationResults.ErrorMessage}"); errorBuilder.AppendLine($"Debug file created at '{debugFilename}'"); throw new RegressionBotException(errorBuilder.ToString()); } break; default: throw new RegressionBotException($"Unsupported configuration format: {configurationExtension}"); } return(localconfiguration.ToObject <Configuration>()); } else { throw new RegressionBotException($"Invalid file path or url: '{configurationFilenameOrUrl}'"); } }