Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mganss/XmlSchemaClassGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
mganss committed Dec 14, 2023
2 parents d400ed3 + bbebb82 commit c8f38c1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
59 changes: 58 additions & 1 deletion XmlSchemaClassGenerator.Tests/DateTimeTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;
Expand Down Expand Up @@ -46,10 +47,66 @@ private static IEnumerable<string> ConvertXml(string xsd, Generator generatorPro
return writer.Content;
}

[Fact]
public void WhenDateTimeOffsetIsUsed_NoDataTypePropertyIsPresent()
{
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=""someDate"" type=""xs:dateTime"" />
</xs:sequence>
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(
xsd, new()
{
NamespaceProvider = new()
{
GenerateNamespace = _ => "Test"
},
DateTimeWithTimeZone = true
});

var expectedXmlSerializationAttribute = "[System.Xml.Serialization.XmlElementAttribute(\"someDate\")]";
var generatedProperty = generatedType.First();

Assert.Contains(expectedXmlSerializationAttribute, generatedProperty);
}

[Fact]
public void WhenDateTimeOffsetIsNotUsed_DataTypePropertyIsPresent()
{
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=""someDate"" type=""xs:dateTime"" />
</xs:sequence>
</xs:complexType>
</xs:schema>";

var generatedType = ConvertXml(
xsd, new()
{
NamespaceProvider = new()
{
GenerateNamespace = _ => "Test"
},
DateTimeWithTimeZone = false
});

var expectedXmlSerializationAttribute = "[System.Xml.Serialization.XmlElementAttribute(\"someDate\", DataType=\"dateTime\")]";
var generatedProperty = generatedType.First();

Assert.Contains(expectedXmlSerializationAttribute, generatedProperty);
}

[Theory]
[InlineData(false, "System.DateTime")]
[InlineData(true, "System.DateTimeOffset")]
public void TestFallbackType(bool dateTimeWithTimeZone, string expectedType)
public void TestCorrectDateTimeDataType(bool dateTimeWithTimeZone, string expectedType)
{
var xsd = @$"<?xml version=""1.0"" encoding=""UTF-8""?>
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
Expand Down
4 changes: 3 additions & 1 deletion XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,9 @@ private IEnumerable<CodeAttributeDeclaration> GetAttributes(bool isArray, TypeMo
while (xmlSchemaType != null)
{
var qualifiedName = xmlSchemaType.GetQualifiedName();
if (qualifiedName.Namespace == XmlSchema.Namespace && qualifiedName.Name != "anySimpleType")

if ((qualifiedName.Namespace == XmlSchema.Namespace && qualifiedName.Name != "anySimpleType") &&
(xmlSchemaType.Datatype.ValueType == typeof(DateTime) && configuration.DateTimeWithTimeZone) == false)
{
args.Add(new("DataType", new CodePrimitiveExpression(qualifiedName.Name)));
break;
Expand Down

0 comments on commit c8f38c1

Please sign in to comment.