Path: redb-route/tests/redb.Route.Tests.DataFormats/XmlJsonSchemaTests.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.DataFormats/XmlJsonSchemaTests.cs ---- using System.Text; using System.Xml; using System.Xml.Schema; using AwesomeAssertions; using redb.Route.DataFormats.XmlJson; using redb.Route.DataFormats.XmlJson.Internal; namespace redb.Route.Tests.DataFormats; /// /// xmljson with a schema (step S3): repetition from the compiled particles (element maxOccurs, a repeated sequence or /// choice, a ref whose maxOccurs sits on the particle, groups, extension, substitution, recursion, xs:any), typed /// values, element content as an object, imports and includes from the package only. /// public sealed class XmlJsonSchemaTests : IDisposable { private readonly string _dir = Path.Combine(Path.GetTempPath(), "redb-xmljson-xsd-" + Guid.NewGuid().ToString("N")); public XmlJsonSchemaTests() => Directory.CreateDirectory(_dir); public void Dispose() => Directory.Delete(_dir, recursive: true); private const string Order = """ """; private static XmlSchemaSet Set(string xsd) { var set = new XmlSchemaSet(); using var reader = XmlReader.Create(new StringReader(xsd)); set.Add(null, reader); return set; } private static string Json(string xml, XmlSchemaSet set, Action? configure = null) { var options = new XmlJsonOptions { SchemaSet = set, Namespaces = XmlJsonNamespaces.Drop }; configure?.Invoke(options); var converter = new XmlToJson(options); return Encoding.UTF8.GetString(converter.Write(converter.Convert(Encoding.UTF8.GetBytes(xml)))); } [Fact] public void TypesAndRepetition_FromTheSchema() { Json(""" 7true12.500.5007 pen """, Set(Order)) .Should().Be("""{"Order":{"Id":7,"Paid":true,"Total":12.50,"Rate":0.5,"Code":"007","Items":{"Item":[{"@qty":2,"#text":"pen"}]},"Tags":{}}}"""); } [Fact] public void OneAndMany_HaveTheSameShape() { const string one = "101aa"; const string two = "101aab"; Json(one, Set(Order)).Should().Contain("\"Item\":[\"a\"]"); Json(two, Set(Order)).Should().Contain("\"Item\":[\"a\",\"b\"]"); } [Fact] public void Nillable_IsNull() { Json("""111aa""", Set(Order)) .Should().Contain("\"Note\":null"); } [Theory] [InlineData("x11aa", "xmljson: /Order/Id holds 'x', which is not an xs:int as the schema declares.")] [InlineData("1yes1aa", "xmljson: /Order/Paid holds 'yes', which is not an xs:boolean as the schema declares.")] [InlineData("111aa", "xmljson: /Order/Items/Item/@qty holds 'many', which is not an xs:int as the schema declares.")] [InlineData("1", "xmljson: /Order holds 'Extra', which its content in the schema has no place for.")] [InlineData("", "xmljson: the root element 'Other' (no namespace) is not a global element of the schema.")] public void WhatTheSchemaDoesNotAllow_IsAnError(string xml, string message) { var act = () => Json(xml, Set(Order)); act.Should().Throw().WithMessage(message); } [Fact] public void ARepeatedChoice_MakesEachBranchAnArray() { const string xsd = """ """; Json("1", Set(xsd)).Should().Be("""{"r":{"a":["1"]}}"""); } [Fact] public void AnElementTwiceInOneSequence_IsAnArray() { const string xsd = """ """; Json("12", Set(xsd)).Should().Be("""{"r":{"a":["1"],"b":"2"}}"""); } [Fact] public void ARefWithMaxOccursOnTheParticle_Repeats() { const string xsd = """ """; Json("5", Set(xsd)).Should().Be("""{"r":{"Line":[5]}}"""); } [Fact] public void GroupsAndExtension_AreFollowed() { const string xsd = """ """; Json("12", Set(xsd)).Should().Be("""{"r":{"Id":1,"Line":[2]}}"""); } [Fact] public void ASubstitutionGroupMember_TakesTheHeadsPlace() { const string xsd = """ """; Json("ann", Set(xsd)).Should().Be("""{"r":{"Buyer":["ann"]}}"""); } [Fact] public void ARecursiveType_IsFollowedAsDeepAsTheDocument() { const string xsd = """ """; Json("123", Set(xsd)) .Should().Be("""{"node":{"v":1,"node":[{"v":2,"node":[{"v":3}]}]}}"""); } [Fact] public void UnderAWildcard_AGlobalDeclarationIsUsed_ElseTheFallback() { const string xsd = """ """; Json("4123", Set(xsd)) .Should().Be("""{"r":{"Known":[4],"Free":[{"x":["1","2"],"y":"3"}]}}"""); } [Fact] public void ArraysPaths_StillAddArraysTheSchemaDoesNotKnow() { const string xsd = """ """; Json("1", Set(xsd), o => o.Arrays.Add("/r/a")).Should().Be("""{"r":{"a":[1]}}"""); } [Fact] public void Namespaces_TheSchemaMatchesQualifiedNames() { const string xsd = """ """; Json("""1""", Set(xsd), o => o.Namespaces = XmlJsonNamespaces.Keep) .Should().Be("""{"o:Order":{"@xmlns:o":"urn:o","o:Id":[1]}}"""); } [Fact] public void ImportsAndIncludes_ResolveInsideTheRoot() { Directory.CreateDirectory(Path.Combine(_dir, "main")); Directory.CreateDirectory(Path.Combine(_dir, "common")); File.WriteAllText(Path.Combine(_dir, "common", "types.xsd"), """ """); File.WriteAllText(Path.Combine(_dir, "main", "lines.xsd"), """ """); File.WriteAllText(Path.Combine(_dir, "main", "order.xsd"), """ """); var options = new XmlJsonOptions { Schema = Path.Combine(_dir, "main", "order.xsd"), SchemaRoot = _dir, Namespaces = XmlJsonNamespaces.Drop }; var converter = new XmlToJson(options); Encoding.UTF8.GetString(converter.Write(converter.Convert(Encoding.UTF8.GetBytes("3")))) .Should().Be("""{"Order":{"Qty":[3]}}"""); } [Fact] public void AnImportOutsideTheRoot_IsRefused() { Directory.CreateDirectory(Path.Combine(_dir, "main")); File.WriteAllText(Path.Combine(_dir, "types.xsd"), """"""); File.WriteAllText(Path.Combine(_dir, "main", "order.xsd"), """ """); var act = () => new XmlToJson(new XmlJsonOptions { Schema = Path.Combine(_dir, "main", "order.xsd") }); act.Should().Throw().WithMessage("*outside*imports and includes stay inside the package*"); } [Fact] public void AnImportFromTheNetwork_IsRefused() { File.WriteAllText(Path.Combine(_dir, "order.xsd"), """ """); var act = () => new XmlToJson(new XmlJsonOptions { Schema = Path.Combine(_dir, "order.xsd") }); act.Should().Throw().WithMessage("*from the package only, not the network*"); } }