Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: xsd:all support #13974

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private void ParseFieldProperty(ElementMetadata element, StringBuilder classBuil
else
{
elementOrder += 1;
classBuilder.AppendLine(Indent(2) + "[XmlElement(\"" + element.XName + "\", Order = " + elementOrder + ")]");
AddXmlElementAnnotation(element, classBuilder, elementOrder);

// Temporary fix - as long as we use System.Text.Json for serialization and Newtonsoft.Json for
// deserialization, we need both JsonProperty and JsonPropertyName annotations.
Expand Down Expand Up @@ -223,7 +223,7 @@ private void ParseGroupProperty(ElementMetadata element, StringBuilder classBuil
var nullableReference = useNullableReferenceTypes ? "?" : string.Empty;
WriteRestrictionAnnotations(classBuilder, element);
elementOrder += 1;
classBuilder.AppendLine(Indent(2) + "[XmlElement(\"" + element.XName + "\", Order = " + elementOrder + ")]");
AddXmlElementAnnotation(element, classBuilder, elementOrder);

// Temporary fix - as long as we use System.Text.Json for serialization and Newtonsoft.Json for
// deserialization, we need both JsonProperty and JsonPropertyName annotations.
Expand Down Expand Up @@ -265,6 +265,18 @@ private void ParseGroupProperty(ElementMetadata element, StringBuilder classBuil
}
}

private void AddXmlElementAnnotation(ElementMetadata element, StringBuilder classBuilder, int elementOrder)
{
if (element.OrderOblivious)
{
classBuilder.AppendLine($"""{Indent(2)}[XmlElement("{element.XName}")]""");
}
else
{
classBuilder.AppendLine($"""{Indent(2)}[XmlElement("{element.XName}", Order = {elementOrder})]""");
}
}

private void AddShouldSerializeForTagContent(ElementMetadata element, StringBuilder classBuilder, ModelMetadata modelMetadata)
{
var children = modelMetadata.Elements.Values.Where(metadata =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ private sealed class SchemaContext

public bool XmlText { get; set; }


public bool OrderOblivious { get; set; } = false;

public Dictionary<string, Restriction> Restrictions { get; set; } = new();
}

Expand Down Expand Up @@ -185,6 +188,10 @@ private void ProcessKeyword(JsonPointer path, IJsonSchemaKeyword keyword, Schema
ProcessPropertiesKeyword(path, k, context);
break;

case XsdStructureKeyword { Value: "all" }:
context.OrderOblivious = true;
break;

default:
throw new MetamodelConvertException($"Keyword {keyword.Keyword()} not processed!. It's not supported in the current version of the JsonSchemaToMetamodelConverter.");
}
Expand Down Expand Up @@ -267,7 +274,7 @@ private void ProcessPropertiesKeyword(JsonPointer path, PropertiesKeyword keywor
{
foreach (var (name, property) in keyword.Properties)
{
var currentContext = new SchemaContext() { Id = CombineId(context.Id, name), Name = name, ParentId = context.Id, XPath = CombineXPath(context.XPath, context.Name) };
var currentContext = new SchemaContext() { Id = CombineId(context.Id, name), Name = name, ParentId = context.Id, XPath = CombineXPath(context.XPath, context.Name), OrderOblivious = context.OrderOblivious };
var subSchemaPath = path.Combine(JsonPointer.Parse($"/{name}"));

if (property.TryGetKeyword(out XsdTextKeyword xsdTextKeyword))
Expand Down Expand Up @@ -364,7 +371,7 @@ private void ProcessNonPrimitiveType(JsonPointer path, JsonSchema subSchema, Sch
{
ProcessRegularType(path, subSchema, context);

foreach (var keyword in subSchema.Keywords)
foreach (var keyword in subSchema.Keywords.OrderByPriority())
{
var keywordPath = path.Combine(JsonPointer.Parse($"/{keyword.Keyword()}"));

Expand Down Expand Up @@ -478,7 +485,8 @@ private void ProcessRegularType(JsonPointer path, JsonSchema subSchema, SchemaCo
DataBindingName = GetDataBindingName(ElementType.Group, maxOccurs, id, null, xPath),
DisplayString = GetDisplayString(id, typeName, minOccurs, maxOccurs),
IsTagContent = context.XmlText,
Nillable = context.IsNillable
Nillable = context.IsNillable,
OrderOblivious = context.OrderOblivious
});
}

Expand Down Expand Up @@ -534,7 +542,8 @@ private void AddElement(JsonPointer path, JsonSchema subSchema, SchemaContext co
DataBindingName = GetDataBindingName(@type, maxOccurs, id, fixedValue, xPath),
DisplayString = GetDisplayString(id, context.SchemaValueType.ToString(), minOccurs, maxOccurs),
IsTagContent = context.XmlText,
Nillable = context.IsNillable
Nillable = context.IsNillable,
OrderOblivious = context.OrderOblivious
});
}

Expand Down
6 changes: 6 additions & 0 deletions backend/src/DataModeling/Metamodel/ElementMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,11 @@ public ElementMetadata()
[JsonProperty(PropertyName = "nillable")]
[JsonPropertyName("nillable")]
public bool? Nillable { get; set; }

[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
[JsonProperty(PropertyName = "orderOblivious")]
[JsonPropertyName("orderOblivious")]
public bool OrderOblivious { get; set; } = false;
}
}
12 changes: 12 additions & 0 deletions backend/src/DataModeling/Utils/JsonSchemaExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Altinn.Studio.DataModeling.Json.Keywords;
using Json.Schema;

namespace Altinn.Studio.DataModeling.Utils
Expand Down Expand Up @@ -185,5 +186,16 @@ public static JsonSchemaBuilder Type(this JsonSchemaBuilder builder, SchemaValue

return builder;
}

/// <summary>
/// Orders the keywords by priority.
/// Currently the only keyword that should be prioritized is <see cref="XsdStructureKeyword"/>.
/// </summary>
/// <param name="keywords"></param>
/// <returns></returns>
public static IEnumerable<IJsonSchemaKeyword> OrderByPriority(this IEnumerable<IJsonSchemaKeyword> keywords)
{
return keywords.OrderBy(item => item.GetType() != typeof(XsdStructureKeyword));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.IO;
using System.Linq;
using System.Linq;
using System.Xml.Serialization;
using Altinn.Studio.DataModeling.Converter.Csharp;
using DataModeling.Tests.Assertions;
using DataModeling.Tests.BaseClasses;
using DataModeling.Tests.TestDataClasses;
using Designer.Tests.Factories.ModelFactory.DataClasses;
using FluentAssertions;
using SharedResources.Tests;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@ public class CSharpE2ERestrictionsTestData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t1", "string", "[MinLength(5)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t1", "string", "[MaxLength(20)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t2", "string", "[MinLength(10)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t2", "string", "[MaxLength(10)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t4", "string", @"[RegularExpression(@""^\d\.\d\.\d$"")]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "n1", "decimal?", @"[RegularExpression(@""^-?(([0-9]){1}(\.)?){0,10}$"")]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "n1", "decimal?", "[Range(-100d, 100d)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "n1", "decimal?", "[Required]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "i1", "int?", @"[RegularExpression(@""^-?[0-9]{0,10}$"")]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "i1", "int?", "[Required]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "i2", "decimal?", @"[RegularExpression(@""^-?[0-9]{0,10}$"")]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "i2", "decimal?", "[Required]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "nonPrimitive", "string", @"[RegularExpression(@""[0-9]+"")]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "nonPrimitive", "string", "[MinLength(5)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "nonPrimitive", "string", "[MaxLength(20)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "intRangeWithoutLimits", "int?", "[Range(Int32.MinValue, Int32.MaxValue)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "intRangeWithLimits", "int?", "[Range(-100, 100)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "intRangeLeftLimit", "int?", "[Range(-100, Int32.MaxValue)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "intRangeRightLimit", "int?", "[Range(Int32.MinValue, 100)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "integerRangeWithoutLimits", "decimal?", "[Range(Double.MinValue, Double.MaxValue)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "integerRangeWithLimits", "decimal?", "[Range(-100d, 100d)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "integerRangeLeftLimit", "decimal?", "[Range(-100d, Double.MaxValue)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "integerRangeRightLimit", "decimal?", "[Range(Double.MinValue, 100d)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "longRangeWithoutLimits", "long?", "[Range(Int64.MinValue, Int64.MaxValue)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "longRangeWithLimits", "long?", "[Range(-100, 100)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "longRangeLeftLimit", "long?", "[Range(-100, Int64.MaxValue)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "longRangeRightLimit", "long?", "[Range(Int64.MinValue, 100)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "decimalRangeWithLimits", "decimal?", "[Range(-100.0, 100.0)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "decimalRangeLeftLimit", "decimal?", "[Range(-100.0, Double.MaxValue)]" };
yield return new object[] { "Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "decimalRangeRightLimit", "decimal?", "[Range(Double.MinValue, 100.0d)]" };
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t1", "string", "[MinLength(5)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t1", "string", "[MaxLength(20)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t2", "string", "[MinLength(10)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t2", "string", "[MaxLength(10)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "t4", "string", @"[RegularExpression(@""^\d\.\d\.\d$"")]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "n1", "decimal?", @"[RegularExpression(@""^-?(([0-9]){1}(\.)?){0,10}$"")]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "n1", "decimal?", "[Range(-100d, 100d)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "n1", "decimal?", "[Required]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "i1", "int?", @"[RegularExpression(@""^-?[0-9]{0,10}$"")]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "i1", "int?", "[Required]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "i2", "decimal?", @"[RegularExpression(@""^-?[0-9]{0,10}$"")]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "i2", "decimal?", "[Required]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "nonPrimitive", "string", @"[RegularExpression(@""[0-9]+"")]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "nonPrimitive", "string", "[MinLength(5)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "nonPrimitive", "string", "[MaxLength(20)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "intRangeWithoutLimits", "int?", "[Range(Int32.MinValue, Int32.MaxValue)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "intRangeWithLimits", "int?", "[Range(-100, 100)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "intRangeLeftLimit", "int?", "[Range(-100, Int32.MaxValue)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "intRangeRightLimit", "int?", "[Range(Int32.MinValue, 100)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "integerRangeWithoutLimits", "decimal?", "[Range(Double.MinValue, Double.MaxValue)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "integerRangeWithLimits", "decimal?", "[Range(-100d, 100d)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "integerRangeLeftLimit", "decimal?", "[Range(-100d, Double.MaxValue)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "integerRangeRightLimit", "decimal?", "[Range(Double.MinValue, 100d)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "longRangeWithoutLimits", "long?", "[Range(Int64.MinValue, Int64.MaxValue)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "longRangeWithLimits", "long?", "[Range(-100, 100)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "longRangeLeftLimit", "long?", "[Range(-100, Int64.MaxValue)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "longRangeRightLimit", "long?", "[Range(Int64.MinValue, 100)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "decimalRangeWithLimits", "decimal?", "[Range(-100.0, 100.0)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "decimalRangeLeftLimit", "decimal?", "[Range(-100.0, Double.MaxValue)]"];
yield return ["Model/XmlSchema/General/SimpleTypeRestrictionsExtended.xsd", "decimalRangeRightLimit", "decimal?", "[Range(Double.MinValue, 100.0d)]"];
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down
Loading
Loading