Skip to content

Commit

Permalink
hotfix: non nillable props (#11518)
Browse files Browse the repository at this point in the history
* init implementation and test

* Fix test data for Brønnøysundregistrene test data

* dat-bilpleie test data fix

* dat-skjema test data fix

* nsm-klareringsportalen data fix

* hi-algeskjema test data fix

* skjema test data fix

* bokskjema test data fix
  • Loading branch information
mirkoSekulic authored Nov 6, 2023
1 parent 6fcebc0 commit 87d2a81
Show file tree
Hide file tree
Showing 13 changed files with 1,023 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ private void ParseFieldProperty(ElementMetadata element, StringBuilder classBuil

bool shouldBeNullable = isValueType && !element.IsTagContent; // Can't use complex type for XmlText.
classBuilder.AppendLine(Indent(2) + "public " + dataType + (shouldBeNullable ? "?" : string.Empty) + " " + element.Name + " { get; set; }\n");
if (shouldBeNullable && element.Nillable.HasValue && !element.Nillable.Value && element.MinOccurs == 0)
{
WriteShouldSerializeMethod(classBuilder, element.Name);
}
}
}

Expand Down Expand Up @@ -211,11 +215,11 @@ private void ParseAttributeProperty(ElementMetadata element, StringBuilder class
classBuilder.AppendLine(Indent(2) + "[BindNever]");
if (dataType.Equals("string"))
{
classBuilder.AppendLine(Indent(2) + "public " + dataType + " " + element.Name + " {get; set; } = \"" + element.FixedValue + "\";\n");
classBuilder.AppendLine(Indent(2) + "public " + dataType + " " + element.Name + " { get; set; } = \"" + element.FixedValue + "\";\n");
}
else
{
classBuilder.AppendLine(Indent(2) + "public " + dataType + " " + element.Name + " {get; set;} = " + element.FixedValue + ";\n");
classBuilder.AppendLine(Indent(2) + "public " + dataType + " " + element.Name + " { get; set; } = " + element.FixedValue + ";\n");
}
}
else
Expand Down Expand Up @@ -427,7 +431,18 @@ or BaseValueType.Date
BaseValueType.Long => ("long", true),
_ => throw new CsharpGenerationException("Unsupported type: " + typeName)
};
}

/// <summary>
/// When nillable is set in xsd and minOccurs set to 0 nil attribute should not be set when serializing. Xml serializer by default sets attribute as true for nullable types.
/// </summary>
private void WriteShouldSerializeMethod(StringBuilder classBuilder, string propName)
{
classBuilder.AppendLine(Indent(2) + $"public bool ShouldSerialize{propName}()");
classBuilder.AppendLine(Indent(2) + "{");
classBuilder.AppendLine(Indent(3) + $"return {propName}.HasValue;");
classBuilder.AppendLine(Indent(2) + "}");
classBuilder.AppendLine();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ private void ProcessRegularType(JsonPointer path, JsonSchema subSchema, SchemaCo
Restrictions = context.Restrictions,
DataBindingName = GetDataBindingName(ElementType.Group, maxOccurs, id, null, xPath),
DisplayString = GetDisplayString(id, typeName, minOccurs, maxOccurs),
IsTagContent = context.XmlText
IsTagContent = context.XmlText,
Nillable = context.IsNillable
});
}

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

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 @@ -141,5 +141,11 @@ public ElementMetadata()
/// </summary>
[JsonProperty(PropertyName = "displayString")]
public string DisplayString { get; set; }

/// <summary>
/// Used for xsd context. Indicates if nillable is set in xsd schema.
/// </summary>
[JsonProperty(PropertyName = "nillable")]
public bool? Nillable { get; set; }
}
}
28 changes: 22 additions & 6 deletions backend/tests/DataModeling.Tests/Assertions/TypeAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public static void IsEquivalentTo(Type expected, Type actual)
}
}

if (expected.IsClass)
if (expected.IsClass && !expected.IsGenericType)
{
IsEquivalentTo(expected.GetFields(), actual.GetFields());
IsEquivalentTo(expected.GetProperties(), actual.GetProperties());
IsEquivalentTo(expected.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly), actual.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));
}

IsEquivalentTo(expected.Attributes, actual.Attributes);
Expand Down Expand Up @@ -66,12 +67,10 @@ private static void IsEquivalentTo(FieldInfo expected, FieldInfo actual)
private static void IsEquivalentTo(IReadOnlyCollection<PropertyInfo> expected, IReadOnlyCollection<PropertyInfo> actual)
{
expected.Count.Should().Be(actual.Count);
foreach (var expectedItem in expected)
{
foreach (var expectedItem in expected)
{
var actualItem = actual.Single(x => x.Name == expectedItem.Name);
IsEquivalentTo(expectedItem, actualItem);
}
var actualItem = actual.Single(x => x.Name == expectedItem.Name);
IsEquivalentTo(expectedItem, actualItem);
}
}

Expand All @@ -97,6 +96,23 @@ private static void IsEquivalentTo(TypeAttributes expected, TypeAttributes actua
expected.Should().Be(actual);
}

private static void IsEquivalentTo(IReadOnlyCollection<MethodInfo> expected, IReadOnlyCollection<MethodInfo> actual)
{
expected.Count.Should().Be(actual.Count);
foreach (var expectedItem in expected)
{
var actualItem = actual.Single(x => x.Name == expectedItem.Name);
IsEquivalentTo(expectedItem, actualItem);
}
}

private static void IsEquivalentTo(MethodInfo expected, MethodInfo actual)
{
expected.Name.Should().Be(actual.Name);
IsEquivalentTo(expected.ReturnType, actual.ReturnType);
actual.IsPublic.Should().Be(expected.IsPublic);
}

public static void PropertyShouldContainCustomAnnotationAndHaveTypeType(Type type, string propertyName, string propertyType, string expectedAnnotationString)
{
var property = type.Properties().Single(x => x.Name == propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public IEnumerator<object[]> GetEnumerator()
yield return new object[] { "Model/XmlSchema/Gitea/skd-formueinntekt-skattemelding-v2.xsd", "Model/CSharp/Gitea/skd-formueinntekt-skattemelding-v2.cs" };
yield return new object[] { "Model/XmlSchema/Gitea/aal-vedlegg.xsd", "Model/CSharp/Gitea/aal-vedlegg.cs" };
yield return new object[] { "Model/XmlSchema/Gitea/krt-1188a-1.xsd", "Model/CSharp/Gitea/krt-1188a-1.cs" };
yield return new object[] { "Model/XmlSchema/Gitea/Brønnøysundregistrene_ReelleRettighetshavere_M.xsd", "Model/CSharp/Gitea/Brønnøysundregistrene_ReelleRettighetshavere_M.cs" };
}

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

0 comments on commit 87d2a81

Please sign in to comment.