Skip to content

Commit

Permalink
Merge pull request #405 from kronic/master
Browse files Browse the repository at this point in the history
decimal FractionDigitsRestrictionModel.Value is 0 as integer
  • Loading branch information
mganss authored Aug 8, 2023
2 parents 5e5b5e1 + 58dd175 commit 32f94ea
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 31 deletions.
182 changes: 153 additions & 29 deletions XmlSchemaClassGenerator.Tests/IntegerTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ public void TestTotalDigits(int totalDigits, string expectedType)
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:totalDigits value=""{totalDigits}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:totalDigits value=""{totalDigits}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";

Expand All @@ -99,15 +99,15 @@ public void TestFallbackType(int totalDigits, bool useTypeAsFallback, string exp
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:totalDigits value=""{totalDigits}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:totalDigits value=""{totalDigits}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";

Expand Down Expand Up @@ -147,16 +147,16 @@ public void TestInclusiveRange(long minInclusive, ulong maxInclusive, string exp
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:minInclusive value=""{minInclusive}""/>
<xs:maxInclusive value=""{maxInclusive}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:minInclusive value=""{minInclusive}""/>
<xs:maxInclusive value=""{maxInclusive}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";

Expand All @@ -173,5 +173,129 @@ public void TestInclusiveRange(long minInclusive, ulong maxInclusive, string exp

Assert.Contains(expectedProperty, generatedProperty);
}
}

[Theory]
[InlineData(2, "sbyte")]
[InlineData(4, "short")]
[InlineData(9, "int")]
[InlineData(18, "long")]
[InlineData(28, "decimal")]
[InlineData(29, "string")]
public void TestDecimalFractionDigitsZeroTotalDigits(int totalDigits, string expectedType)
{
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:totalDigits value=""{totalDigits}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
}
});

var expectedProperty = $"public {expectedType} SomeValue";
Assert.Contains(expectedProperty, generatedType.First());
}


[Theory]
[InlineData(4, false, "long")]
[InlineData(30, false, "long")]
[InlineData(4, true, "short")]
[InlineData(30, true, "long")]
public void TestDecimalFractionDigitsFallbackType(int totalDigits, bool useTypeAsFallback, string expectedType)
{
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:integer"">
<xs:totalDigits value=""{totalDigits}""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
},
IntegerDataType = typeof(long),
UseIntegerDataTypeAsFallback = useTypeAsFallback
});

var expectedProperty = $"public {expectedType} SomeValue";
Assert.Contains(expectedProperty, generatedType.First());
}

[Theory]
[InlineData(1, 100, "byte")]
[InlineData(byte.MinValue, byte.MaxValue, "byte")]
[InlineData(-100, 100, "sbyte")]
[InlineData(sbyte.MinValue, sbyte.MaxValue, "sbyte")]
[InlineData(1, 1000, "ushort")]
[InlineData(ushort.MinValue, ushort.MaxValue, "ushort")]
[InlineData(-1000, 1000, "short")]
[InlineData(short.MinValue, short.MaxValue, "short")]
[InlineData(1, 100000, "uint")]
[InlineData(uint.MinValue, uint.MaxValue, "uint")]
[InlineData(-100000, 100000, "int")]
[InlineData(int.MinValue, int.MaxValue, "int")]
[InlineData(1, 10000000000, "ulong")]
[InlineData(ulong.MinValue, ulong.MaxValue, "ulong")]
[InlineData(-10000000000, 10000000000, "long")]
[InlineData(long.MinValue, long.MaxValue, "long")]
public void TestDecimalFractionDigitsZeroInclusiveRange(long minInclusive, ulong maxInclusive, string expectedType)
{
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""document"">
<xs:sequence>
<xs:element name=""someValue"">
<xs:simpleType>
<xs:restriction base=""xs:decimal"">
<xs:minInclusive value=""{minInclusive}""/>
<xs:maxInclusive value=""{maxInclusive}""/>
<xs:fractionDigits value=""0""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(nameof(TestTotalDigits), xsd, new Generator
{
NamespaceProvider = new NamespaceProvider
{
GenerateNamespace = key => "Test"
}
});

var expectedProperty = $"public {expectedType} SomeValue";
var generatedProperty = generatedType.First();

Assert.Contains(expectedProperty, generatedProperty);
}
}

}
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator.Tests/SharedTestFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void validate2(object s, ValidationEventArgs e)
{
if (HandleValidationError(output, xmlLines, e))
throw e.Exception;
};
}

settings.ValidationEventHandler += validate2;

Expand Down
3 changes: 2 additions & 1 deletion XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfig
XmlTypeCode.Time => typeof(DateTime),
XmlTypeCode.Idref => typeof(string),
XmlTypeCode.Integer or XmlTypeCode.NegativeInteger or XmlTypeCode.NonNegativeInteger or XmlTypeCode.NonPositiveInteger or XmlTypeCode.PositiveInteger => GetIntegerDerivedType(type, configuration, restrictions),
_ => type.ValueType,
XmlTypeCode.Decimal when restrictions.OfType<FractionDigitsRestrictionModel>().SingleOrDefault() is { IsSupported: true, Value: 0 } => GetIntegerDerivedType(type, configuration, restrictions),
_ => type.ValueType,
};

if (schemaType.IsDerivedFrom(GuidQualifiedName))
Expand Down

0 comments on commit 32f94ea

Please sign in to comment.