Skip to content

Commit

Permalink
Add support for ParseLimitsInInvariantCulture and ConvertValueInInvar…
Browse files Browse the repository at this point in the history
…iantCulture (fixes #268)
  • Loading branch information
Michael Ganss committed May 27, 2021
1 parent 5c1c3e5 commit 729c1b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
13 changes: 13 additions & 0 deletions XmlSchemaClassGenerator.Tests/xsd/simple/restriction.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="DecimalLimitedType">
<xsd:restriction base="xsd:decimal">
<xsd:minInclusive value="10.00"/>
<xsd:maxInclusive value="24.99"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="minmax">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="val" type="DecimalLimitedType"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
26 changes: 21 additions & 5 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,11 +1523,27 @@ public IEnumerable<CodeAttributeDeclaration> GetRestrictionAttributes()

if (minInclusive != null && maxInclusive != null)
{
yield return new CodeAttributeDeclaration(
CodeUtilities.CreateTypeReference(typeof(RangeAttribute), Configuration),
new CodeAttributeArgument(new CodeTypeOfExpression(minInclusive.Type)),
new CodeAttributeArgument(new CodePrimitiveExpression(minInclusive.Value)),
new CodeAttributeArgument(new CodePrimitiveExpression(maxInclusive.Value)));
var rangeAttribute = new CodeAttributeDeclaration(
CodeUtilities.CreateTypeReference(typeof(RangeAttribute), Configuration),
new CodeAttributeArgument(new CodeTypeOfExpression(minInclusive.Type)),
new CodeAttributeArgument(new CodePrimitiveExpression(minInclusive.Value)),
new CodeAttributeArgument(new CodePrimitiveExpression(maxInclusive.Value)));

// see https://github.com/mganss/XmlSchemaClassGenerator/issues/268
if (Configuration.NetCoreSpecificCode)
{
if (minInclusive.Value.Contains(".") || maxInclusive.Value.Contains("."))
{
rangeAttribute.Arguments.Add(new CodeAttributeArgument("ParseLimitsInInvariantCulture", new CodePrimitiveExpression(true)));
}

if (minInclusive.Type != typeof(int) && minInclusive.Type != typeof(double))
{
rangeAttribute.Arguments.Add(new CodeAttributeArgument("ConvertValueInInvariantCulture", new CodePrimitiveExpression(true)));
}
}

yield return rangeAttribute;
}
}
}
Expand Down

0 comments on commit 729c1b5

Please sign in to comment.